11package 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 .*;
78import java .util .ArrayList ;
9+ import java .util .Arrays ;
810import java .util .Collections ;
911import 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-
1613public 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 ;
0 commit comments