Skip to content

Commit eccb28d

Browse files
committed
added Puzzle Board and Approvals Lite w/Jim
1 parent fb4cb33 commit eccb28d

File tree

6 files changed

+40
-242
lines changed

6 files changed

+40
-242
lines changed
236 KB
Binary file not shown.

TeachingKidsProgramming/src/org/teachingkidsprogramming/section08tdd/PuzzleBoard.java

Lines changed: 0 additions & 93 deletions
This file was deleted.

TeachingKidsProgramming/src/org/teachingkidsprogramming/section08tdd/PuzzleSolver.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

TeachingKidsProgramming/src/org/teachingkidsprogramming/section08tdd/PuzzleWindow.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

TeachingKidsProgramming/src/org/teachingkidsprogramming/section08tdd/SimplePuzzle.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,67 @@
11
package org.teachingkidsprogramming.section08tdd;
22

33
import java.awt.EventQueue;
4+
import java.util.Arrays;
5+
import java.util.Random;
46

57
import javax.swing.UIManager;
68
import javax.swing.UnsupportedLookAndFeelException;
79

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.PuzzlePlayer;
15+
import org.teachingextensions.logo.PuzzleState;
16+
import org.teachingextensions.logo.PuzzleWindow;
17+
18+
import com.spun.util.MySystem;
19+
820
public class SimplePuzzle implements Runnable
921
{
1022
public static void main(String[] args)
1123
{
1224
EventQueue.invokeLater(new SimplePuzzle());
1325
}
26+
@SuppressWarnings("unused")
27+
private 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+
// Simple swap
35+
int a = copy[index];
36+
copy[index] = copy[i];
37+
copy[i] = a;
38+
}
39+
return copy;
40+
}
41+
@Override
1442
public void run()
1543
{
1644
this.setLookAndFeel();
17-
PuzzleBoard board = new PuzzleBoard();
18-
PuzzleWindow pw = new PuzzleWindow();
19-
pw.add(board);
45+
// int[] cells = {0, 1, 2, 3, 4, 5, 6, 7, 8};
46+
// int[] shuffled = shuffled(cells);
47+
int[] shuffled = {5, 6, 2, 4, 1, 8, 7, 0, 3}; // known to be solvable
48+
MySystem.message(Arrays.toString(shuffled));
49+
Puzzle puzzle = new Puzzle(shuffled);
50+
PuzzlePlayer player = new AStarPlayer(puzzle);
51+
PuzzleState solution = player.solve();
52+
PuzzleBoard board = new PuzzleBoard(puzzle, solution);
53+
PuzzleWindow pw = new PuzzleWindow(board);
2054
pw.setVisible(true);
21-
new Thread(new PuzzleSolver(board)).start();
55+
new Thread(new PuzzleAnimation(board)).start();
2256
}
2357
private void setLookAndFeel()
2458
{
2559
try
2660
{
2761
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
2862
}
29-
catch (ClassNotFoundException ex)
30-
{
31-
}
32-
catch (InstantiationException ex)
33-
{
34-
}
35-
catch (IllegalAccessException ex)
36-
{
37-
}
38-
catch (UnsupportedLookAndFeelException ex)
63+
catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException
64+
| IllegalAccessException ignored)
3965
{
4066
}
4167
}

TeachingKidsProgramming/src/org/teachingkidsprogramming/section08tdd/Tile.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)