-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
383 lines (321 loc) · 9.86 KB
/
Board.java
File metadata and controls
383 lines (321 loc) · 9.86 KB
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
package ida.ipl;
import java.io.Serializable;
import java.io.FileReader;
import java.io.IOException;
/**
* Class representing a particular position of the 15 puzzle.
*/
public final class Board implements Serializable {
private static final long serialVersionUID = 5914825388221307541L;
private static final class Position {
int x;
int y;
}
static final int NSQRT = 4; // the 15 puzzle is 4 x 4
static final int NPUZZLE = NSQRT * NSQRT - 1;
// property of the sliding tile puzzle
static final int BRANCH_FACTOR = 4;
// positions of all the tiles in the goal position
private static Position[] goal = new Position[NPUZZLE + 1];
// static initializer of goal
static {
for (int i = 0; i <= NPUZZLE; i++) {
goal[i] = new Position();
}
int v = 0;
for (int y = 0; y < NSQRT; y++) {
for (int x = 0; x < NSQRT; x++) {
goal[v].x = x;
goal[v].y = y;
v++;
}
}
}
/**
* array with one element for each position on the board. element (x,y) on
* the board is (NSQRT * y) + x in this array ideally this would be
* byte[NSQRT][NSQRT], but this makes creating a new board with the copy
* constructor (which we do _a_lot_) too expensive.
*/
private byte[] board;
private int distance;
private int bound;
private int blankX, blankY;
private int prevDx, prevDy;
private int depth;
/**
* create a board by deterministically shuffeling the puzzle a given number
* of times.
*/
public Board(int length) {
board = new byte[NSQRT * NSQRT];
// Generate a starting position by shuffling the blanc around
// in cycles. Just cycling along the outer bounds of the
// board yields low quality start positions whose solutions
// require less than 'length' steps. Therefore we use two
// alternating cycling dimensions.
// start with "solution"
for (int i = 0; i < board.length; i++) {
board[i] = (byte) i;
}
blankX = 0;
blankY = 0;
// size of cylce. alternates between NSQRT and (NSQRT - 1)
int n = NSQRT - 1;
for (int i = 0; i < length; i++) {
// direction we should go
int dx;
int dy;
if (blankX == 0 && blankY == 0) {
// at starting position, change cycle dimension
if (n == NSQRT) {
n = NSQRT - 1;
} else {
n = NSQRT;
}
}
if (blankX == 0 && blankY < n - 1) { // going down
dx = 0;
dy = 1;
} else if (blankY == n - 1 && blankX < n - 1) { // going to the
// right
dx = 1;
dy = 0;
} else if (blankX == n - 1 && blankY > 0) { // going up
dx = 0;
dy = -1;
} else if (blankY == 0 && blankX > 0) { // going left
dx = -1;
dy = 0;
} else {
throw new Error("not going in any direction");
}
move(dx, dy);
}
// reset values changed by calls to move()
bound = 0;
prevDx = 0;
prevDy = 0;
depth = 0;
distance = calculateBoardDistance();
}
/**
* Create a new board. Read initial board position form a file. File should
* contain one character per position, denoting the value of each position
* as a hexidecimal number.
*
* @throws Exception
* @throws IOException
*/
public Board(String fileName) throws Exception {
board = new byte[NSQRT * NSQRT];
bound = 0;
prevDx = 0;
prevDy = 0;
depth = 0;
FileReader fileReader = new FileReader(fileName);
for (int i = 0; i < board.length; i++) {
char c;
do {
int value = fileReader.read();
if (value == -1) {
throw new Exception("unexpected end of stream while "
+ "reading characters");
}
c = (char) value;
} while (Character.isWhitespace(c));
if (c == '.') {
c = '0';
}
int digit = Character.digit(c, 16);
if (digit == 0) {
blankX = i % NSQRT;
blankY = i / NSQRT;
}
board[i] = (byte) digit;
}
distance = calculateBoardDistance();
}
public void init(Board original) {
System.arraycopy(original.board, 0, board, 0, NSQRT * NSQRT);
distance = original.distance;
bound = original.bound;
blankX = original.blankX;
blankY = original.blankY;
prevDx = original.prevDx;
prevDy = original.prevDy;
depth = original.depth;
}
/**
* Copy constructor
*/
public Board(Board original) {
board = new byte[NSQRT * NSQRT];
init(original);
}
/**
* get value of tile at given position
*/
private byte getBoardValue(int x, int y) {
return board[(NSQRT * y) + x];
}
/**
* set value of tile at given position
*/
private void setBoardValue(byte v, int x, int y) {
board[(NSQRT * y) + x] = v;
}
/**
* Manhattan distance of the given coordinate to the goal position of the
* given value.
*/
private int tileDistance(int v, int x, int y) {
if (v == 0) {
// blank always in the right position
return 0;
}
return Math.abs(goal[v].x - x) + Math.abs(goal[v].y - y);
}
/**
* Calculates the total distance of all elements of this puzzle to the goal.
*
*/
private int calculateBoardDistance() {
int result = 0;
for (int y = 0; y < NSQRT; y++) {
for (int x = 0; x < NSQRT; x++) {
result += tileDistance(getBoardValue(x, y), x, y);
}
}
return result;
}
/**
* Moves the blank in the given direction. Also updates bound, distance and
* depth.
*/
private void move(int dx, int dy) {
int x = blankX + dx;
int y = blankY + dy;
byte v = getBoardValue(x, y);
bound--;
distance += -tileDistance(v, x, y) + tileDistance(v, blankX, blankY);
depth++;
setBoardValue((byte) 0, x, y);
setBoardValue(v, blankX, blankY);
prevDx = dx;
prevDy = dy;
blankX = x;
blankY = y;
}
/**
* Make all possible moves with this board position. As an optimization,
* does not "undo" the move which created this board. Elements in the
* returned array may be "null".
*/
public Board[] makeMoves() {
Board[] result = new Board[BRANCH_FACTOR];
int n = 0;
if (blankX > 0 && prevDx != 1) {
result[n] = new Board(this);
result[n].move(-1, 0);
n++;
}
if (blankX < (NSQRT - 1) && prevDx != -1) {
result[n] = new Board(this);
result[n].move(1, 0);
n++;
}
if (blankY > 0 && prevDy != 1) {
result[n] = new Board(this);
result[n].move(0, -1);
n++;
}
if (blankY < (NSQRT - 1) && prevDy != -1) {
result[n] = new Board(this);
result[n].move(0, 1);
n++;
}
return result;
}
/**
* Make all possible moves with this board position. As an optimization,
* does not "undo" the move which created this board. Elements in the
* returned array may be "null".
*/
public Board[] makeMoves(BoardCache cache) {
Board[] result = new Board[BRANCH_FACTOR];
int n = 0;
if (blankX > 0 && prevDx != 1) {
// result[n] = new Board(this);
result[n] = cache.get(this);
result[n].move(-1, 0);
n++;
}
if (blankX < (NSQRT - 1) && prevDx != -1) {
// result[n] = new Board(this);
result[n] = cache.get(this);
result[n].move(1, 0);
n++;
}
if (blankY > 0 && prevDy != 1) {
// result[n] = new Board(this);
result[n] = cache.get(this);
result[n].move(0, -1);
n++;
}
if (blankY < (NSQRT - 1) && prevDy != -1) {
// result[n] = new Board(this);
result[n] = cache.get(this);
result[n].move(0, 1);
n++;
}
return result;
}
/**
* manhattan distance of this board to the solution of the 15 puzzle
*/
public int distance() {
return distance;
}
/**
* Returns the depth of this board. A board created my making a move with a
* board of depth N has a depth of N+1.
*/
public int depth() {
return depth;
}
/**
* returns the bound of this board.
*/
public int bound() {
return bound;
}
/**
* sets the bound of this board.
*/
public void setBound(int bound) {
if (depth != 0) {
System.err.println("warning: setting bound only makes sense at"
+ "the initial job");
}
this.bound = bound;
}
/**
* returns string representing board position.
*/
public String toString() {
String result = "";
for (int y = 0; y < NSQRT; y++) {
for (int x = 0; x < NSQRT; x++) {
byte value = getBoardValue(x, y);
if (value == 0) {
result += ".";
} else {
result += Character.forDigit(value, 16);
}
}
result += "\n";
}
return result;
}
}