-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
415 lines (360 loc) · 10.1 KB
/
Board.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* ------------------------------------------------
* 8 Tiles GUI
*
* Class: CS 342, Fall 2016
* System: Windows 10, IntelliJ IDE
* Author: Five
* ------------------------------------------------- */
import java.util.*;
/**
* Underlying Board Structure class which represents the board by a 2D grid.
* Handles creation of random board and user selected board
* Handles movement of tiles on the board
*/
public class Board
{
private int[][] grid; //3x3 grid to store board pieces
private int hVal; //Heuristic value of board
private int[] movable; //Pieces able to be moved
private final int SIZE = Constants.BOARD_SIZE; //Size of board obtained from Constants class
public Board()
{
grid = loadBoard(); //Randomly loaded board;
hVal = heuristicCalc(); //Calculate goodness of board
movable = availableMoves();
}
public Board(int[] arr)
{
grid = loadBoard(arr);
hVal = heuristicCalc(); //Calculate goodness of board
movable = availableMoves();
}
public int[][] getGrid()
{
return grid;
}
public void setGrid(int[][] grid)
{
this.grid = grid;
}
public int gethVal()
{
return hVal;
}
public int[] getMovable()
{
return movable;
}
public int getSIZE()
{
return SIZE;
}
/**
* Loads in 3x3 grid with random, unique, value from given size range
* @return Randomly loaded grid
*/
private int[][] loadBoard()
{
int[][] retGrid = new int[SIZE][SIZE];
int[] nums = new int[SIZE*SIZE];
boolean flag = true;
int index;
Random generator = new Random(System.currentTimeMillis());
//Create number set that we'll be randomly loading into grid
for(int i=0;i<(SIZE*SIZE);i++)
{
nums[i] = i;
}
//Loop through grid
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
//Loop until unique number is allocated to spot [i][j]
while(flag)
{
index = (generator.nextInt(9)); //Generate rand num from size range (0-9)
if(nums[index] !=-1) //If number is not 'used'
{
retGrid[i][j] = nums[index];
nums[index] =-1; //Number is now 'used'
flag = false;
}
}
flag = true;
}//End for j
}//End for i
return retGrid;
}
/**
* Loads in 3x3 grid with user configuration value
* @return Configured loaded grid
*/
private int[][] loadBoard(int[] config)
{
int[][] retGrid = new int[SIZE][SIZE];
for(int i=0;i<config.length;i++)
{
if(i<3)
{
retGrid[0][i] = config[i];
}
else if(i>=3 && i<6)
{
retGrid[1][i-3] = config[i];
}
else
{
retGrid[2][i-6] = config[i];
}
}
return retGrid;
}
/**
* Calculates total heuristic value(goodness) of board
* @return Heuristic value of board
*/
public int heuristicCalc()
{
int heuristic =0;
int currVal;
int dx;
int dy;
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
currVal = grid[x][y];
switch(currVal)
{
case 0 :
dx = Math.abs(x-2);
dy = Math.abs(y-2);
heuristic += dx+dy;
break;
case 1:
dx = Math.abs(x-0);
dy = Math.abs(y-0);
heuristic += dx+dy;
break;
case 2:
dx = Math.abs(x-0);
dy = Math.abs(y-1);
heuristic += dx+dy;
break;
case 3:
dx = Math.abs(x-0);
dy = Math.abs(y-2);
heuristic += dx+dy;
break;
case 4:
dx = Math.abs(x-1);
dy = Math.abs(y-0);
heuristic += dx+dy;
break;
case 5:
dx = Math.abs(x-1);
dy = Math.abs(y-1);
heuristic += dx+dy;
break;
case 6:
dx = Math.abs(x-1);
dy = Math.abs(y-2);
heuristic += dx+dy;
break;
case 7:
dx = Math.abs(x-2);
dy = Math.abs(y-0);
heuristic += dx+dy;
break;
case 8:
dx = Math.abs(x-2);
dy = Math.abs(y-1);
heuristic += dx+dy;
break;
}//End switch
}
}
return heuristic;
}
/**
* Find open piece and identify north,south,east,and west pieces
* @return array of all possible movable pieces
*/
public int[] availableMoves()
{
int north =- 1; int south = -1;
int east = -1; int west= -1;
//Find the 'open' space coordinates
int[] openSpace = findSpace();
int x = openSpace[0];
int y = openSpace[1];
//North piece
if(x>0)
{
north = grid[x-1][y];
}
//South piece
if(x<(SIZE-1))
{
south = grid[x+1][y];
}
//East piece
if(y<(SIZE-1))
{
east = grid[x][y+1];
}
//West piece
if(y>0)
{
west = grid[x][y-1];
}
int[] moves = {north,south,east,west};
return moves;
}
/**
* Finds the 'open' space in the board
* @return the coordinate of the open space via array containing {x,y}
*/
private int[] findSpace()
{
//Array which will hold coordinate
int[] coordinate = new int[2];
//Loop through and look for space
for(int i=0;i<SIZE;i++)
{
for(int j=0;j<SIZE;j++)
{
//Return coordinates of space
if(grid[i][j]==0)
{
coordinate[0]=i;
coordinate[1]=j;
return coordinate;
}
}
}
return coordinate; //Should never happen
}
/**
* Checks to see if provided input is a valid move
* @return True if input is in movable
*/
public boolean checkMove(int input)
{
for(int i = 0; i< movable.length; i++)
{
if(movable[i] == input)
{
return true;
}
}
return false;
}
/**
* Move input to adjacent blank space
* Update hVal and Moveable list after swapped
*/
public void move(int input)
{
int x0=0;
int y0=0;
int xInput=0;
int yInput=0;
//Find the coordinates of 0 and provided input
for(int i=0;i<SIZE;i++)
{
for(int j=0;j<SIZE;j++)
{
if(grid[i][j]==0) {x0=i; y0=j;}
if(grid[i][j]==input){xInput=i; yInput=j;}
}
}
//Swap 0 with the provided input
grid[x0][y0] = grid[xInput][yInput];
grid[xInput][yInput]=0;
movable =availableMoves();
hVal = heuristicCalc();
}
/**
* Print the board
*/
public void printBoard()
{
int count =0;
int x0=-1;
int y0=-1;
for(int i=0;i<SIZE;i++)
{
for(int j=0;j<SIZE;j++)
{
if(grid[i][j]==0)
{
x0=i;
y0=j;
System.out.print(" " + " ");
}
else
{
System.out.print(grid[i][j]+ " ");
}
}
System.out.println("");
}
// System.out.println("SPACE IS AT: {"+x0+","+y0+"}");
}
/**
* Print pieces available to move
*/
public void printMoveable()
{
System.out.print("Piece to move: ");
for(int i = 0; i< movable.length; i++)
{
if(movable[i]!=-1)
{
System.out.print(movable[i] + " ");
}
}
System.out.println("");
}
/**
* Print heuristic value of board
*/
public void printHeuristic()
{
System.out.println("Heuristic Value: " + hVal);
}
/**
* Creates a copy of the specified board
*/
public void copyBoard(Board b)
{
grid = copyGrid(b);
hVal = b.gethVal();
movable = copyMovable(b);
}
public int[] copyMovable(Board b)
{
int[] toCopy = b.getMovable();
int[] newCopy = new int[4];
for(int i=0;i<4;i++)
{
newCopy[i] = toCopy[i];
}
return newCopy;
}
public int[][] copyGrid(Board b)
{
int[][] toCopy = b.grid;
int [][] newCopy = new int[SIZE][SIZE];
for(int i=0;i<SIZE;i++)
{
for(int j=0;j<SIZE;j++)
{
newCopy[i][j] = toCopy[i][j];
}
}
return newCopy;
}
} //End class