-
Notifications
You must be signed in to change notification settings - Fork 0
/
PuzzleSolver.java
69 lines (56 loc) · 2.5 KB
/
PuzzleSolver.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
package com.bogdan;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class PuzzleSolver {
private static List<Piece> pieces = Arrays.asList(new Piece[]{
new Piece(Arrays.asList(new Coord(0,0,0), new Coord(1,0,0), new Coord(2,0,0), new Coord(1,1,0))),
new Piece(Arrays.asList(new Coord(0,0,0), new Coord(0,1,0), new Coord(0,2,0), new Coord(1,1,0))),
new Piece(Arrays.asList(new Coord(-1,1,0), new Coord(0,1,0), new Coord(1,1,0), new Coord(0,0,0))),
new Piece(Arrays.asList(new Coord(1,-1,0), new Coord(1,0,0), new Coord(1,1,0), new Coord(0,0,0))),
new Piece(Arrays.asList(new Coord(-1,0,1), new Coord(0,0,1), new Coord(1,0,1), new Coord(0,0,0))),
new Piece(Arrays.asList(new Coord(0,-1,1), new Coord(0,0,1), new Coord(0,1,1), new Coord(0,0,0))),
new Piece(Arrays.asList(new Coord(0,0,0), new Coord(0,0,1), new Coord(0,0,2), new Coord(1,0,1))),
new Piece(Arrays.asList(new Coord(0,0,0), new Coord(0,0,1), new Coord(0,0,2), new Coord(0,1,1))),
new Piece(Arrays.asList(new Coord(1,0,0), new Coord(1,0,1), new Coord(1,0,2), new Coord(0,0,1))),
new Piece(Arrays.asList(new Coord(0,1,0), new Coord(0,1,1), new Coord(0,1,2), new Coord(0,0,1)))
});
public static void main(String[] args) {
MapRenderer mapRenderer = new MapRenderer();
Map emptyMap = new Map();
List<Map> explortionFrontier = new ArrayList<>();
explortionFrontier.add(emptyMap);
Set<Integer> exploredMapsHashesSet = new HashSet<>();
exploredMapsHashesSet.add(emptyMap.hashCodeSimetric());
while(!explortionFrontier.isEmpty()) {
Map map = explortionFrontier.remove(explortionFrontier.size() - 1);
Optional<Coord> firstEmptyPositionOpt = map.findEmptyPosition();
if (firstEmptyPositionOpt.isEmpty()) {
continue;
}
for (Piece piece : pieces) {
List<Piece> piecesToFill = piece.getAllAbsolutePlacementsToFill(map, firstEmptyPositionOpt.get());
//Collections.shuffle(placementsToFit);
for (Piece pieceToFill : piecesToFill) {
Map newMap = map.duplicate();
newMap.placePieces(pieceToFill);
int hash = newMap.hashCodeSimetric();
if (exploredMapsHashesSet.contains(hash)) {
// go on.
} else if (newMap.isDone()) {
mapRenderer.render(newMap);
System.out.println(newMap);
return;
} else if (newMap.isDoable(pieces)) {
mapRenderer.render(newMap);
exploredMapsHashesSet.add(hash);
explortionFrontier.add(newMap);
}
}
}
}
}
}