77import javax .swing .UIManager ;
88import javax .swing .UnsupportedLookAndFeelException ;
99
10- import org .teachingextensions .logo .AStarPlayer ;
1110import org .teachingextensions .logo .Puzzle ;
1211import org .teachingextensions .logo .PuzzleAnimation ;
1312import org .teachingextensions .logo .PuzzleBoard ;
14- import org .teachingextensions .logo .PuzzlePlayer ;
1513import org .teachingextensions .logo .PuzzleState ;
16- import org .teachingextensions .logo .PuzzleWindow ;
1714
1815public class SimplePuzzle implements Runnable
1916{
17+ public Puzzle puzzle = null ;
18+ public PuzzleState solution = null ;
19+ public int [] cells = {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
2020 public static void main (String [] args )
2121 {
2222 EventQueue .invokeLater (new SimplePuzzle ());
2323 }
24- @ SuppressWarnings ("unused" )
25- private static int [] shuffled (int [] source )
24+ public static int [] shuffled (int [] source )
2625 {
2726 int [] copy = Arrays .copyOf (source , source .length );
2827 Random rnd = new Random ();
2928 for (int i = copy .length - 1 ; i > 0 ; i --)
3029 {
3130 int index = rnd .nextInt (i + 1 );
32- // Simple swap
3331 int a = copy [index ];
3432 copy [index ] = copy [i ];
3533 copy [i ] = a ;
@@ -40,15 +38,21 @@ private static int[] shuffled(int[] source)
4038 public void run ()
4139 {
4240 this .setLookAndFeel ();
43- // int[] cells = {0, 1, 2, 3, 4, 5, 6, 7, 8};
44- // int[] shuffled = shuffled(cells);
45- int [] shuffled = {5 , 6 , 2 , 4 , 1 , 8 , 7 , 0 , 3 }; // known to be solvable
46- Puzzle puzzle = new Puzzle (shuffled );
47- PuzzlePlayer player = new AStarPlayer (puzzle );
48- PuzzleState solution = player .solve ();
41+ // Do this until the player finds the solution -- #6.1
42+ // Create a Message Box that shows the message "Looking for puzzle solution..." -- #4
43+ // Try to create a solvable puzzle -- #5.1
44+ // Create an array of integers named 'shuffled' which shuffles the cell array -- #2.1
45+ // Make the puzzle use the cells array, run it, then use the shuffled array -- #2.2
46+ puzzle = new Puzzle (cells );
47+ // Create a AStarPlayer named 'player' which uses the current puzzle -- #3.1
48+ // Create a solution by telling the player to solve it -- #3.2 TIP: Not all puzzles can be solved!
49+ // NOTE for teacher - have kids run it multiple times here to see that sometimes it fails
50+ // End of try --#5.2
51+ // Create a Message Box that shows the message "This puzzle is not solvable, click ok to try again" -- #5.4
52+ // End of catch --#5.3
53+ // End of while --#6.2
4954 PuzzleBoard board = new PuzzleBoard (puzzle , solution );
50- PuzzleWindow pw = new PuzzleWindow (board );
51- pw .setVisible (true );
55+ // Create a new Puzzle Window that takes a parameter named board -- #1
5256 new Thread (new PuzzleAnimation (board )).start ();
5357 }
5458 private void setLookAndFeel ()
0 commit comments