-
Notifications
You must be signed in to change notification settings - Fork 0
/
#Puzzle.java#
389 lines (352 loc) · 15 KB
/
#Puzzle.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
package pentominoes;
import java.util.*;
public class Puzzle implements Comparable<Puzzle> {
int[][] grid;
BoardTile[][][] boardTiles;
ArrayList<PentominoShape> availableShapes = new ArrayList<PentominoShape>();
public Puzzle (String[][] strGrid) {
int[][] intGrid = new int[strGrid.length][];
int highestNum = 0;
for (int i = 0; i < strGrid.length; i++) {
intGrid[i] = new int[strGrid[i].length];
for (int j = 0; j < intGrid[i].length; j++) {
String str = strGrid[i][j];
if (str.equals("*")) {
intGrid[i][j] = 0;
} else if (str.equals(".")) {
intGrid[i][j] = 1;
} else {
intGrid[i][j] = Integer.parseInt(str);
}
if (intGrid[i][j] > highestNum) {
highestNum = intGrid[i][j];
}
}
}
grid = intGrid;
boardTiles = new BoardTile[highestNum][grid.length][grid[0].length];
}
public static ArrayList<Puzzle> findSolution(Puzzle p){
int layer = p.getNumLayers();
if (layer == 0) {
return new ArrayList<Puzzle>();
}
Coordinate target = null;
for(int x = 0; x < p.getGrid().length && target == null; x++){
for(int y = 0; y < p.getGrid()[0].length && target == null; y++){
BoardTile tile = p.getBoard()[layer - 1][x][y];
if(tile == null && p.getGrid()[x][y] == layer){
target = new Coordinate(x, y);
}
}
}
if(target == null){
return null;
}
int x = target.getX();
int y = target.getY();
ArrayList<Puzzle> puzzleQueue = new ArrayList<Puzzle>();
//System.out.println(x + ", " + y + " (Target is " + target +")");
for(PentominoShape shape : p.getShapes()){
//System.out.println("Attempt: " + shape + " at " + target + " Layer: " + layer);
for(Pentomino pent : PuzzleSolver.getUniqueForms(shape)){
Puzzle attemptPuzzle = p.getClone();
if(attemptPuzzle.addPiece(pent, x, y, layer, p.getShapes(), target)){
//System.out.println("Added: " + pent.getShape());
puzzleQueue.add(attemptPuzzle);
}
}
}
//sort queue
puzzleQueue.sort(null);
if (puzzleQueue.size() > 0) {
//System.out.println("BEST MOVE: ");
//puzzleQueue.get(0).printBoard();
}
for (Puzzle nextPuzzle : puzzleQueue) {
if(layer != nextPuzzle.getNumLayers()){
ArrayList<Puzzle> solution = new ArrayList<Puzzle>();
solution.add(nextPuzzle);
ArrayList<Puzzle> remainder = findSolution(nextPuzzle.getClone());
if(remainder != null){
solution.addAll(remainder);
return solution;
}
} else {
ArrayList<Puzzle> solution = findSolution(nextPuzzle.getClone());
if(solution != null){
return solution;
}
}
}
//System.out.println("Couldn't fill target square!\n");
return null;
}
@Override
public int compareTo (Puzzle otherPuzzle) {
int layer = getNumLayers();
int thisNum = 0;
int otherNum = 0;
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[x].length; y++) {
if (grid[x][y] == layer) thisNum++;
if (otherPuzzle.getGrid()[x][y] == layer) otherNum++;
}
}
return thisNum - otherNum;
}
public Puzzle(int[][] cloneGrid, BoardTile[][][] cloneTiles, ArrayList<PentominoShape> shapes) {
grid = cloneGrid;
boardTiles = cloneTiles;
availableShapes = shapes;
}
public void setShapes(ArrayList<PentominoShape> shapes){
availableShapes = shapes;
}
public int[][] getGrid() {
int[][] gridClone = new int[grid.length][grid[0].length];
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[0].length; y++) {
gridClone[x][y] = grid[x][y];
}
}
return gridClone;
}
public ArrayList<PentominoShape> getShapes() {
return new ArrayList<PentominoShape>(availableShapes);
}
public Puzzle getClone () {
BoardTile[][][] tiles = new BoardTile[boardTiles.length][boardTiles[0].length][boardTiles[0][0].length];
int[][] gridCopy = new int[grid.length][grid[0].length];
for (int l = 0; l < tiles.length; l++) {
for (int x = 0; x < tiles[0].length; x++) {
for (int y = 0; y < tiles[0][0].length; y++) {
if (boardTiles[l][x][y] != null) {
tiles[l][x][y] = boardTiles[l][x][y].copy();
}
gridCopy[x][y] = grid[x][y];
}
}
}
Puzzle clonePuzzle = new Puzzle(gridCopy, tiles, new ArrayList<PentominoShape>(availableShapes));
return clonePuzzle;
}
public void printBoard () {
for (int l = 0; l < boardTiles.length; l++) {
for (int x = 0; x < boardTiles[l].length; x++) {
for (int y = 0; y < boardTiles[l][x].length; y++) {
if (boardTiles[l][x][y] == null) {
//System.out.print(grid[x][y]);
System.out.print(".");
} else {
System.out.print(boardTiles[l][x][y]);
}
}
System.out.println();
}
System.out.println();
}
}
public boolean finishedAtLayer (int layer) {
//System.out.println("Check if finished:");
//System.out.println(this);
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[x].length; y++) {
if (grid[x][y] == layer) {
return false;
}
}
}
return true;
}
public int getNumLayers () {
int maxValue = grid[0][0];
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[x].length; y++) {
if (grid[x][y] >= maxValue) {
maxValue = grid[x][y];
}
}
}
return maxValue;
}
// return true iff successful
public boolean addPiece (Pentomino pentomino, int x, int y, int layer, ArrayList<PentominoShape> remainingShapes, Coordinate target) {
Coordinate[] blocks = pentomino.determineBlocks(x, y);
boolean violation = isViolation(blocks, layer, remainingShapes, target);
if (!violation) {
for (Coordinate coord : blocks) {
grid[coord.getX()][coord.getY()]--;
boardTiles[layer - 1][coord.getX()][coord.getY()] = new BoardTile(x, y, layer, pentomino.getShape());
}
availableShapes.remove(pentomino.getShape());
return true;
} else {
return false;
}
}
public boolean isViolation (Coordinate[] blocks, int layer, ArrayList<PentominoShape> remainingShapes, Coordinate target) {
boolean foundTarget = false;
for (Coordinate coord : blocks) {
int x = coord.getX();
int y = coord.getY();
if (coord.equals(target)){
foundTarget = true;
}
if (x < 0 || y < 0 || x >= grid.length || y >= grid[0].length) {
//System.out.println("Out of grid!");
return true;
}
if (boardTiles[layer - 1][x][y] != null) {
if (boardTiles[layer - 1][x][y].getPentomino() != null) {
//System.out.println("Space is already occupied!");
return true;
}
}
if (grid[x][y] == 0) {
//System.out.println("Nothing should be placed here! (because this grid space is 0)");
return true;
}
}
if(!foundTarget){
return true;
}
int[][] gridClone = getGrid();
for (Coordinate coord : blocks) {
gridClone[coord.getX()][coord.getY()]--;
}
if (!checkLayer1(gridClone, remainingShapes)) {
//System.out.println("Not enough (or too many) contiguous spaces on layer 1!");
return true;
}
//System.out.println("No violations!");
return false;
}
public boolean outOfBounds (Coordinate coord) {
int x = coord.getX();
int y = coord.getY();
if (x < 0 || y < 0 || x >= grid.length || y >= grid[0].length) {
return true;
}
return false;
}
// return true if layer 1 is still solvable
public boolean checkLayer1 (int[][] testGrid, ArrayList<PentominoShape> remainingShapes) {
ArrayList<Coordinate> validated = new ArrayList<Coordinate>();
for (int x = 0; x < testGrid.length; x++) {
for (int y = 0; y < testGrid[x].length; y++) {
int count = 0;
Coordinate newCoord = new Coordinate(x, y);
if (!validated.contains(newCoord) && testGrid[x][y] != 0) {
ArrayList<Coordinate> queue = new ArrayList<Coordinate>();
ArrayList<Coordinate> visited = new ArrayList<Coordinate>();
queue.add(new Coordinate(x,y));
while (queue.size() > 0) {
count += 1;
Coordinate coord = queue.remove(0);
visited.add(coord);
Coordinate[] neighbours = new Coordinate[]{
new Coordinate(coord.getX() + 1, coord.getY()),
new Coordinate(coord.getX() - 1, coord.getY()),
new Coordinate(coord.getX(), coord.getY() + 1),
new Coordinate(coord.getX(), coord.getY() - 1)
};
for (Coordinate neighbour : neighbours) {
if (!outOfBounds(neighbour) && !visited.contains(neighbour) && !queue.contains(neighbour)) {
if (testGrid[neighbour.getX()][neighbour.getY()] != 0) {
queue.add(neighbour);
}
}
}
}
if (count < 5) {
return false;
}
validated.addAll(visited);
if (count == 5) {
boolean foundShape = false;
for (PentominoShape shape : remainingShapes) {
for (Pentomino pentomino : PuzzleSolver.getUniqueForms(shape)) {
if (Coordinate.compareArrangement(visited, pentomino.getOffsets())) {
foundShape = true;
}
}
}
if (!foundShape) {
//System.out.println("Couldn't find a pentomino to fill the spaces!");
return false;
}
}
}
}
}
return true;
}
// return false if a space cannot be filled
// i.e. if there are less than 5 transitively contiguous neighbour spaces
public boolean checkAllSpaces (int[][] testGrid) {
ArrayList<Coordinate> validated = new ArrayList<Coordinate>();
for (int x = 0; x < testGrid.length; x++) {
for (int y = 0; y < testGrid[x].length; y++) {
int count = 0;
boolean foundEnough = false;
Coordinate newCoord = new Coordinate(x, y);
if (!validated.contains(newCoord) && testGrid[x][y] != 0) {
ArrayList<Coordinate> queue = new ArrayList<Coordinate>();
ArrayList<Coordinate> visited = new ArrayList<Coordinate>();
queue.add(new Coordinate(x,y));
while (queue.size() > 0) {
count += 1;
if (count >= 5) {
//System.out.println("Found 5 ... count = " + count);
validated.addAll(visited);
foundEnough = true;
break;
}
Coordinate coord = queue.remove(0);
//System.out.println(coord + "| " + queue);
//System.out.println(" Visited: " + visited);
visited.add(coord);
Coordinate[] neighbours = new Coordinate[]{
new Coordinate(coord.getX() + 1, coord.getY()),
new Coordinate(coord.getX() - 1, coord.getY()),
new Coordinate(coord.getX(), coord.getY() + 1),
new Coordinate(coord.getX(), coord.getY() - 1)
};
for (Coordinate neighbour : neighbours) {
if (!outOfBounds(neighbour) && !visited.contains(neighbour) && !queue.contains(neighbour)) {
if (testGrid[neighbour.getX()][neighbour.getY()] != 0) {
queue.add(neighbour);
//System.out.println(" Adding: " + neighbour);
}
}
}
}
if (!foundEnough) {
//System.out.println("All spaces checked: FAILED! Bad board below:");
//Puzzle to_print = new Puzzle(testGrid, new BoardTile[0][0]);
//System.out.println(to_print);
return false;
}
}
}
}
//System.out.println("All spaces checked: PASSED! Good board below:");
//Puzzle to_print = new Puzzle(testGrid, new BoardTile[0][0]);
//System.out.println(to_print);
return true;
}
public BoardTile[][][] getBoard() {
return boardTiles;
}
public String toString () {
String returnString = "";
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[x].length; y++) {
returnString += grid[x][y] + " ";
}
returnString += "\n";
}
return returnString;
}
}