/* * letters.java * * Created on 09 July 2007, 18:35 */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.*; import java.lang.String; /** * * @author Dan Goodman */ public class letters extends MIDlet implements CommandListener { /** Creates a new instance of letters */ public letters() { initialize(); } private Form solver;//GEN-BEGIN:MVDFields private Command exitCommand1; private Command solveCommand; private TextField textFieldLetters; private StringItem stringItemSolution;//GEN-END:MVDFields //GEN-LINE:MVDMethods /** Called by the system to indicate that a command has been invoked on a particular displayable.//GEN-BEGIN:MVDCABegin * @param command the Command that ws invoked * @param displayable the Displayable on which the command was invoked */ public void commandAction(Command command, Displayable displayable) {//GEN-END:MVDCABegin // Insert global pre-action code here if (displayable == solver) {//GEN-BEGIN:MVDCABody if (command == solveCommand) {//GEN-END:MVDCABody // Insert pre-action code here String l=textFieldLetters.getString().toLowerCase(); if(l.length()>0){ try{ /* The algorithm works by reading in dict.dat each time you run the solve command * rather than loading it at the start because it saves on memory and it's plenty * quick enough as it is. The file dict.dat is a Java data file consisting of * a list of strings, which you read using the DataInputStream.readUTF method. * It's much quicker to read it like this than to have a text file which you have * to read byte by byte. The words are organised in dict.dat in order: 9 letter words * first, then 8 letters, and so on. You scan through the file, checking for each word * whether or not it can be made using the available letters, and if it can you add it * to the list of found words. Once you have 5 found words you stop. This guarantees * that you get 5 best words and wastes as little time as possible doing so. */ InputStream is = getClass().getResourceAsStream("/dict.dat"); if (is == null) throw new Exception("File Does Not Exist"); DataInputStream dis = new DataInputStream(is); int foundwords=0; stringItemSolution.setText(""); // start off with no found words, we add to this text display while(dis.available()>0 && foundwords<5){ String s = dis.readUTF(); // read the next word from the dictionary file StringBuffer lb = new StringBuffer(l); // an editable copy of the letters available // can we make the string s using the letters available? boolean makeable=true; for(int i=0;i-1){ lb.setCharAt(j,' '); // set the letter to ' ' to indicate it's been used up } else { makeable=false; // j=-1 means the letter we were looking for is not there so we can't make it } } if(makeable){ // we can make this word if(foundwords==0){ stringItemSolution.setText(s); // add to the list } else { stringItemSolution.setText(stringItemSolution.getText()+'\n'+s); // add to the list } foundwords++; } } dis.close(); // Do nothing//GEN-LINE:MVDCAAction8 } catch(Exception e) { System.out.println("Arg!"); System.out.println(e); } } // Insert post-action code here } else if (command == exitCommand1) {//GEN-LINE:MVDCACase8 // Insert pre-action code here exitMIDlet();//GEN-LINE:MVDCAAction7 // Insert post-action code here }//GEN-BEGIN:MVDCACase7 }//GEN-END:MVDCACase7 // Insert global post-action code here }//GEN-LINE:MVDCAEnd /** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin */ private void initialize() {//GEN-END:MVDInitBegin // Insert pre-init code here getDisplay().setCurrent(get_solver());//GEN-LINE:MVDInitInit // Insert post-init code here }//GEN-LINE:MVDInitEnd /** * This method should return an instance of the display. */ public Display getDisplay() {//GEN-FIRST:MVDGetDisplay return Display.getDisplay(this); }//GEN-LAST:MVDGetDisplay /** * This method should exit the midlet. */ public void exitMIDlet() {//GEN-FIRST:MVDExitMidlet getDisplay().setCurrent(null); destroyApp(true); notifyDestroyed(); }//GEN-LAST:MVDExitMidlet /** This method returns instance for solver component and should be called instead of accessing solver field directly.//GEN-BEGIN:MVDGetBegin2 * @return Instance for solver component */ public Form get_solver() { if (solver == null) {//GEN-END:MVDGetBegin2 // Insert pre-init code here solver = new Form("Countdown Letters solver", new Item[] {//GEN-BEGIN:MVDGetInit2 get_textFieldLetters(), get_stringItemSolution() }); solver.addCommand(get_exitCommand1()); solver.addCommand(get_solveCommand()); solver.setCommandListener(this);//GEN-END:MVDGetInit2 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd2 return solver; }//GEN-END:MVDGetEnd2 /** This method returns instance for exitCommand1 component and should be called instead of accessing exitCommand1 field directly.//GEN-BEGIN:MVDGetBegin3 * @return Instance for exitCommand1 component */ public Command get_exitCommand1() { if (exitCommand1 == null) {//GEN-END:MVDGetBegin3 // Insert pre-init code here exitCommand1 = new Command("Exit", Command.EXIT, 1);//GEN-LINE:MVDGetInit3 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd3 return exitCommand1; }//GEN-END:MVDGetEnd3 /** This method returns instance for solveCommand component and should be called instead of accessing solveCommand field directly.//GEN-BEGIN:MVDGetBegin4 * @return Instance for solveCommand component */ public Command get_solveCommand() { if (solveCommand == null) {//GEN-END:MVDGetBegin4 // Insert pre-init code here solveCommand = new Command("Solve", Command.OK, 1);//GEN-LINE:MVDGetInit4 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd4 return solveCommand; }//GEN-END:MVDGetEnd4 /** This method returns instance for textFieldLetters component and should be called instead of accessing textFieldLetters field directly.//GEN-BEGIN:MVDGetBegin6 * @return Instance for textFieldLetters component */ public TextField get_textFieldLetters() { if (textFieldLetters == null) {//GEN-END:MVDGetBegin6 // Insert pre-init code here textFieldLetters = new TextField("Letters: ", null, 120, TextField.ANY);//GEN-LINE:MVDGetInit6 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd6 return textFieldLetters; }//GEN-END:MVDGetEnd6 /** This method returns instance for stringItemSolution component and should be called instead of accessing stringItemSolution field directly.//GEN-BEGIN:MVDGetBegin21 * @return Instance for stringItemSolution component */ public StringItem get_stringItemSolution() { if (stringItemSolution == null) {//GEN-END:MVDGetBegin21 // Insert pre-init code here stringItemSolution = new StringItem("Best words: ", "");//GEN-LINE:MVDGetInit21 // Insert post-init code here }//GEN-BEGIN:MVDGetEnd21 return stringItemSolution; }//GEN-END:MVDGetEnd21 public void startApp() { } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }