Repository navigation
Expand file tree
/
Copy pathModeTest.java
More file actions
106 lines (101 loc) · 2.65 KB
/
Copy pathModeTest.java
File metadata and controls
106 lines (101 loc) · 2.65 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
/**
* Warp searches the true hidden cards. Legal player searches a shuffled copy.
*/
public class ModeTest {
public static void main(String[] args) {
Klondike deal = Klondike.deal(9);
WarpMode.choose(deal);
if (WideBound.lastRoot != deal) {
throw new AssertionError("Warp must search the real deal");
}
Klondike legalDeal = Klondike.deal(9);
new StrongAI(9L).choose(legalDeal);
if (WideBound.lastRoot == legalDeal) {
throw new AssertionError("Legal player must not search the real hidden layout");
}
if (sameHidden(legalDeal, WideBound.lastRoot)) {
throw new AssertionError("Legal sample matched the true hidden cards");
}
boolean differed = false;
for (int seed = 1; seed <= 12 && !differed; seed++) {
Klondike game = Klondike.deal(seed);
java.util.HashSet<Long> seen = new java.util.HashSet<Long>();
for (int step = 0; step < 80 && !differed; step++) {
if (game.won() || !seen.add(game.hash())) {
break;
}
Klondike.Move blind = SmartAI.heuristic(game);
Klondike.Move warp = WarpMode.choose(game);
if (!same(blind, warp)) {
Klondike swapped = game.copy();
if (swapHidden(swapped)) {
Klondike.Move after = WarpMode.choose(swapped);
if (!same(warp, after)) {
differed = true;
}
}
}
if (blind == null) {
break;
}
game.apply(blind);
}
}
if (!differed) {
throw new AssertionError("Warp ignored a change to hidden cards");
}
System.out.println("ModeTest passed");
}
private static boolean swapHidden(Klondike g) {
int firstPile = -1;
int firstIndex = -1;
for (int c = 0; c < 7; c++) {
if (g.dn[c] > 0) {
firstPile = c;
firstIndex = 0;
break;
}
}
if (firstPile < 0) {
return false;
}
for (int c = 0; c < 7; c++) {
for (int i = 0; i < g.dn[c]; i++) {
if (c == firstPile && i == firstIndex) {
continue;
}
int tmp = g.pile[firstPile][firstIndex];
g.pile[firstPile][firstIndex] = g.pile[c][i];
g.pile[c][i] = tmp;
return true;
}
}
return false;
}
private static boolean sameHidden(Klondike a, Klondike b) {
if (a.faceDown() != b.faceDown() || a.stockKnown != b.stockKnown) {
return false;
}
for (int c = 0; c < 7; c++) {
for (int i = 0; i < a.dn[c]; i++) {
if (a.pile[c][i] != b.pile[c][i]) {
return false;
}
}
}
if (!a.stockKnown) {
for (int i = 0; i < a.sn; i++) {
if (a.stock[i] != b.stock[i]) {
return false;
}
}
}
return true;
}
private static boolean same(Klondike.Move a, Klondike.Move b) {
if (a == null || b == null) {
return a == b;
}
return a.kind == b.kind && a.from == b.from && a.index == b.index && a.to == b.to;
}
}