Repository navigation
Expand file tree
/
Copy pathLegacyHintAI.java
More file actions
201 lines (185 loc) · 5.1 KB
/
Copy pathLegacyHintAI.java
File metadata and controls
201 lines (185 loc) · 5.1 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
/**
* Faithful port of {@link Game#hint(int)} for headless win-rate measurement.
* Same priorities, same king-slide block, same 75-point failsafe, and the same
* lack of autocomplete. A win is four completed foundations, not merely a
* position {@link Game#winnable()} would offer to finish.
*/
final class LegacyHintAI {
private final boolean[] kingsPlaced = new boolean[4];
private final java.util.HashSet<Long> seen = new java.util.HashSet<Long>();
private int highestScore;
private int steps;
/** When set, keep going past the 75-point failsafe and stop on a repeated position. */
boolean relaxed;
String reason = "";
boolean win(Klondike g) {
kingsPlaced[0] = kingsPlaced[1] = kingsPlaced[2] = kingsPlaced[3] = false;
highestScore = 0;
steps = 0;
seen.clear();
return hint(g);
}
private boolean hint(Klondike g) {
if (steps++ > 8000) {
reason = "limit";
return false;
}
if (g.foundationsComplete() || (relaxed && g.won())) {
reason = "win";
return true;
}
if (relaxed && !seen.add(g.hash())) {
reason = "cycle";
return false;
}
if (g.points > highestScore) {
highestScore = g.points;
}
if (!relaxed && g.points < highestScore - 75) {
reason = "failsafe";
return false;
}
for (int pile = 0; pile < 7; pile++) {
for (int cardIndex = 0; cardIndex < g.pn[pile]; cardIndex++) {
if (cardIndex < g.dn[pile]) {
continue;
}
for (int pile2 = 0; pile2 < 7; pile2++) {
if (tableauToTableau(g, pile, cardIndex, pile2, false, 0)) {
return hint(g);
}
}
}
}
if (g.wn > 0) {
for (int pile = 0; pile < 7; pile++) {
if (tableauToTableau(g, Klondike.WASTE, -1, pile, false, 1)) {
return hint(g);
}
}
}
for (int pile = 0; pile < 7; pile++) {
if (g.pn[pile] == 0) {
continue;
}
if (tableauToFoundation(g, pile)) {
return hint(g);
}
}
for (int pile = 0; pile < 7; pile++) {
for (int cardIndex = 0; cardIndex < g.pn[pile] - 1; cardIndex++) {
if (cardIndex < g.dn[pile]) {
continue;
}
int card = g.pile[pile][cardIndex];
if (!g.canFound(card)) {
continue;
}
for (int pile1 = 0; pile1 < 7; pile1++) {
if (tableauToTableau(g, pile, cardIndex + 1, pile1, true, 0)) {
int where = find(g, card);
if (where >= 0 && g.pn[where] > 0 && g.tail(where) == card && g.canFound(card)) {
g.apply(new Klondike.Move(Klondike.TO_FOUND, where, g.pn[where] - 1, Klondike.suit(card)));
return hint(g);
}
}
}
}
}
if (g.wn > 0 && g.canFound(g.wasteTop())) {
int card = g.wasteTop();
g.apply(new Klondike.Move(Klondike.TO_FOUND, Klondike.WASTE, -1, Klondike.suit(card)));
return hint(g);
}
if (g.wn > 0) {
int drawn = g.wasteTop();
for (int s = 0; s < 4; s++) {
if (g.found[s] == 0) {
continue;
}
int top = s * 13 + g.found[s] - 1;
if (!canSit(drawn, top)) {
continue;
}
boolean movedOff = false;
for (int pile = 0; pile < 7; pile++) {
if (!g.canPlace(top, pile)) {
continue;
}
// The CLI AI peeks an empty foundation if a second pile also fits
// after the card has already been moved. That crashes the deal.
if (movedOff) {
reason = "crash";
return false;
}
g.apply(new Klondike.Move(Klondike.TO_TAB, Klondike.FOUND, s, pile));
movedOff = true;
if (g.wn > 0 && g.canPlace(g.wasteTop(), pile)) {
g.apply(new Klondike.Move(Klondike.TO_TAB, Klondike.WASTE, -1, pile));
return hint(g);
}
}
}
}
if (g.canDraw()) {
g.apply(Klondike.Move.draw());
return hint(g);
}
reason = g.foundationsComplete() ? "win" : "stuck";
return g.foundationsComplete();
}
private boolean tableauToTableau(Klondike g, int from, int index, int to, boolean inner, int drawnKind) {
int card;
if (from == Klondike.WASTE) {
if (g.wn == 0 || !g.canPlace(g.wasteTop(), to)) {
return false;
}
card = g.wasteTop();
} else {
if (index < 0 || index >= g.pn[from] || !g.canPlace(g.pile[from][index], to)) {
return false;
}
card = g.pile[from][index];
}
if (!inner) {
if (g.pn[to] == 0) {
if (kingsPlaced[card % 4]) {
return false;
}
kingsPlaced[card % 4] = true;
} else if (drawnKind == 0 && index > 0 && index - 1 >= g.dn[from]) {
return false;
}
}
if (from == Klondike.WASTE) {
g.apply(new Klondike.Move(Klondike.TO_TAB, Klondike.WASTE, -1, to));
} else {
g.apply(new Klondike.Move(Klondike.TO_TAB, from, index, to));
}
return true;
}
private boolean tableauToFoundation(Klondike g, int pile) {
if (g.pn[pile] == 0) {
return false;
}
int card = g.tail(pile);
if (!g.canFound(card)) {
return false;
}
g.apply(new Klondike.Move(Klondike.TO_FOUND, pile, g.pn[pile] - 1, Klondike.suit(card)));
return true;
}
private static int find(Klondike g, int card) {
for (int c = 0; c < 7; c++) {
for (int i = 0; i < g.pn[c]; i++) {
if (g.pile[c][i] == card) {
return c;
}
}
}
return -1;
}
private static boolean canSit(int card, int onto) {
return Klondike.rank(card) + 1 == Klondike.rank(onto) && Klondike.red(card) != Klondike.red(onto);
}
}