Skip to content

Commit f00e9ae

Browse files
committed
Distance calculations
1 parent 78f4fb6 commit f00e9ae

6 files changed

Lines changed: 187 additions & 58 deletions

File tree

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

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
package org.teachingkidsprogramming.section08tdd;
22

3-
import java.awt.Graphics;
4-
import java.awt.Graphics2D;
5-
import java.awt.Image;
6-
import java.awt.Point;
3+
import org.teachingextensions.approvals.lite.util.ObjectUtils;
4+
import org.teachingextensions.logo.PenColors;
5+
6+
import javax.swing.*;
7+
import java.awt.*;
78
import java.util.ArrayList;
9+
import java.util.Arrays;
810
import java.util.Collections;
911
import java.util.List;
1012

11-
import javax.swing.JPanel;
12-
13-
import org.teachingextensions.approvals.lite.util.ObjectUtils;
14-
import org.teachingextensions.logo.PenColors;
15-
1613
public class PuzzleBoard extends JPanel {
1714
private static final String completed = "Batgirl.png";
1815
private static final long serialVersionUID = -3592444274530147326L;
19-
private final List<Tile> tiles;
20-
private final List<Point> positions;
21-
private boolean done;
16+
private final List<Tile> tiles;
17+
private final Point[] positions;
18+
private boolean done;
2219

2320
public PuzzleBoard() {
2421
this.positions = createPositions();
2522
this.tiles = createTiles(shuffled(this.positions));
2623

2724
}
2825

29-
public PuzzleBoard(List<Point> positions, List<Tile> tiles) {
30-
this.positions = new ArrayList<>(positions.size());
31-
for (int i = 0; i < positions.size(); i++) {
32-
Point p = positions.get(i);
33-
this.positions.add(i, new Point(p));
26+
public PuzzleBoard(Point[] positions, List<Tile> tiles) {
27+
this.positions = new Point[positions.length];
28+
for (int i = 0; i < positions.length; i++) {
29+
this.positions[i] = new Point(positions[i]);
3430
}
3531

3632
this.tiles = new ArrayList<>(tiles.size());
@@ -44,14 +40,14 @@ public PuzzleBoard(PuzzleBoard puzzle) {
4440
this(puzzle.positions, puzzle.tiles);
4541
}
4642

47-
private static List<Point> shuffled(List<Point> positions) {
48-
List<Point> s = new ArrayList<>(positions);
43+
private static List<Point> shuffled(Point[] positions) {
44+
List<Point> s = Arrays.asList(positions);
4945
Collections.shuffle(s);
5046
return s;
5147
}
5248

53-
public static List<Point> createPositions() {
54-
ArrayList<Point> p = new ArrayList<>(9);
49+
public static Point[] createPositions() {
50+
Point[] p = new Point[9];
5551
Point point;
5652
for (int i = 0; i < 9; i++) {
5753
if (i < 3) {
@@ -61,7 +57,7 @@ public static List<Point> createPositions() {
6157
} else {
6258
point = new Point(289, 35 + (127 * (i - 6)));
6359
}
64-
p.add(point);
60+
p[i] = point;
6561
}
6662
return p;
6763
}
@@ -74,6 +70,10 @@ public static List<Tile> createTiles(List<Point> positions) {
7470
return t;
7571
}
7672

73+
public static List<Tile> createTiles(Point[] positions) {
74+
return createTiles(Arrays.asList(positions));
75+
}
76+
7777
@Override
7878
protected void paintComponent(Graphics g) {
7979
super.paintComponent(g);
@@ -89,7 +89,7 @@ protected void paintComponent(Graphics g) {
8989
private void drawReward(Graphics g) {
9090
Image image = ObjectUtils.loadImage(this.getClass(), completed);
9191
Graphics2D g2d = (Graphics2D) g.create();
92-
g2d.drawImage(image, this.positions.get(0).x, this.positions.get(0).y, 376,
92+
g2d.drawImage(image, this.positions[0].x, this.positions[0].y, 376,
9393
376, null);
9494
g2d.dispose();
9595
}
@@ -117,7 +117,7 @@ public Tile getPiece(int i) {
117117
}
118118

119119
public List<Point> getPositions() {
120-
return new ArrayList<>(positions);
120+
return Arrays.asList(positions);
121121
}
122122

123123
public boolean isSorted() {
@@ -128,7 +128,7 @@ private int countMisplaced() {
128128
int misplaced = 0;
129129
for (int i = 0; i < 8; i++) {
130130
Tile tile = this.tiles.get(i);
131-
Point point = this.positions.get(i);
131+
Point point = this.positions[i];
132132
if (!tile.isAt(point)) {
133133
misplaced++;
134134
}
@@ -148,12 +148,12 @@ public List<Tile> getTiles() {
148148
* A move is valid if the target is the board's blank square
149149
*
150150
* @param move
151-
* the move to check
151+
* the move to check
152152
* @return true if the move is valid
153153
*/
154154
public boolean isValidMove(TileMove move) {
155155
Point blank = findBlank();
156-
return blank == this.positions.get(move.getTarget());
156+
return blank == this.positions[move.getTarget()];
157157
}
158158

159159
private Point findBlank() {
@@ -180,14 +180,14 @@ private boolean anyTileIsAt(Point point) {
180180
* Create a copy of the board then use the provided move to update it
181181
*
182182
* @param move
183-
* the move to perform on the puzzle copy
183+
* the move to perform on the puzzle copy
184184
* @return The updated copy of the board
185185
*/
186186
public PuzzleBoard useMove(TileMove move) {
187187
PuzzleBoard c = new PuzzleBoard(this);
188188

189189
Tile s = c.getPieceFromPosition(move.getSource());
190-
s.moveTo(c.positions.get(move.getTarget()));
190+
s.moveTo(c.positions[move.getTarget()]);
191191
s.teleport();
192192

193193
return c;
@@ -198,7 +198,7 @@ public PuzzleBoard useMove(TileMove move) {
198198
* solution.
199199
*
200200
* @param history
201-
* All the steps we have already visited.
201+
* All the steps we have already visited.
202202
* @return The estimated cost
203203
*/
204204
public int estimateCost(List<PuzzleBoard> history) {
@@ -234,10 +234,8 @@ public boolean equals(Object o) {
234234

235235
PuzzleBoard that = (PuzzleBoard) o;
236236

237-
if (!tiles.equals(that.tiles))
238-
return false;
237+
return tiles.equals(that.tiles);
239238

240-
return true;
241239
}
242240

243241
@Override
@@ -257,7 +255,7 @@ private int timesVisited(List<PuzzleBoard> history) {
257255
}
258256

259257
public Tile getPieceFromPosition(int source) {
260-
Point position = this.positions.get(source);
258+
Point position = this.positions[source];
261259
for (Tile tile : this.tiles) {
262260
if (tile.isAt(position)) {
263261
return tile;

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package org.teachingkidsprogramming.section08tdd;
22

3-
import java.awt.Point;
3+
import javax.swing.*;
4+
import java.awt.*;
45
import java.util.ArrayList;
56
import java.util.List;
67

7-
import javax.swing.SwingUtilities;
8-
98
public class PuzzleSolver implements Runnable {
10-
private final PuzzleBoard board;
9+
private final PuzzleBoard board;
1110
private final List<PuzzleBoard> history = new ArrayList<>();
1211

1312
public PuzzleSolver(PuzzleBoard board) {
@@ -155,4 +154,23 @@ private static void animationDemo(final PuzzleBoard puzzle) {
155154
piece.step();
156155
}
157156
}
157+
158+
public static int distance(Point start, Point end) {
159+
return Math.abs(start.x - end.x) + Math.abs(start.y - end.y);
160+
}
161+
162+
public static int distance(PuzzleBoard start, PuzzleBoard end) {
163+
int result = 0;
164+
for (Tile tile : start.getTiles()) {
165+
// tile has a position index of its goal
166+
int goalIndex = tile.getCorrectPositionIndex();
167+
// get the goal position
168+
Point goal = start.getPositions().get(goalIndex);
169+
// tile has a current position
170+
Point position = tile.getPosition();
171+
// sum the distances
172+
result += distance(position, goal);
173+
}
174+
return result;
175+
}
158176
}

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

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

3-
import java.awt.Graphics2D;
4-
import java.awt.Image;
5-
import java.awt.Point;
6-
73
import org.teachingextensions.approvals.lite.util.ObjectUtils;
84

5+
import java.awt.*;
6+
97
public class Tile {
10-
private final static String[] resources = { "Batgirl1a.png", "Batgirl1b.png",
8+
private final static String[] resources = {"Batgirl1a.png", "Batgirl1b.png",
119
"Batgirl1c.png", "Batgirl2a.png", "Batgirl2b.png", "Batgirl2c.png",
12-
"Batgirl3a.png", "Batgirl3b.png", "Batgirl3c.png" };
10+
"Batgirl3a.png", "Batgirl3b.png", "Batgirl3c.png"};
11+
1312
private final static int width = 122;
1413
private final static int height = 122;
15-
private Image image;
16-
private Point position;
17-
private Point target;
18-
private int imageIdx;
1914

20-
public Tile(int imageIdx, Point position) {
21-
this.imageIdx = imageIdx;
15+
private Image image;
16+
private Point position;
17+
private Point target;
18+
private int correctPositionIndex;
19+
20+
// TODO: point should be a current position index maybe?
21+
public Tile(int correctPosition, Point position) {
22+
this.correctPositionIndex = correctPosition;
2223
this.position = new Point(position);
23-
this.image = ObjectUtils.loadImage(this.getClass(), resources[imageIdx]);
24+
this.image = ObjectUtils.loadImage(this.getClass(), resources[correctPosition]);
2425
}
2526

2627
public Tile(Tile t) {
27-
this(t.imageIdx, new Point(t.position));
28+
this(t.correctPositionIndex, new Point(t.position));
2829
}
2930

3031
public void paint(Graphics2D g2d) {
@@ -85,4 +86,12 @@ public void teleport() {
8586
public boolean isAtTarget() {
8687
return this.isAt(this.target);
8788
}
89+
90+
public int getCorrectPositionIndex() {
91+
return correctPositionIndex;
92+
}
93+
94+
public Point getPosition() {
95+
return position;
96+
}
8897
}

src/main/java/org/teachingkidsprogramming/section08tdd/PuzzleBoardTest.java renamed to src/test/java/org/teachingextensions/logo/tests/PuzzleBoardTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.teachingkidsprogramming.section08tdd;
1+
package org.teachingextensions.logo.tests;
22

33
import static org.junit.Assert.assertFalse;
44
import static org.junit.Assert.assertTrue;
@@ -7,6 +7,9 @@
77
import java.util.List;
88

99
import org.junit.Test;
10+
import org.teachingkidsprogramming.section08tdd.PuzzleBoard;
11+
import org.teachingkidsprogramming.section08tdd.Tile;
12+
import org.teachingkidsprogramming.section08tdd.TileMove;
1013

1114
public class PuzzleBoardTest {
1215

@@ -18,11 +21,11 @@ public void defaultNotSortedTest() {
1821

1922
@Test
2023
public void notSortedTest() {
21-
List<Point> positions = PuzzleBoard.createPositions();
24+
Point[] positions = PuzzleBoard.createPositions();
2225
List<Tile> tiles = PuzzleBoard.createTiles(positions);
2326

2427
Tile t = tiles.get(7);
25-
t.moveTo(positions.get(8));
28+
t.moveTo(positions[8]);
2629
t.teleport();
2730

2831
PuzzleBoard b = new PuzzleBoard(positions, tiles);
@@ -36,11 +39,10 @@ public void sortedTest() {
3639
}
3740

3841
private PuzzleBoard getSolvedBoard() {
39-
List<Point> positions = PuzzleBoard.createPositions();
42+
Point[] positions = PuzzleBoard.createPositions();
4043
List<Tile> tiles = PuzzleBoard.createTiles(positions);
4144

42-
PuzzleBoard b = new PuzzleBoard(positions, tiles);
43-
return b;
45+
return new PuzzleBoard(positions, tiles);
4446
}
4547

4648
@Test

0 commit comments

Comments
 (0)