Skip to content

Commit 937b74a

Browse files
committed
distance to goal
1 parent f00e9ae commit 937b74a

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void run() {
2929

3030
try {
3131
Thread.sleep(10);
32-
} catch (InterruptedException e) {
32+
} catch (InterruptedException ignored) {
3333
}
3434
}
3535
}
@@ -52,7 +52,7 @@ private void update(final PuzzleBoard puzzle) {
5252
private void solve(PuzzleBoard puzzle) {
5353
this.copyToHistory(puzzle);
5454

55-
int mininumCost = Integer.MAX_VALUE;
55+
int minimumCost = Integer.MAX_VALUE;
5656
TileMove min = null;
5757
// create a list of all moves that are possible
5858
List<TileMove> moves = createMoves();
@@ -68,9 +68,9 @@ private void solve(PuzzleBoard puzzle) {
6868
int estimate = estimateSolvingCost(next, this.history);
6969

7070
// if the cost is the smallest we've seen so far
71-
if (estimate < mininumCost) {
71+
if (estimate < minimumCost) {
7272
// remember the move and the cost
73-
mininumCost = estimate;
73+
minimumCost = estimate;
7474
min = tileMove;
7575
}
7676

@@ -144,6 +144,7 @@ private boolean animate(PuzzleBoard puzzle) {
144144
return true;
145145
}
146146

147+
@SuppressWarnings("UnusedDeclaration")
147148
private static void animationDemo(final PuzzleBoard puzzle) {
148149
Point target = puzzle.getPositions().get(8);
149150
Tile piece = puzzle.getPiece(7);
@@ -159,16 +160,12 @@ public static int distance(Point start, Point end) {
159160
return Math.abs(start.x - end.x) + Math.abs(start.y - end.y);
160161
}
161162

162-
public static int distance(PuzzleBoard start, PuzzleBoard end) {
163+
public static int distance(PuzzleBoard start) {
163164
int result = 0;
164165
for (Tile tile : start.getTiles()) {
165-
// tile has a position index of its goal
166166
int goalIndex = tile.getCorrectPositionIndex();
167-
// get the goal position
168167
Point goal = start.getPositions().get(goalIndex);
169-
// tile has a current position
170168
Point position = tile.getPosition();
171-
// sum the distances
172169
result += distance(position, goal);
173170
}
174171
return result;

src/test/java/org/teachingextensions/logo/tests/PuzzleSolverTest.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void manhattanDistanceFromEightToZeroTest() {
5757
@Test
5858
public void distanceFromSolvedToSolved() {
5959
PuzzleBoard solved = getSolvedPuzzle();
60-
assertEquals(0, PuzzleSolver.distance(solved, solved));
60+
assertEquals(0, PuzzleSolver.distance(solved));
6161
}
6262

6363
private PuzzleBoard getSolvedPuzzle() {
@@ -78,6 +78,39 @@ public void distanceForPuzzleWithOneMisplacedTile() {
7878
List<Tile> tiles = PuzzleBoard.createTiles(positions);
7979
PuzzleBoard b = new PuzzleBoard(PuzzleBoard.createPositions(), tiles);
8080
int expected = PuzzleSolver.distance(positions[8], positions[7]);
81-
assertEquals(expected, PuzzleSolver.distance(b, getSolvedPuzzle()));
81+
assertEquals(expected, PuzzleSolver.distance(b));
82+
}
83+
84+
@Test
85+
public void distanceForPuzzleWithSeveralMisplacedTiles() {
86+
Point[] positions = PuzzleBoard.createPositions();
87+
88+
positions = swap(positions, 7, 8);
89+
positions = swap(positions, 1, 4);
90+
positions = swap(positions, 6, 2);
91+
92+
List<Tile> tiles = PuzzleBoard.createTiles(positions);
93+
94+
// cost of swaps
95+
int expected = PuzzleSolver.distance(positions[8], positions[7]) +
96+
PuzzleSolver.distance(positions[1], positions[4]) +
97+
PuzzleSolver.distance(positions[6], positions[2]);
98+
99+
// each swap requires 2 tiles to move
100+
expected *= 2;
101+
102+
// except the blank
103+
expected -= PuzzleSolver.distance(positions[8], positions[7]);
104+
105+
106+
PuzzleBoard b = new PuzzleBoard(PuzzleBoard.createPositions(), tiles);
107+
assertEquals(expected, PuzzleSolver.distance(b));
108+
}
109+
110+
private Point[] swap(Point[] positions, int i, int j) {
111+
Point p = positions[i];
112+
positions[i] = positions[j];
113+
positions[j] = p;
114+
return positions;
82115
}
83116
}

0 commit comments

Comments
 (0)