|
import java.applet.Applet; import java.awt.*;
public class New extends Applet { boolean found = false; int ninepos,rowpos,colpos=0; List Q; TextField t1,t2; char chkeep[];
public void init() { Label lab1 = new Label("enter start"); add(lab1); t1 = new TextField(50); add(t1); Label lab2 = new Label("enter goal"); add(lab2); t2 = new TextField(50); add(t2); Q = new List(12,false); add(Q); }
public boolean action(Event e, Object o) { Q.addItem(t1.getText());
while (!found) { expand(Q.getItem(0)); Q.delItem(0); }
return true; }
public void expand(String s) { for (int x=0;x<9;x++) if (s.charAt(x)=='9') ninepos = x;
rowpos = ninepos / 3; colpos = ninepos % 3;
if (rowpos>0) { char ch[]=s.toCharArray(); char tmp = ch[ninepos]; ch[ninepos] = ch[ninepos-3]; ch[ninepos-3] = tmp; String stmp = String.copyValueOf(ch); Q.addItem(stmp);
if (t2.getText().equals(stmp)) found = true; }
if (rowpos<2) { char ch[]=s.toCharArray(); char tmp = ch[ninepos]; ch[ninepos] = ch[ninepos+3]; ch[ninepos+3] = tmp; String stmp = String.copyValueOf(ch); Q.addItem(stmp);
if (t2.getText().equals(stmp)) found = true; }
if (colpos>0) { char ch[]=s.toCharArray(); char tmp = ch[ninepos]; ch[ninepos] = ch[ninepos-1]; ch[ninepos-1] = tmp; String stmp = String.copyValueOf(ch); Q.addItem(stmp);
if (t2.getText().equals(stmp)) found = true; }
if (colpos<2) { char ch[]=s.toCharArray(); char tmp = ch[ninepos]; ch[ninepos] = ch[ninepos+1]; ch[ninepos+1] = tmp; String stmp = String.copyValueOf(ch); Q.addItem(stmp);
if (t2.getText().equals(stmp)) found = true; } } // end of expand methos } // end of class
|