Skip to content

Commit 98fa4a3

Browse files
committed
published new recipe - SimplePuzzle w/Jim
1 parent 43b48e5 commit 98fa4a3

File tree

2 files changed

+105
-14
lines changed

2 files changed

+105
-14
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.teachingkidsprogramming.recipes.completed;
2+
3+
import java.awt.EventQueue;
4+
import java.util.Arrays;
5+
import java.util.Random;
6+
7+
import javax.swing.UIManager;
8+
import javax.swing.UnsupportedLookAndFeelException;
9+
10+
import org.teachingextensions.logo.AStarPlayer;
11+
import org.teachingextensions.logo.Puzzle;
12+
import org.teachingextensions.logo.PuzzleAnimation;
13+
import org.teachingextensions.logo.PuzzleBoard;
14+
import org.teachingextensions.logo.PuzzleState;
15+
import org.teachingextensions.logo.PuzzleWindow;
16+
import org.teachingextensions.windows.MessageBox;
17+
18+
public class SimplePuzzle implements Runnable
19+
{
20+
public Puzzle puzzle = null;
21+
public PuzzleState solution = null;
22+
public int[] cells = {0, 1, 2, 3, 4, 5, 6, 7, 8};
23+
public static void main(String[] args)
24+
{
25+
EventQueue.invokeLater(new SimplePuzzle());
26+
}
27+
public static int[] shuffled(int[] source)
28+
{
29+
int[] copy = Arrays.copyOf(source, source.length);
30+
Random rnd = new Random();
31+
for (int i = copy.length - 1; i > 0; i--)
32+
{
33+
int index = rnd.nextInt(i + 1);
34+
int a = copy[index];
35+
copy[index] = copy[i];
36+
copy[i] = a;
37+
}
38+
return copy;
39+
}
40+
@Override
41+
public void run()
42+
{
43+
this.setLookAndFeel();
44+
// Do this until the player finds the solution -- #6.1
45+
while (solution == null)
46+
{
47+
// Create a Message Box that shows the message "Looking for puzzle solution..." -- #4
48+
MessageBox.showMessage("Looking for puzzle solution...");
49+
// Try to create a solvable puzzle -- #5.1
50+
try
51+
{
52+
// Create an array of integers named 'shuffled' which shuffles the cell array -- #2.1
53+
int[] shuffled = shuffled(cells);
54+
// Make the puzzle use the cells array, run it, then use the shuffled array -- #2.2
55+
puzzle = new Puzzle(shuffled);
56+
// Create a AStarPlayer named 'player' which uses the current puzzle -- #3.1
57+
AStarPlayer player = new AStarPlayer(puzzle);
58+
// Create a solution by telling the player to solve it -- #3.2 TIP: Not all puzzles can be solved!
59+
// NOTE for teacher - have kids run it multiple times here to see that sometimes it fails
60+
solution = player.solve();
61+
// End of try --#5.2
62+
}
63+
catch (Exception e)
64+
{
65+
// Create a Message Box that shows the message "This puzzle is not solvable, click ok to try again" -- #5.4
66+
MessageBox.showMessage("This puzzle is not solvable, click ok to try again");
67+
// End of catch --#5.3
68+
}
69+
// End of while --#6.2
70+
}
71+
PuzzleBoard board = new PuzzleBoard(puzzle, solution);
72+
// Create a new Puzzle Window that takes a parameter named board -- #1
73+
new PuzzleWindow(board);
74+
new Thread(new PuzzleAnimation(board)).start();
75+
}
76+
private void setLookAndFeel()
77+
{
78+
try
79+
{
80+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
81+
}
82+
catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException
83+
| IllegalAccessException ignored)
84+
{
85+
}
86+
}
87+
}

src/org/teachingkidsprogramming/section08tdd/SimplePuzzle.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,27 @@
77
import javax.swing.UIManager;
88
import javax.swing.UnsupportedLookAndFeelException;
99

10-
import org.teachingextensions.logo.AStarPlayer;
1110
import org.teachingextensions.logo.Puzzle;
1211
import org.teachingextensions.logo.PuzzleAnimation;
1312
import org.teachingextensions.logo.PuzzleBoard;
14-
import org.teachingextensions.logo.PuzzlePlayer;
1513
import org.teachingextensions.logo.PuzzleState;
16-
import org.teachingextensions.logo.PuzzleWindow;
1714

1815
public 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

Comments
 (0)