-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
328 lines (279 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
import edu.princeton.cs.algs4.StdRandom;
import java.util.ArrayList;
import java.util.Arrays;
public class Board {
private final int[][] tiles;
private final int dimension;
private final int firstIndexToSwap;
private final int secondIndextoSwap;
/**
* Create a board from n by n array of tiles
*
* @param tiles array of tiles
*/
public Board(int[][] tiles) {
if (tiles == null || !isBoardSymetic(tiles)) throw new IllegalArgumentException();
this.tiles = copyBoardTiles(tiles);
dimension = this.tiles.length;
int[] twinIndicies = getRandomPairOfIndicesInBoard();
firstIndexToSwap = twinIndicies[0];
secondIndextoSwap = twinIndicies[1];
}
private boolean isBoardSymetic(int[][] inputTiles) {
for (int[] row : inputTiles) if (row.length != inputTiles.length) return false;
return true;
}
/**
* Return string representation of current board.
* First line denotes the size of the puzzle.
*
* @return string representation of current board.
*/
public String toString() {
StringBuilder s = new StringBuilder();
s.append(dimension());
s.append("\n");
for (int row = 0; row < dimension(); row++) {
for (int col = 0; col < dimension(); col++) {
s.append(String.format("%2d ", tiles[row][col]));
}
s.append("\n");
}
return s.toString();
}
/**
* Return board dimension.
*
* @return board size
*/
public int dimension() {
return dimension;
}
/**
* Give the distance between the board and the goal board.
* The distance is measured by the number of tiles that are in the wrong position.
*
* @return number of tiles out of place
*/
public int hamming() {
int hamming = 0;
int i = 0;
for (int row = 0; row < dimension(); row++) {
for (int col = 0; col < dimension(); col++) {
i++;
if (i == dimension() * dimension()) i = 0;
if (tiles[row][col] != i && tiles[row][col] != 0) hamming++;
}
}
return hamming;
}
/**
* Calculate Manhattan distance between current tiles and goal tiles
*
* @return sum of Manhattan distances between tiles and goal tiles
*/
public int manhattan() {
int manhattan = 0;
int[] flat = flattenTiles();
for (int i = 0, k = 1; i < flat.length; i++, k++) {
int element = flat[i];
if (element == 0 || element == k) continue;
if (element % dimension() == 0) {
manhattan += Math.abs(i / dimension() - (element / dimension() - 1)) + Math.abs(
i % dimension() - (dimension() - 1));
}
else {
manhattan += Math.abs(i / dimension() - flat[i] / dimension()) + Math.abs(
i % dimension() - Math.abs(flat[i] % dimension() - 1));
}
}
return manhattan;
}
/**
* Check if current board is equal to the goal board.
*
* @return {@code true} if current board is equal to the goal one,
* {@code false} otherwise
*/
public boolean isGoal() {
for (int row = 0, i = 1; row < dimension(); row++) {
for (int col = 0; col < dimension(); col++) {
if (i == dimension() * dimension()) i = 0;
if (this.tiles[row][col] != i++) return false;
}
}
return true;
}
/**
* Check if current board is equal to an argument board.
*
* @param y argument board
* @return {@code true} if the boards are equal, {@code false} otherwise
*/
public boolean equals(Object y) {
if (y == this) return true;
if (y == null) return false;
if (y.getClass() != this.getClass()) return false;
Board that = (Board) y;
return Arrays.deepEquals(this.tiles, that.tiles);
}
/**
* Generate all board's neighbors boards.
*
* @return ArrayList of boards containing all the board's neighbors
*/
public Iterable<Board> neighbors() {
ArrayList<Board> neighbors = new ArrayList<>();
int[] emptyTile = getEmptyTileCoordinates();
if (emptyTile.length == 0) return neighbors;
if (emptyTile[0] < tiles.length - 1) {
neighbors.add(
newNeighborByRow(emptyTile[0], emptyTile[1], emptyTile[0] + 1));
}
if (emptyTile[0] > 0) {
neighbors.add(
newNeighborByRow(emptyTile[0], emptyTile[1], emptyTile[0] - 1));
}
if (emptyTile[1] < tiles.length - 1) {
neighbors.add(
newNeighborByColumn(emptyTile[0], emptyTile[1], emptyTile[1] + 1));
}
if (emptyTile[1] > 0) {
neighbors.add(
newNeighborByColumn(emptyTile[0], emptyTile[1], emptyTile[1] - 1));
}
return neighbors;
}
private int[] getEmptyTileCoordinates() {
int[] result = new int[2];
for (int row = 0; row < tiles.length; row++)
for (int col = 0; col < tiles[row].length; col++)
if (tiles[row][col] == 0) {
result[0] = row;
result[1] = col;
}
return result;
}
private Board newNeighborByRow(int row, int col, int newRow) {
int[][] neighbor = copyBoardTiles();
neighbor[row][col] = neighbor[newRow][col];
neighbor[newRow][col] = 0;
return new Board(neighbor);
}
private Board newNeighborByColumn(int row, int col, int newCol) {
int[][] neighbor = copyBoardTiles();
neighbor[row][col] = neighbor[row][newCol];
neighbor[row][newCol] = 0;
return new Board(neighbor);
}
private int[][] copyBoardTiles() {
int[][] copy = new int[tiles.length][];
for (int i = 0; i < tiles.length; i++) {
copy[i] = Arrays.copyOf(tiles[i], tiles[i].length);
}
return copy;
}
private int[][] copyBoardTiles(int[][] tilesToCopy) {
int[][] copy = new int[tilesToCopy.length][];
for (int row = 0; row < tilesToCopy.length; row++) {
copy[row] = Arrays.copyOf(tilesToCopy[row], tilesToCopy[row].length);
}
return copy;
}
/**
* Return a board that is obtained by exchanging any pair of tiles of the original board.
* None of the exchanged tiles is the empty one.
*
* @return board that is obtained by exchanging any pair of tiles
*/
public Board twin() {
int[] board = flattenTiles();
int[] twinBoard = board.clone();
int swap = twinBoard[firstIndexToSwap];
twinBoard[firstIndexToSwap] = twinBoard[secondIndextoSwap];
twinBoard[secondIndextoSwap] = swap;
return new Board(unflattenBoard(twinBoard));
}
private int[] flattenTiles() {
int[] flattenTiles = new int[dimension() * dimension()];
int index = 0;
for (int[] row : tiles)
for (int item : row)
flattenTiles[index++] = item;
return flattenTiles;
}
private int[] getRandomPairOfIndicesInBoard() {
int[] result = new int[2];
result[0] = getRandomBoardIndex();
do {
result[1] = getRandomBoardIndex();
}
while (result[0] == result[1]);
return result;
}
private int getRandomBoardIndex() {
int i;
int[] board = flattenTiles();
do {
i = StdRandom.uniformInt(0, board.length);
} while (board[i] == 0);
return i;
}
private int[][] unflattenBoard(int[] inputBoard) {
int tilesSize = (int) Math.round(Math.sqrt(inputBoard.length));
int[][] result = new int[tilesSize][tilesSize];
int i = 0;
for (int row = 0; row < tilesSize; row++)
for (int col = 0; col < tilesSize; col++)
result[row][col] = inputBoard[i++];
return result;
}
public static void main(String[] args) {
// test values for tiles
// int[] a = { 0, 1, 3 };
// int[] b = { 4, 2, 5 };
// int[] c = { 7, 8, 6 };
// test values for tiles
int[] a = { 5, 8, 7 };
int[] b = { 1, 4, 6 };
int[] c = { 3, 0, 2 };
// test values for tiles
// int[] a = { 1, 5, 3, 8 };
// int[] b = { 4, 2, 0, 9 };
// int[] c = { 7, 15, 6, 10 };
// int[] d = { 11, 12, 13, 14 };
// test values for tiles
// int[] a = { 8, 1, 3 };
// int[] b = { 4, 0, 2 };
// int[] c = { 7, 6, 5 };
// goal tiles
int[] ga = { 1, 2, 3 };
int[] gb = { 4, 5, 6 };
int[] gc = { 7, 8, 0 };
int[][] tiles = { a, b, c };
int[][] goal = { ga, gb, gc };
Board board = new Board(tiles);
Board goalBoard = new Board(goal);
System.out.println("Original board");
System.out.println(board.toString());
Board twin = board.twin();
System.out.println("Twin board");
System.out.println(twin.toString());
System.out.println("Hamming distance in the original board is: " + board.hamming());
System.out.println();
System.out.println("Manhattan distance in the original board is: " + board.manhattan());
System.out.println();
System.out.println("Hamming distance in the twin board is: " + twin.hamming());
System.out.println("Manhattan distance in the twin board is: " + twin.manhattan());
System.out.println("Neighbors of the original board are:");
for (Board neighbor : board.neighbors()) {
System.out.println(neighbor.toString());
System.out.println("---------");
}
Board secondTwin = board.twin();
System.out.println("Test if second twin board is the same as the first one:");
System.out.println(secondTwin.toString());
System.out.println("Is puzzle solved: " + board.isGoal());
System.out.println("Are both boards equal: " + board.equals(goalBoard));
}
}