Probdist: Keep row sums for faster picking
[pachi/json.git] / board.c
blob479a098944e3c4e95db2186560b4789b820d2e3a
1 #include <alloca.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include "board.h"
8 #include "debug.h"
9 #include "mq.h"
10 #include "random.h"
12 #ifdef BOARD_SPATHASH
13 #include "patternsp.h"
14 #endif
15 #ifdef BOARD_PAT3
16 #include "pattern3.h"
17 #endif
18 #ifdef BOARD_TRAITS
19 static void board_trait_recompute(struct board *board, coord_t coord);
20 #include "tactics.h"
21 #endif
22 #ifdef BOARD_GAMMA
23 #include "pattern.h"
24 #endif
27 #if 0
28 #define profiling_noinline __attribute__((noinline))
29 #else
30 #define profiling_noinline
31 #endif
33 #define gi_granularity 4
34 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
37 static void
38 board_setup(struct board *b)
40 memset(b, 0, sizeof(*b));
42 struct move m = { pass, S_NONE };
43 b->last_move = b->last_move2 = b->last_ko = b->ko = m;
46 struct board *
47 board_init(void)
49 struct board *b = malloc2(sizeof(struct board));
50 board_setup(b);
52 // Default setup
53 b->size = 9 + 2;
54 board_clear(b);
56 return b;
59 struct board *
60 board_copy(struct board *b2, struct board *b1)
62 memcpy(b2, b1, sizeof(struct board));
64 int bsize = board_size2(b2) * sizeof(*b2->b);
65 int gsize = board_size2(b2) * sizeof(*b2->g);
66 int fsize = board_size2(b2) * sizeof(*b2->f);
67 int nsize = board_size2(b2) * sizeof(*b2->n);
68 int psize = board_size2(b2) * sizeof(*b2->p);
69 int hsize = board_size2(b2) * 2 * sizeof(*b2->h);
70 int gisize = board_size2(b2) * sizeof(*b2->gi);
71 #ifdef WANT_BOARD_C
72 int csize = board_size2(b2) * sizeof(*b2->c);
73 #else
74 int csize = 0;
75 #endif
76 #ifdef BOARD_SPATHASH
77 int ssize = board_size2(b2) * sizeof(*b2->spathash);
78 #else
79 int ssize = 0;
80 #endif
81 #ifdef BOARD_PAT3
82 int p3size = board_size2(b2) * sizeof(*b2->pat3);
83 #else
84 int p3size = 0;
85 #endif
86 #ifdef BOARD_TRAITS
87 int tsize = board_size2(b2) * sizeof(*b2->t);
88 int tqsize = board_size2(b2) * sizeof(*b2->t);
89 #else
90 int tsize = 0;
91 int tqsize = 0;
92 #endif
93 #ifdef BOARD_GAMMA
94 int pbsize = board_size2(b2) * sizeof(*b2->prob[0].items);
95 int rowpbsize = board_size(b2) * sizeof(*b2->prob[0].rowtotals);
96 #else
97 int pbsize = 0;
98 int rowpbsize = 0;
99 #endif
100 int cdsize = board_size2(b2) * sizeof(*b2->coord);
101 void *x = malloc2(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize);
102 memcpy(x, b1->b, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize);
103 b2->b = x; x += bsize;
104 b2->g = x; x += gsize;
105 b2->f = x; x += fsize;
106 b2->p = x; x += psize;
107 b2->n = x; x += nsize;
108 b2->h = x; x += hsize;
109 b2->gi = x; x += gisize;
110 #ifdef WANT_BOARD_C
111 b2->c = x; x += csize;
112 #endif
113 #ifdef BOARD_SPATHASH
114 b2->spathash = x; x += ssize;
115 #endif
116 #ifdef BOARD_PAT3
117 b2->pat3 = x; x += p3size;
118 #endif
119 #ifdef BOARD_TRAITS
120 b2->t = x; x += tsize;
121 b2->tq = x; x += tqsize;
122 #endif
123 #ifdef BOARD_GAMMA
124 b2->prob[0].items = x; x += pbsize;
125 b2->prob[1].items = x; x += pbsize;
126 b2->prob[0].rowtotals = x; x += rowpbsize;
127 b2->prob[1].rowtotals = x; x += rowpbsize;
128 #endif
129 b2->coord = x; x += cdsize;
131 return b2;
134 void
135 board_done_noalloc(struct board *board)
137 if (board->b) free(board->b);
140 void
141 board_done(struct board *board)
143 board_done_noalloc(board);
144 free(board);
147 void
148 board_resize(struct board *board, int size)
150 #ifdef BOARD_SIZE
151 assert(board_size(board) == size + 2);
152 #endif
153 board->size = size + 2 /* S_OFFBOARD margin */;
154 board->size2 = board_size(board) * board_size(board);
156 board->bits2 = 1;
157 while ((1 << board->bits2) < board->size2) board->bits2++;
159 if (board->b)
160 free(board->b);
162 int bsize = board_size2(board) * sizeof(*board->b);
163 int gsize = board_size2(board) * sizeof(*board->g);
164 int fsize = board_size2(board) * sizeof(*board->f);
165 int nsize = board_size2(board) * sizeof(*board->n);
166 int psize = board_size2(board) * sizeof(*board->p);
167 int hsize = board_size2(board) * 2 * sizeof(*board->h);
168 int gisize = board_size2(board) * sizeof(*board->gi);
169 #ifdef WANT_BOARD_C
170 int csize = board_size2(board) * sizeof(*board->c);
171 #else
172 int csize = 0;
173 #endif
174 #ifdef BOARD_SPATHASH
175 int ssize = board_size2(board) * sizeof(*board->spathash);
176 #else
177 int ssize = 0;
178 #endif
179 #ifdef BOARD_PAT3
180 int p3size = board_size2(board) * sizeof(*board->pat3);
181 #else
182 int p3size = 0;
183 #endif
184 #ifdef BOARD_TRAITS
185 int tsize = board_size2(board) * sizeof(*board->t);
186 int tqsize = board_size2(board) * sizeof(*board->t);
187 #else
188 int tsize = 0;
189 int tqsize = 0;
190 #endif
191 #ifdef BOARD_GAMMA
192 int pbsize = board_size2(board) * sizeof(*board->prob[0].items);
193 int rowpbsize = board_size(board) * sizeof(*board->prob[0].rowtotals);
194 #else
195 int pbsize = 0;
196 int rowpbsize = 0;
197 #endif
198 int cdsize = board_size2(board) * sizeof(*board->coord);
199 void *x = malloc2(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize);
200 memset(x, 0, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize);
201 board->b = x; x += bsize;
202 board->g = x; x += gsize;
203 board->f = x; x += fsize;
204 board->p = x; x += psize;
205 board->n = x; x += nsize;
206 board->h = x; x += hsize;
207 board->gi = x; x += gisize;
208 #ifdef WANT_BOARD_C
209 board->c = x; x += csize;
210 #endif
211 #ifdef BOARD_SPATHASH
212 board->spathash = x; x += ssize;
213 #endif
214 #ifdef BOARD_PAT3
215 board->pat3 = x; x += p3size;
216 #endif
217 #ifdef BOARD_TRAITS
218 board->t = x; x += tsize;
219 board->tq = x; x += tqsize;
220 #endif
221 #ifdef BOARD_GAMMA
222 board->prob[0].items = x; x += pbsize;
223 board->prob[1].items = x; x += pbsize;
224 board->prob[0].rowtotals = x; x += rowpbsize;
225 board->prob[1].rowtotals = x; x += rowpbsize;
226 #endif
227 board->coord = x; x += cdsize;
230 void
231 board_clear(struct board *board)
233 int size = board_size(board);
234 float komi = board->komi;
236 board_done_noalloc(board);
237 board_setup(board);
238 board_resize(board, size - 2 /* S_OFFBOARD margin */);
240 board->komi = komi;
242 /* Setup neighborhood iterators */
243 board->nei8[0] = -size - 1; // (-1,-1)
244 board->nei8[1] = 1;
245 board->nei8[2] = 1;
246 board->nei8[3] = size - 2; // (-1,0)
247 board->nei8[4] = 2;
248 board->nei8[5] = size - 2; // (-1,1)
249 board->nei8[6] = 1;
250 board->nei8[7] = 1;
251 board->dnei[0] = -size - 1;
252 board->dnei[1] = 2;
253 board->dnei[2] = size*2 - 2;
254 board->dnei[3] = 2;
256 /* Setup initial symmetry */
257 board->symmetry.d = 1;
258 board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2;
259 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
260 board->symmetry.type = SYM_FULL;
262 /* Set up coordinate cache */
263 foreach_point(board) {
264 board->coord[c][0] = c % board_size(board);
265 board->coord[c][1] = c / board_size(board);
266 } foreach_point_end;
268 /* Draw the offboard margin */
269 int top_row = board_size2(board) - board_size(board);
270 int i;
271 for (i = 0; i < board_size(board); i++)
272 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
273 for (i = 0; i <= top_row; i += board_size(board))
274 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
276 foreach_point(board) {
277 coord_t coord = c;
278 if (board_at(board, coord) == S_OFFBOARD)
279 continue;
280 foreach_neighbor(board, c, {
281 inc_neighbor_count_at(board, coord, board_at(board, c));
282 } );
283 } foreach_point_end;
285 /* All positions are free! Except the margin. */
286 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
287 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
288 board->f[board->flen++] = i;
290 /* Initialize zobrist hashtable. */
291 foreach_point(board) {
292 int max = (sizeof(hash_t) << history_hash_bits);
293 /* fast_random() is 16-bit only */
294 board->h[c * 2] = ((hash_t) fast_random(max))
295 | ((hash_t) fast_random(max) << 16)
296 | ((hash_t) fast_random(max) << 32)
297 | ((hash_t) fast_random(max) << 48);
298 if (!board->h[c * 2])
299 /* Would be kinda "oops". */
300 board->h[c * 2] = 1;
301 /* And once again for white */
302 board->h[c * 2 + 1] = ((hash_t) fast_random(max))
303 | ((hash_t) fast_random(max) << 16)
304 | ((hash_t) fast_random(max) << 32)
305 | ((hash_t) fast_random(max) << 48);
306 if (!board->h[c * 2 + 1])
307 board->h[c * 2 + 1] = 1;
308 } foreach_point_end;
310 #ifdef BOARD_SPATHASH
311 /* Initialize spatial hashes. */
312 foreach_point(board) {
313 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
314 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
315 ptcoords_at(x, y, c, board, j);
316 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
317 pthashes[0][j][board_at(board, c)];
318 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
319 pthashes[0][j][stone_other(board_at(board, c))];
322 } foreach_point_end;
323 #endif
324 #ifdef BOARD_PAT3
325 /* Initialize 3x3 pattern codes. */
326 foreach_point(board) {
327 if (board_at(board, c) == S_NONE)
328 board->pat3[c] = pattern3_hash(board, c);
329 } foreach_point_end;
330 #endif
331 #ifdef BOARD_TRAITS
332 /* Initialize traits. */
333 foreach_point(board) {
334 trait_at(board, c, S_BLACK).cap = 0;
335 trait_at(board, c, S_BLACK).safe = true;
336 trait_at(board, c, S_WHITE).cap = 0;
337 trait_at(board, c, S_WHITE).safe = true;
338 } foreach_point_end;
339 #endif
340 #ifdef BOARD_GAMMA
341 board->prob[0].n = board->prob[1].n = board_size2(board);
342 board->prob[0].n1 = board->prob[1].n1 = board_size(board);
343 foreach_point(board) {
344 probdist_set(&board->prob[0], c, (board_at(board, c) == S_NONE) * 1.0f);
345 probdist_set(&board->prob[1], c, (board_at(board, c) == S_NONE) * 1.0f);
346 } foreach_point_end;
347 #endif
350 static char *
351 board_print_top(struct board *board, char *s, char *end, int c)
353 for (int i = 0; i < c; i++) {
354 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
355 s += snprintf(s, end - s, " ");
356 for (int x = 1; x < board_size(board) - 1; x++)
357 s += snprintf(s, end - s, "%c ", asdf[x - 1]);
358 s += snprintf(s, end -s, " ");
360 s += snprintf(s, end - s, "\n");
361 for (int i = 0; i < c; i++) {
362 s += snprintf(s, end - s, " +-");
363 for (int x = 1; x < board_size(board) - 1; x++)
364 s += snprintf(s, end - s, "--");
365 s += snprintf(s, end - s, "+");
367 s += snprintf(s, end - s, "\n");
368 return s;
371 static char *
372 board_print_bottom(struct board *board, char *s, char *end, int c)
374 for (int i = 0; i < c; i++) {
375 s += snprintf(s, end - s, " +-");
376 for (int x = 1; x < board_size(board) - 1; x++)
377 s += snprintf(s, end - s, "--");
378 s += snprintf(s, end - s, "+");
380 s += snprintf(s, end - s, "\n");
381 return s;
384 static char *
385 board_print_row(struct board *board, int y, char *s, char *end, board_cprint cprint)
387 s += snprintf(s, end - s, " %2d | ", y);
388 for (int x = 1; x < board_size(board) - 1; x++) {
389 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
390 s += snprintf(s, end - s, "%c)", stone2char(board_atxy(board, x, y)));
391 else
392 s += snprintf(s, end - s, "%c ", stone2char(board_atxy(board, x, y)));
394 s += snprintf(s, end - s, "|");
395 if (cprint) {
396 s += snprintf(s, end - s, " %2d | ", y);
397 for (int x = 1; x < board_size(board) - 1; x++) {
398 s = cprint(board, coord_xy(board, x, y), s, end);
400 s += snprintf(s, end - s, "|");
402 s += snprintf(s, end - s, "\n");
403 return s;
406 void
407 board_print_custom(struct board *board, FILE *f, board_cprint cprint)
409 char buf[10240];
410 char *s = buf;
411 char *end = buf + sizeof(buf);
412 s += snprintf(s, end - s, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
413 board->moves, board->komi, board->handicap,
414 board->captures[S_BLACK], board->captures[S_WHITE]);
415 s = board_print_top(board, s, end, 1 + !!cprint);
416 for (int y = board_size(board) - 2; y >= 1; y--)
417 s = board_print_row(board, y, s, end, cprint);
418 board_print_bottom(board, s, end, 1 + !!cprint);
419 fprintf(f, "%s\n", buf);
422 static char *
423 cprint_group(struct board *board, coord_t c, char *s, char *end)
425 s += snprintf(s, end - s, "%d ", group_base(group_at(board, c)));
426 return s;
429 void
430 board_print(struct board *board, FILE *f)
432 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL);
435 void
436 board_gamma_set(struct board *b, struct features_gamma *gamma, bool precise_selfatari)
438 #ifdef BOARD_GAMMA
439 b->gamma = gamma;
440 b->precise_selfatari = precise_selfatari;
441 for (int i = 0; i < b->flen; i++) {
442 board_trait_recompute(b, b->f[i]);
444 #endif
448 /* Update the probability distribution we maintain incrementally. */
449 void
450 board_gamma_update(struct board *board, coord_t coord, enum stone color)
452 #ifdef BOARD_GAMMA
453 if (!board->gamma)
454 return;
456 /* Punch out invalid moves and moves filling our own eyes. */
457 if (board_at(board, coord) != S_NONE
458 || (board_is_eyelike(board, coord, stone_other(color))
459 && !trait_at(board, coord, color).cap)
460 || (board_is_one_point_eye(board, coord, color))) {
461 probdist_set(&board->prob[color - 1], coord, 0);
462 return;
465 int pat = board->pat3[coord];
466 if (color == S_WHITE) {
467 /* We work with the pattern3s as black-to-play. */
468 pat = pattern3_reverse(pat);
471 /* We just quickly replicate the general pattern matcher stuff
472 * here in the most bare-bone way. */
473 double value = board->gamma->gamma[FEAT_PATTERN3][pat];
474 if (trait_at(board, coord, color).cap)
475 value *= board->gamma->gamma[FEAT_CAPTURE][0];
476 if (trait_at(board, coord, stone_other(color)).cap
477 && trait_at(board, coord, color).safe)
478 value *= board->gamma->gamma[FEAT_AESCAPE][0];
479 if (!trait_at(board, coord, color).safe)
480 value *= board->gamma->gamma[FEAT_SELFATARI][1 + board->precise_selfatari];
481 probdist_set(&board->prob[color - 1], coord, value);
482 #endif
485 #ifdef BOARD_TRAITS
486 static bool
487 board_trait_safe(struct board *board, coord_t coord, enum stone color)
489 /* sic! */
490 if (board->precise_selfatari)
491 return is_bad_selfatari(board, color, coord);
492 else
493 return board_safe_to_play(board, coord, color);
496 static void
497 board_trait_recompute(struct board *board, coord_t coord)
499 trait_at(board, coord, S_BLACK).safe = board_trait_safe(board, coord, S_BLACK);;
500 trait_at(board, coord, S_WHITE).safe = board_trait_safe(board, coord, S_WHITE);
501 if (DEBUGL(8)) {
502 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d safe=%d) (white cap=%d safe=%d)\n",
503 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
504 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).safe,
505 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).safe);
507 board_gamma_update(board, coord, S_BLACK);
508 board_gamma_update(board, coord, S_WHITE);
510 #endif
512 /* Recompute traits for dirty points that we have previously touched
513 * somehow (libs of their neighbors changed or so). */
514 static void
515 board_traits_recompute(struct board *board)
517 #ifdef BOARD_TRAITS
518 for (int i = 0; i < board->tqlen; i++) {
519 coord_t coord = board->tq[i];
520 if (!trait_at(board, coord, S_BLACK).dirty) continue;
521 if (board_at(board, coord) != S_NONE) continue;
522 board_trait_recompute(board, coord);
523 trait_at(board, coord, S_BLACK).dirty = false;
525 board->tqlen = 0;
526 #endif
529 /* Queue traits of given point for recomputing. */
530 static void
531 board_trait_queue(struct board *board, coord_t coord)
533 #ifdef BOARD_TRAITS
534 board->tq[board->tqlen++] = coord;
535 trait_at(board, coord, S_BLACK).dirty = true;
536 #endif
540 /* Update board hash with given coordinate. */
541 static void profiling_noinline
542 board_hash_update(struct board *board, coord_t coord, enum stone color)
544 board->hash ^= hash_at(board, coord, color);
545 if (DEBUGL(8))
546 fprintf(stderr, "board_hash_update(%d,%d,%d) ^ %"PRIhash" -> %"PRIhash"\n", color, coord_x(coord, board), coord_y(coord, board), hash_at(board, coord, color), board->hash);
548 #ifdef BOARD_SPATHASH
549 /* Gridcular metric is reflective, so we update all hashes
550 * of appropriate ditance in OUR circle. */
551 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
552 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
553 ptcoords_at(x, y, coord, board, j);
554 /* We either changed from S_NONE to color
555 * or vice versa; doesn't matter. */
556 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
557 pthashes[0][j][color] ^ pthashes[0][j][S_NONE];
558 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
559 pthashes[0][j][stone_other(color)] ^ pthashes[0][j][S_NONE];
562 #endif
564 #if defined(BOARD_PAT3)
565 /* @color is not what we need in case of capture. */
566 enum stone new_color = board_at(board, coord);
567 if (new_color == S_NONE)
568 board->pat3[coord] = pattern3_hash(board, coord);
569 foreach_8neighbor(board, coord) { // internally, the loop uses fn__i=[0..7]
570 if (board_at(board, c) != S_NONE)
571 continue;
572 board->pat3[c] &= ~(3 << (fn__i*2));
573 board->pat3[c] |= new_color << (fn__i*2);
574 #if 0
575 if (board_at(board, c) != S_OFFBOARD && pattern3_hash(board, c) != board->pat3[c]) {
576 board_print(board, stderr);
577 fprintf(stderr, "%s->%s %x != %x (%d-%d:%d)\n", coord2sstr(coord, board), coord2sstr(c, board), pattern3_hash(board, c), board->pat3[c], coord, c, fn__i);
578 assert(0);
580 #endif
581 board_gamma_update(board, c, S_BLACK);
582 board_gamma_update(board, c, S_WHITE);
583 } foreach_8neighbor_end;
584 #endif
587 /* Commit current board hash to history. */
588 static void profiling_noinline
589 board_hash_commit(struct board *board)
591 if (DEBUGL(8))
592 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
593 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
594 board->history_hash[board->hash & history_hash_mask] = board->hash;
595 } else {
596 hash_t i = board->hash;
597 while (board->history_hash[i & history_hash_mask]) {
598 if (board->history_hash[i & history_hash_mask] == board->hash) {
599 if (DEBUGL(5))
600 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
601 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
602 board->superko_violation = true;
603 return;
605 i = history_hash_next(i);
607 board->history_hash[i & history_hash_mask] = board->hash;
612 void
613 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
615 if (likely(symmetry->type == SYM_NONE)) {
616 /* Fully degenerated already. We do not support detection
617 * of restoring of symmetry, assuming that this is too rare
618 * a case to handle. */
619 return;
622 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
623 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
624 if (DEBUGL(6)) {
625 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
626 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
627 symmetry->d, symmetry->type, x, y);
630 switch (symmetry->type) {
631 case SYM_FULL:
632 if (x == t && y == t) {
633 /* Tengen keeps full symmetry. */
634 return;
636 /* New symmetry now? */
637 if (x == y) {
638 symmetry->type = SYM_DIAG_UP;
639 symmetry->x1 = symmetry->y1 = 1;
640 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
641 symmetry->d = 1;
642 } else if (dx == y) {
643 symmetry->type = SYM_DIAG_DOWN;
644 symmetry->x1 = symmetry->y1 = 1;
645 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
646 symmetry->d = 1;
647 } else if (x == t) {
648 symmetry->type = SYM_HORIZ;
649 symmetry->y1 = 1;
650 symmetry->y2 = board_size(b) - 1;
651 symmetry->d = 0;
652 } else if (y == t) {
653 symmetry->type = SYM_VERT;
654 symmetry->x1 = 1;
655 symmetry->x2 = board_size(b) - 1;
656 symmetry->d = 0;
657 } else {
658 break_symmetry:
659 symmetry->type = SYM_NONE;
660 symmetry->x1 = symmetry->y1 = 1;
661 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
662 symmetry->d = 0;
664 break;
665 case SYM_DIAG_UP:
666 if (x == y)
667 return;
668 goto break_symmetry;
669 case SYM_DIAG_DOWN:
670 if (dx == y)
671 return;
672 goto break_symmetry;
673 case SYM_HORIZ:
674 if (x == t)
675 return;
676 goto break_symmetry;
677 case SYM_VERT:
678 if (y == t)
679 return;
680 goto break_symmetry;
681 case SYM_NONE:
682 assert(0);
683 break;
686 if (DEBUGL(6)) {
687 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
688 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
689 symmetry->d, symmetry->type);
691 /* Whew. */
695 void
696 board_handicap_stone(struct board *board, int x, int y, FILE *f)
698 struct move m;
699 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
701 board_play(board, &m);
702 /* Simulate white passing; otherwise, UCT search can get confused since
703 * tree depth parity won't match the color to move. */
704 board->moves++;
706 char *str = coord2str(m.coord, board);
707 if (DEBUGL(1))
708 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
709 if (f) fprintf(f, "%s ", str);
710 free(str);
713 void
714 board_handicap(struct board *board, int stones, FILE *f)
716 int margin = 3 + (board_size(board) >= 13);
717 int min = margin;
718 int mid = board_size(board) / 2;
719 int max = board_size(board) - 1 - margin;
720 const int places[][2] = {
721 { min, min }, { max, max }, { max, min }, { min, max },
722 { min, mid }, { max, mid },
723 { mid, min }, { mid, max },
724 { mid, mid },
727 board->handicap = stones;
729 if (stones == 5 || stones == 7) {
730 board_handicap_stone(board, mid, mid, f);
731 stones--;
734 int i;
735 for (i = 0; i < stones; i++)
736 board_handicap_stone(board, places[i][0], places[i][1], f);
740 static void __attribute__((noinline))
741 check_libs_consistency(struct board *board, group_t g)
743 #ifdef DEBUG
744 if (!g) return;
745 struct group *gi = &board_group_info(board, g);
746 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
747 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
748 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
749 assert(0);
751 #endif
754 static void
755 board_capturable_add(struct board *board, group_t group, coord_t lib)
757 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
758 #ifdef BOARD_TRAITS
759 /* Increase capturable count trait of my last lib. */
760 enum stone capturing_color = stone_other(board_at(board, group));
761 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
762 foreach_neighbor(board, lib, {
763 if (DEBUGL(8) && group_at(board, c) == group)
764 fprintf(stderr, "%s[%d] %s cap bump bc of %s(%d) member %s\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap, stone2str(capturing_color), coord2sstr(group, board), board_group_info(board, group).libs, coord2sstr(c, board));
765 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
767 board_trait_queue(board, lib);
768 #endif
770 #ifdef WANT_BOARD_C
771 /* Update the list of capturable groups. */
772 assert(group);
773 assert(board->clen < board_size2(board));
774 board->c[board->clen++] = group;
775 #endif
777 static void
778 board_capturable_rm(struct board *board, group_t group, coord_t lib)
780 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
781 #ifdef BOARD_TRAITS
782 /* Decrease capturable count trait of my previously-last lib. */
783 enum stone capturing_color = stone_other(board_at(board, group));
784 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
785 foreach_neighbor(board, lib, {
786 if (DEBUGL(8) && group_at(board, c) == group)
787 fprintf(stderr, "%s[%d] cap dump bc of %s(%d) member %s\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap, coord2sstr(group, board), board_group_info(board, group).libs, coord2sstr(c, board));
788 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
790 board_trait_queue(board, lib);
791 #endif
793 #ifdef WANT_BOARD_C
794 /* Update the list of capturable groups. */
795 for (int i = 0; i < board->clen; i++) {
796 if (unlikely(board->c[i] == group)) {
797 board->c[i] = board->c[--board->clen];
798 return;
801 fprintf(stderr, "rm of bad group %d\n", group_base(group));
802 assert(0);
803 #endif
806 static void
807 board_atariable_add(struct board *board, group_t group, coord_t lib1, coord_t lib2)
809 #ifdef BOARD_TRAITS
810 board_trait_queue(board, lib1);
811 board_trait_queue(board, lib2);
812 #endif
814 static void
815 board_atariable_rm(struct board *board, group_t group, coord_t lib1, coord_t lib2)
817 #ifdef BOARD_TRAITS
818 board_trait_queue(board, lib1);
819 board_trait_queue(board, lib2);
820 #endif
823 static void
824 board_group_addlib(struct board *board, group_t group, coord_t coord)
826 if (DEBUGL(7)) {
827 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
828 group_base(group), coord2sstr(group_base(group), board),
829 board_group_info(board, group).libs, coord2sstr(coord, board));
832 check_libs_consistency(board, group);
834 struct group *gi = &board_group_info(board, group);
835 if (gi->libs < GROUP_KEEP_LIBS) {
836 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
837 #if 0
838 /* Seems extra branch just slows it down */
839 if (!gi->lib[i])
840 break;
841 #endif
842 if (unlikely(gi->lib[i] == coord))
843 return;
845 if (gi->libs == 0) {
846 board_capturable_add(board, group, coord);
847 } else if (gi->libs == 1) {
848 board_capturable_rm(board, group, gi->lib[0]);
849 board_atariable_add(board, group, gi->lib[0], coord);
850 } else if (gi->libs == 2) {
851 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
853 gi->lib[gi->libs++] = coord;
856 check_libs_consistency(board, group);
859 static void
860 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
862 /* Add extra liberty from the board to our liberty list. */
863 unsigned char watermark[board_size2(board) / 8];
864 memset(watermark, 0, sizeof(watermark));
865 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
866 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
868 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
869 watermark_set(gi->lib[i]);
870 watermark_set(avoid);
872 foreach_in_group(board, group) {
873 coord_t coord2 = c;
874 foreach_neighbor(board, coord2, {
875 if (board_at(board, c) + watermark_get(c) != S_NONE)
876 continue;
877 watermark_set(c);
878 gi->lib[gi->libs++] = c;
879 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
880 return;
881 } );
882 } foreach_in_group_end;
883 #undef watermark_get
884 #undef watermark_set
887 static void
888 board_group_rmlib(struct board *board, group_t group, coord_t coord)
890 if (DEBUGL(7)) {
891 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
892 group_base(group), coord2sstr(group_base(group), board),
893 board_group_info(board, group).libs, coord2sstr(coord, board));
896 struct group *gi = &board_group_info(board, group);
897 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
898 #if 0
899 /* Seems extra branch just slows it down */
900 if (!gi->lib[i])
901 break;
902 #endif
903 if (likely(gi->lib[i] != coord))
904 continue;
906 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
907 gi->lib[gi->libs] = 0;
909 check_libs_consistency(board, group);
911 /* Postpone refilling lib[] until we need to. */
912 assert(GROUP_REFILL_LIBS > 1);
913 if (gi->libs > GROUP_REFILL_LIBS)
914 return;
915 if (gi->libs == GROUP_REFILL_LIBS)
916 board_group_find_extra_libs(board, group, gi, coord);
918 if (gi->libs == 2) {
919 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
920 } else if (gi->libs == 1) {
921 board_capturable_add(board, group, gi->lib[0]);
922 board_atariable_rm(board, group, gi->lib[0], lib);
923 } else if (gi->libs == 0)
924 board_capturable_rm(board, group, lib);
925 return;
928 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
929 * can call this multiple times per coord. */
930 check_libs_consistency(board, group);
931 return;
935 /* This is a low-level routine that doesn't maintain consistency
936 * of all the board data structures. */
937 static void
938 board_remove_stone(struct board *board, group_t group, coord_t c)
940 enum stone color = board_at(board, c);
941 board_at(board, c) = S_NONE;
942 group_at(board, c) = 0;
943 board_hash_update(board, c, color);
944 #ifdef BOARD_TRAITS
945 /* We mark as cannot-capture now. If this is a ko/snapback,
946 * we will get incremented later in board_group_addlib(). */
947 trait_at(board, c, S_BLACK).cap = 0;
948 trait_at(board, c, S_WHITE).cap = 0;
949 board_trait_queue(board, c);
950 #endif
952 /* Increase liberties of surrounding groups */
953 coord_t coord = c;
954 foreach_neighbor(board, coord, {
955 dec_neighbor_count_at(board, c, color);
956 board_trait_queue(board, c);
957 group_t g = group_at(board, c);
958 if (g && g != group)
959 board_group_addlib(board, g, coord);
962 if (DEBUGL(6))
963 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
964 board->f[board->flen++] = c;
967 static int profiling_noinline
968 board_group_capture(struct board *board, group_t group)
970 int stones = 0;
972 foreach_in_group(board, group) {
973 board->captures[stone_other(board_at(board, c))]++;
974 board_remove_stone(board, group, c);
975 stones++;
976 } foreach_in_group_end;
978 struct group *gi = &board_group_info(board, group);
979 if (gi->libs == 2)
980 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
981 else if (gi->libs == 1)
982 board_capturable_rm(board, group, gi->lib[0]);
983 memset(gi, 0, sizeof(*gi));
985 return stones;
989 static void profiling_noinline
990 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
992 group_at(board, coord) = group;
993 groupnext_at(board, coord) = groupnext_at(board, prevstone);
994 groupnext_at(board, prevstone) = coord;
996 #ifdef BOARD_TRAITS
997 if (board_group_info(board, group).libs == 1) {
998 /* Our group is temporarily in atari; make sure the capturable
999 * counts also correspond to the newly added stone before we
1000 * start adding liberties again so bump-dump ops match. */
1001 enum stone capturing_color = stone_other(board_at(board, group));
1002 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1003 coord_t lib = board_group_info(board, group).lib[0];
1004 if (coord_is_adjecent(lib, coord, board)) {
1005 if (DEBUGL(8)) fprintf(stderr, "add_to_group %s: %s[%d] bump\n", coord2sstr(group, board), coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
1006 trait_at(board, lib, capturing_color).cap++;
1007 board_trait_queue(board, lib);
1010 #endif
1012 foreach_neighbor(board, coord, {
1013 if (board_at(board, c) == S_NONE)
1014 board_group_addlib(board, group, c);
1017 if (DEBUGL(8))
1018 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1019 coord_x(prevstone, board), coord_y(prevstone, board),
1020 coord_x(coord, board), coord_y(coord, board),
1021 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
1022 group_base(group));
1025 static void profiling_noinline
1026 merge_groups(struct board *board, group_t group_to, group_t group_from)
1028 if (DEBUGL(7))
1029 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
1030 group_base(group_from), group_base(group_to));
1031 struct group *gi_from = &board_group_info(board, group_from);
1032 struct group *gi_to = &board_group_info(board, group_to);
1034 /* We do this early before the group info is rewritten. */
1035 if (gi_from->libs == 2)
1036 board_atariable_rm(board, group_from, gi_from->lib[0], gi_from->lib[1]);
1037 else if (gi_from->libs == 1)
1038 board_capturable_rm(board, group_from, gi_from->lib[0]);
1040 if (DEBUGL(7))
1041 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
1043 if (gi_to->libs < GROUP_KEEP_LIBS) {
1044 for (int i = 0; i < gi_from->libs; i++) {
1045 for (int j = 0; j < gi_to->libs; j++)
1046 if (gi_to->lib[j] == gi_from->lib[i])
1047 goto next_from_lib;
1048 if (gi_to->libs == 0) {
1049 board_capturable_add(board, group_to, gi_from->lib[i]);
1050 } else if (gi_to->libs == 1) {
1051 board_capturable_rm(board, group_to, gi_to->lib[0]);
1052 board_atariable_add(board, group_to, gi_to->lib[0], gi_from->lib[i]);
1053 } else if (gi_to->libs == 2) {
1054 board_atariable_rm(board, group_to, gi_to->lib[0], gi_to->lib[1]);
1056 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
1057 if (gi_to->libs >= GROUP_KEEP_LIBS)
1058 break;
1059 next_from_lib:;
1063 #ifdef BOARD_TRAITS
1064 if (board_group_info(board, group_to).libs == 1) {
1065 /* Our group is currently in atari; make sure we properly
1066 * count in even the neighbors from the other group in the
1067 * capturable counter. */
1068 enum stone capturing_color = stone_other(board_at(board, group_to));
1069 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1070 coord_t lib = board_group_info(board, group_to).lib[0];
1071 foreach_neighbor(board, lib, {
1072 if (DEBUGL(8) && group_at(board, c) == group_from) fprintf(stderr, "%s[%d] cap bump\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
1073 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
1075 board_trait_queue(board, lib);
1077 #endif
1079 coord_t last_in_group;
1080 foreach_in_group(board, group_from) {
1081 last_in_group = c;
1082 group_at(board, c) = group_to;
1083 } foreach_in_group_end;
1084 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
1085 groupnext_at(board, group_base(group_to)) = group_base(group_from);
1086 memset(gi_from, 0, sizeof(struct group));
1088 if (DEBUGL(7))
1089 fprintf(stderr, "board_play_raw: merged group: %d\n",
1090 group_base(group_to));
1093 static group_t profiling_noinline
1094 new_group(struct board *board, coord_t coord)
1096 group_t group = coord;
1097 struct group *gi = &board_group_info(board, group);
1098 foreach_neighbor(board, coord, {
1099 if (board_at(board, c) == S_NONE)
1100 /* board_group_addlib is ridiculously expensive for us */
1101 #if GROUP_KEEP_LIBS < 4
1102 if (gi->libs < GROUP_KEEP_LIBS)
1103 #endif
1104 gi->lib[gi->libs++] = c;
1107 group_at(board, coord) = group;
1108 groupnext_at(board, coord) = 0;
1110 if (gi->libs == 2)
1111 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
1112 else if (gi->libs == 1)
1113 board_capturable_add(board, group, gi->lib[0]);
1114 check_libs_consistency(board, group);
1116 if (DEBUGL(8))
1117 fprintf(stderr, "new_group: added %d,%d to group %d\n",
1118 coord_x(coord, board), coord_y(coord, board),
1119 group_base(group));
1121 return group;
1124 static inline group_t
1125 play_one_neighbor(struct board *board,
1126 coord_t coord, enum stone color, enum stone other_color,
1127 coord_t c, group_t group)
1129 enum stone ncolor = board_at(board, c);
1130 group_t ngroup = group_at(board, c);
1132 inc_neighbor_count_at(board, c, color);
1133 /* We can be S_NONE, in that case we need to update the safety
1134 * trait since we might be left with only one liberty. */
1135 board_trait_queue(board, c);
1137 if (!ngroup)
1138 return group;
1140 board_group_rmlib(board, ngroup, coord);
1141 if (DEBUGL(7))
1142 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1143 group_base(ngroup), ncolor, color, other_color);
1145 if (ncolor == color && ngroup != group) {
1146 if (!group) {
1147 group = ngroup;
1148 add_to_group(board, group, c, coord);
1149 } else {
1150 merge_groups(board, group, ngroup);
1152 } else if (ncolor == other_color) {
1153 if (DEBUGL(8)) {
1154 struct group *gi = &board_group_info(board, ngroup);
1155 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1156 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1157 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1158 fprintf(stderr, "\n");
1160 if (unlikely(board_group_captured(board, ngroup)))
1161 board_group_capture(board, ngroup);
1163 return group;
1166 /* We played on a place with at least one liberty. We will become a member of
1167 * some group for sure. */
1168 static group_t profiling_noinline
1169 board_play_outside(struct board *board, struct move *m, int f)
1171 coord_t coord = m->coord;
1172 enum stone color = m->color;
1173 enum stone other_color = stone_other(color);
1174 group_t group = 0;
1176 board->f[f] = board->f[--board->flen];
1177 if (DEBUGL(6))
1178 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1180 #if defined(BOARD_TRAITS) && defined(DEBUG)
1181 /* Sanity check that cap matches reality. */
1183 int a = 0;
1184 foreach_neighbor(board, coord, {
1185 group_t g = group_at(board, c);
1186 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1188 assert(a == trait_at(board, coord, color).cap);
1189 assert(board_trait_safe(board, coord, color) == trait_at(board, coord, color).safe);
1191 #endif
1192 foreach_neighbor(board, coord, {
1193 group = play_one_neighbor(board, coord, color, other_color, c, group);
1196 board_at(board, coord) = color;
1197 if (unlikely(!group))
1198 group = new_group(board, coord);
1199 board_gamma_update(board, coord, S_BLACK);
1200 board_gamma_update(board, coord, S_WHITE);
1202 board->last_move2 = board->last_move;
1203 board->last_move = *m;
1204 board->moves++;
1205 board_hash_update(board, coord, color);
1206 board_symmetry_update(board, &board->symmetry, coord);
1207 struct move ko = { pass, S_NONE };
1208 board->ko = ko;
1210 return group;
1213 /* We played in an eye-like shape. Either we capture at least one of the eye
1214 * sides in the process of playing, or return -1. */
1215 static int profiling_noinline
1216 board_play_in_eye(struct board *board, struct move *m, int f)
1218 coord_t coord = m->coord;
1219 enum stone color = m->color;
1220 /* Check ko: Capture at a position of ko capture one move ago */
1221 if (unlikely(color == board->ko.color && coord == board->ko.coord)) {
1222 if (DEBUGL(5))
1223 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1224 return -1;
1225 } else if (DEBUGL(6)) {
1226 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1227 color, coord_x(coord, board), coord_y(coord, board),
1228 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1231 struct move ko = { pass, S_NONE };
1233 int captured_groups = 0;
1235 foreach_neighbor(board, coord, {
1236 group_t g = group_at(board, c);
1237 if (DEBUGL(7))
1238 fprintf(stderr, "board_check: group %d has %d libs\n",
1239 g, board_group_info(board, g).libs);
1240 captured_groups += (board_group_info(board, g).libs == 1);
1243 if (likely(captured_groups == 0)) {
1244 if (DEBUGL(5)) {
1245 if (DEBUGL(6))
1246 board_print(board, stderr);
1247 fprintf(stderr, "board_check: one-stone suicide\n");
1250 return -1;
1252 #ifdef BOARD_TRAITS
1253 /* We _will_ for sure capture something. */
1254 assert(trait_at(board, coord, color).cap > 0);
1255 assert(trait_at(board, coord, color).safe == board_trait_safe(board, coord, color));
1256 #endif
1258 board->f[f] = board->f[--board->flen];
1259 if (DEBUGL(6))
1260 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1262 foreach_neighbor(board, coord, {
1263 inc_neighbor_count_at(board, c, color);
1264 /* Originally, this could not have changed any trait
1265 * since no neighbors were S_NONE, however by now some
1266 * of them might be removed from the board. */
1267 board_trait_queue(board, c);
1269 group_t group = group_at(board, c);
1270 if (!group)
1271 continue;
1273 board_group_rmlib(board, group, coord);
1274 if (DEBUGL(7))
1275 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1276 group_base(group));
1278 if (board_group_captured(board, group)) {
1279 if (board_group_capture(board, group) == 1) {
1280 /* If we captured multiple groups at once,
1281 * we can't be fighting ko so we don't need
1282 * to check for that. */
1283 ko.color = stone_other(color);
1284 ko.coord = c;
1285 board->last_ko = ko;
1286 board->last_ko_age = board->moves;
1287 if (DEBUGL(5))
1288 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1293 board_at(board, coord) = color;
1294 group_t group = new_group(board, coord);
1295 board_gamma_update(board, coord, S_BLACK);
1296 board_gamma_update(board, coord, S_WHITE);
1298 board->last_move2 = board->last_move;
1299 board->last_move = *m;
1300 board->moves++;
1301 board_hash_update(board, coord, color);
1302 board_hash_commit(board);
1303 board_traits_recompute(board);
1304 board_symmetry_update(board, &board->symmetry, coord);
1305 board->ko = ko;
1307 return !!group;
1310 static int __attribute__((flatten))
1311 board_play_f(struct board *board, struct move *m, int f)
1313 if (DEBUGL(7)) {
1314 fprintf(stderr, "board_play(): ---- Playing %d,%d\n", coord_x(m->coord, board), coord_y(m->coord, board));
1316 if (likely(!board_is_eyelike(board, m->coord, stone_other(m->color)))) {
1317 /* NOT playing in an eye. Thus this move has to succeed. (This
1318 * is thanks to New Zealand rules. Otherwise, multi-stone
1319 * suicide might fail.) */
1320 group_t group = board_play_outside(board, m, f);
1321 if (unlikely(board_group_captured(board, group))) {
1322 board_group_capture(board, group);
1324 board_hash_commit(board);
1325 board_traits_recompute(board);
1326 return 0;
1327 } else {
1328 return board_play_in_eye(board, m, f);
1333 board_play(struct board *board, struct move *m)
1335 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1336 struct move nomove = { pass, S_NONE };
1337 board->ko = nomove;
1338 board->last_move2 = board->last_move;
1339 board->last_move = *m;
1340 return 0;
1343 int f;
1344 for (f = 0; f < board->flen; f++)
1345 if (board->f[f] == m->coord)
1346 return board_play_f(board, m, f);
1348 if (DEBUGL(7))
1349 fprintf(stderr, "board_check: stone exists\n");
1350 return -1;
1354 static inline bool
1355 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1357 *coord = b->f[f];
1358 struct move m = { *coord, color };
1359 if (DEBUGL(6))
1360 fprintf(stderr, "trying random move %d: %d,%d\n", f, coord_x(*coord, b), coord_y(*coord, b));
1361 if (unlikely(board_is_one_point_eye(b, *coord, color)) /* bad idea to play into one, usually */
1362 || !board_is_valid_move(b, &m)
1363 || (permit && !permit(permit_data, b, &m)))
1364 return false;
1365 *coord = m.coord; // permit might modify it
1366 return likely(board_play_f(b, &m, f) >= 0);
1369 void
1370 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1372 if (unlikely(b->flen == 0))
1373 goto pass;
1375 int base = fast_random(b->flen), f;
1376 for (f = base; f < b->flen; f++)
1377 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1378 return;
1379 for (f = 0; f < base; f++)
1380 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1381 return;
1383 pass:
1384 *coord = pass;
1385 struct move m = { pass, color };
1386 board_play(b, &m);
1390 bool
1391 board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color)
1393 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1395 /* XXX: We attempt false eye detection but we will yield false
1396 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1398 foreach_diag_neighbor(board, coord) {
1399 color_diag_libs[(enum stone) board_at(board, c)]++;
1400 } foreach_diag_neighbor_end;
1401 /* For false eye, we need two enemy stones diagonally in the
1402 * middle of the board, or just one enemy stone at the edge
1403 * or in the corner. */
1404 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1405 return color_diag_libs[stone_other(eye_color)] >= 2;
1408 bool
1409 board_is_one_point_eye(struct board *board, coord_t coord, enum stone eye_color)
1411 return board_is_eyelike(board, coord, eye_color)
1412 && !board_is_false_eyelike(board, coord, eye_color);
1415 enum stone
1416 board_get_one_point_eye(struct board *board, coord_t coord)
1418 if (board_is_one_point_eye(board, coord, S_WHITE))
1419 return S_WHITE;
1420 else if (board_is_one_point_eye(board, coord, S_BLACK))
1421 return S_BLACK;
1422 else
1423 return S_NONE;
1427 float
1428 board_fast_score(struct board *board)
1430 int scores[S_MAX];
1431 memset(scores, 0, sizeof(scores));
1433 foreach_point(board) {
1434 enum stone color = board_at(board, c);
1435 if (color == S_NONE)
1436 color = board_get_one_point_eye(board, c);
1437 scores[color]++;
1438 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1439 } foreach_point_end;
1441 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1444 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1446 /* One flood-fill iteration; returns true if next iteration
1447 * is required. */
1448 static bool
1449 board_tromp_taylor_iter(struct board *board, int *ownermap)
1451 bool needs_update = false;
1452 foreach_point(board) {
1453 /* Ignore occupied and already-dame positions. */
1454 if (board_at(board, c) != S_NONE || ownermap[c] == 3)
1455 continue;
1456 /* Count neighbors. */
1457 int nei[4] = {0};
1458 foreach_neighbor(board, c, {
1459 nei[ownermap[c]]++;
1461 /* If we have neighbors of both colors, or dame,
1462 * we are dame too. */
1463 if ((nei[1] && nei[2]) || nei[3]) {
1464 ownermap[c] = 3;
1465 /* Speed up the propagation. */
1466 foreach_neighbor(board, c, {
1467 if (board_at(board, c) == S_NONE)
1468 ownermap[c] = 3;
1470 needs_update = true;
1471 continue;
1473 /* If we have neighbors of one color, we are owned
1474 * by that color, too. */
1475 if (!ownermap[c] && (nei[1] || nei[2])) {
1476 int newowner = nei[1] ? 1 : 2;
1477 ownermap[c] = newowner;
1478 /* Speed up the propagation. */
1479 foreach_neighbor(board, c, {
1480 if (board_at(board, c) == S_NONE && !ownermap[c])
1481 ownermap[c] = newowner;
1483 needs_update = true;
1484 continue;
1486 } foreach_point_end;
1487 return needs_update;
1490 /* Tromp-Taylor Counting */
1491 float
1492 board_official_score(struct board *board, struct move_queue *q)
1495 /* A point P, not colored C, is said to reach C, if there is a path of
1496 * (vertically or horizontally) adjacent points of P's color from P to
1497 * a point of color C.
1499 * A player's score is the number of points of her color, plus the
1500 * number of empty points that reach only her color. */
1502 int ownermap[board_size2(board)];
1503 int s[4] = {0};
1504 const int o[4] = {0, 1, 2, 0};
1505 foreach_point(board) {
1506 ownermap[c] = o[board_at(board, c)];
1507 s[board_at(board, c)]++;
1508 } foreach_point_end;
1510 if (q) {
1511 /* Process dead groups. */
1512 for (unsigned int i = 0; i < q->moves; i++) {
1513 foreach_in_group(board, q->move[i]) {
1514 enum stone color = board_at(board, c);
1515 ownermap[c] = o[stone_other(color)];
1516 s[color]--; s[stone_other(color)]++;
1517 } foreach_in_group_end;
1521 /* We need to special-case empty board. */
1522 if (!s[S_BLACK] && !s[S_WHITE])
1523 return board->komi + board->handicap;
1525 while (board_tromp_taylor_iter(board, ownermap))
1526 /* Flood-fill... */;
1528 int scores[S_MAX];
1529 memset(scores, 0, sizeof(scores));
1531 foreach_point(board) {
1532 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
1533 if (ownermap[c] == 3)
1534 continue;
1535 scores[ownermap[c]]++;
1536 } foreach_point_end;
1538 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];