-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.zig
328 lines (296 loc) · 9.51 KB
/
tree.zig
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
const std = @import("std");
const print = std.debug.print;
const expect = std.testing.expect;
const brd = @import("board.zig");
const pos_score = brd.pos_score;
const pos_from_view = brd.pos_from_view;
const soul_from_pos = brd.soul_from_pos;
const util = @import("util.zig");
const max2 = util.max2;
const min2 = util.min2;
const UNRESOLVED: i8 = brd.UNRESOLVED;
pub const SoulTable = struct {
souls: [19683]i8 = [_]i8{UNRESOLVED} ** 19683,
num_entries: u16 = 0,
pub fn get_score_from_pos(self: *const SoulTable, pos: *const [9]i8) i8 {
return self.souls[soul_from_pos(pos)];
}
pub fn get_score(self: *const SoulTable, key: u16) i8 {
return self.souls[key];
}
pub fn set_score_from_pos(self: *SoulTable, pos: *const [9]i8, val: i8) i8 {
return self.set_score(soul_from_pos(pos), val);
}
pub fn set_score(self: *SoulTable, key: u16, val: i8) i8 {
var extant: i8 = self.souls[key];
if (extant == UNRESOLVED) {
self.num_entries += 1;
self.souls[key] = val;
return val;
}
if (extant != val) {
print("CANNOT OVERWRITE key {d} val {d} with new val {d}", .{
key,
extant,
val,
});
}
return extant;
}
pub fn get_count(self: *SoulTable) u16 {
return self.num_entries;
}
};
pub fn minimax(
pos: *const [9]i8,
is_black: bool,
souls: *SoulTable,
) i8 {
var score: i8 = pos_score(pos);
if (score != UNRESOLVED) { // game over
return souls.set_score_from_pos(pos, score);
}
var val: i8 = if (is_black) 99 else -99;
for (0..9) |p| {
if (pos[p] != 0) continue;
var child = pos.*;
child[p] = if (is_black) -1 else 1;
val = if (is_black)
min2(val, minimax(&child, !is_black, souls))
else
max2(val, minimax(&child, !is_black, souls));
}
return souls.set_score_from_pos(pos, val);
}
test "white always wins, terminal leaf souls, hopeless black parent" {
const W = -1;
const WHITE_TO_PLAY = false;
const BLACK_TO_PLAY = true;
const white_diagonal_win = [_]i8{
W, 1, 1,
1, W, 1,
0, W, W,
};
const white_diagonal_soul = soul_from_pos(&white_diagonal_win);
const white_diagonal_score = pos_score(&white_diagonal_win);
// test score
// negative for W win,
// with 0 + 1 remaining
try expect(white_diagonal_score == -2);
var souls: SoulTable = SoulTable{};
// minimax returns parent/root score,
// is that what we expect?
try expect(-2 == minimax(&white_diagonal_win, WHITE_TO_PLAY, &souls));
try expect(souls.get_count() == 1);
// sibling, another way for white to win
const white_bottom_row_win = [_]i8{
W, 1, 1,
1, 0, 1,
W, W, W,
};
const white_bottom_row_soul = soul_from_pos(&white_bottom_row_win);
const white_bottom_row_score = pos_score(&white_bottom_row_win);
try expect(white_bottom_row_score == -2);
try expect(-2 == minimax(&white_bottom_row_win, WHITE_TO_PLAY, &souls));
try expect(souls.get_count() == 2);
// let's test integrity of both previous states
try expect(white_diagonal_score == souls.get_score(white_diagonal_soul));
try expect(white_bottom_row_score == souls.get_score(white_bottom_row_soul));
try expect(UNRESOLVED == souls.get_score(123));
// going up (backwards), let's consider parent
const black_parent = [_]i8{
W, 1, 1,
1, 0, 1,
0, W, W,
};
try expect(pos_score(&black_parent) == UNRESOLVED);
try expect(-2 == minimax(&black_parent, BLACK_TO_PLAY, &souls));
try expect(souls.get_count() == 3);
}
test "white always wins, start from hopeless black parent" {
const W = -1;
const BLACK_TO_PLAY = true;
var souls: SoulTable = SoulTable{};
const white_diagonal_win = [_]i8{
W, 1, 1,
1, W, 1,
0, W, W,
};
const white_diagonal_soul = soul_from_pos(&white_diagonal_win);
const white_diagonal_score = pos_score(&white_diagonal_win);
// test score
// negative for W win,
// with 0 + 1 remaining
try expect(white_diagonal_score == -2);
// sibling, another way for white to win
const white_bottom_row_win = [_]i8{
W, 1, 1,
1, 0, 1,
W, W, W,
};
const white_bottom_row_soul = soul_from_pos(&white_bottom_row_win);
const white_bottom_row_score = pos_score(&white_bottom_row_win);
try expect(white_bottom_row_score == -2);
// prove that souls knows nothing
try expect(UNRESOLVED == souls.get_score(white_diagonal_soul));
try expect(UNRESOLVED == souls.get_score(white_bottom_row_soul));
try expect(UNRESOLVED == souls.get_score(123));
// going up (backwards), let's consider parent
const black_parent = [_]i8{
W, 1, 1,
1, 0, 1,
0, W, W,
};
try expect(pos_score(&black_parent) == UNRESOLVED);
try expect(-2 == minimax(&black_parent, BLACK_TO_PLAY, &souls));
try expect(souls.get_count() == 3);
}
test "unbalanced tree, two and three levels from white" {
const W = -1;
const WHITE_TO_PLAY = false;
const BLACK_TO_PLAY = true;
var souls: SoulTable = SoulTable{};
const white_diagonal_win = [_]i8{
W, 0, 1,
1, W, 1,
1, W, W,
};
const white_diagonal_score = pos_score(&white_diagonal_win);
try expect(white_diagonal_score == -2);
try expect(-2 == minimax(&white_diagonal_win, WHITE_TO_PLAY, &souls));
// black nephew wins by row and diagonal
const black_nephew_double_win = [_]i8{
W, W, 1,
1, 1, 1,
1, W, W,
};
const black_nephew_double_score = pos_score(&black_nephew_double_win);
// no empty space but still a positive win for black
try expect(black_nephew_double_score == 1);
try expect(1 == minimax(&black_nephew_double_win, BLACK_TO_PLAY, &souls));
// going up (backwards), let's consider parent
const black_parent = [_]i8{
W, 1, 1,
1, 0, 1,
0, W, W,
};
try expect(pos_score(&black_parent) == UNRESOLVED);
try expect(-2 == minimax(&black_parent, BLACK_TO_PLAY, &souls));
try expect(souls.get_count() == 5);
}
test "full minimax from empty board" {
const W: i8 = -1;
var souls: SoulTable = SoulTable{};
var empty_board = pos_from_view(0);
var resMinimax = minimax(&empty_board, false, &souls);
try expect(resMinimax == 0);
try expect(souls.get_score(0) == souls.get_score(1)); // top left
try expect(souls.get_score(3) == souls.get_score(81)); // top == middle
try expect(souls.get_score_from_pos(&[9]i8{
W, 1, 0,
W, 1, 0,
0, 0, 0,
}) == 5);
try expect(souls.get_score_from_pos(&[9]i8{
W, 1, 0,
W, 1, 0,
0, 1, 0,
}) == 5);
try expect(souls.get_score_from_pos(&[9]i8{
W, 1, 0,
W, 1, 0,
1, 0, 0,
}) == 3);
try expect(souls.get_score_from_pos(&[9]i8{
W, 1, 0,
W, 1, 0,
1, W, 0,
}) == 3);
try expect(souls.get_score_from_pos(&[9]i8{
W, 1, 1,
W, 1, 0,
1, W, 0,
}) == 3);
try expect(souls.get_score_from_pos(&[9]i8{
W, 1, 1,
W, 1, 0,
1, W, 0,
}) == 3);
try expect(souls.get_score_from_pos(&[9]i8{
W, 1, 1,
W, 1, W,
1, W, 0,
}) == UNRESOLVED);
try expect(souls.get_score_from_pos(&[9]i8{
W, 1, 0,
W, 1, 1,
1, W, 0,
}) == 0);
try expect(souls.get_count() >= 765);
}
test "four levels from the top, white to play" {
const W = -1;
const WHITE_TO_PLAY = false;
var souls: SoulTable = SoulTable{};
const white_to_play = [_]i8{
W, 0, 1,
1, 0, 1,
0, W, W,
};
try expect(pos_score(&white_to_play) == UNRESOLVED);
try expect(minimax(&white_to_play, WHITE_TO_PLAY, &souls) == 3);
try expect(souls.get_score(4153) == 3);
try expect(souls.get_score(4180) == -2);
try expect(souls.get_score(4234) == 3);
try expect(souls.get_score(8314) == -2);
//
// WHITE (4153+3)
// / | \
// / | \
// BLACK (4180-2) (4234+3) (8314-2)
// / \ / \
// / \ / \
// WHITE (4342-2) (10502-2) (10768+1) (8476-2)
// /
// /
// BLACK (10849+1)
//
try expect(souls.get_score(4342) == -2);
try expect(souls.get_score(10502) == -2);
try expect(souls.get_score(10768) == 1);
try expect(souls.get_score(8476) == -2);
try expect(souls.get_score(10849) == 1);
try expect(souls.get_count() >= 9);
}
test "white lead to draw to win early" {
const W: i8 = -1;
var souls: SoulTable = SoulTable{};
var empty_board = pos_from_view(0);
try expect(0 == minimax(&empty_board, false, &souls));
try expect(souls.get_score_from_pos(&[9]i8{
1, 0, 0,
1, W, 0,
0, 0, 0,
}) == 0);
try expect(souls.get_score_from_pos(&[9]i8{
1, 0, 0,
1, W, 0,
W, 0, 0,
}) == 0);
try expect(souls.get_score_from_pos(&[9]i8{
1, 0, 1,
1, W, 0,
W, 0, 0,
}) == 0);
try expect(souls.get_score_from_pos(&[9]i8{
1, W, 1,
1, W, 0,
W, 0, 0,
}) == 0);
try expect(souls.get_score_from_pos(&[9]i8{
1, W, 1,
1, W, 0,
W, 1, 0,
}) == 0);
try expect(souls.get_count() >= 765);
}