Merge pull request #49 from lemonsqueeze/dragon
[pachi.git] / board.c
blob8fc16d87c160d1bde8e821eb35f48757ba618ab4
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 //#define DEBUG
8 #include "board.h"
9 #include "debug.h"
10 #include "fbook.h"
11 #include "mq.h"
12 #include "random.h"
14 #ifdef BOARD_SPATHASH
15 #include "patternsp.h"
16 #endif
17 #ifdef BOARD_PAT3
18 #include "pattern3.h"
19 #endif
20 #ifdef BOARD_TRAITS
21 static void board_trait_recompute(struct board *board, coord_t coord);
22 #include "tactics/selfatari.h"
23 #endif
26 #if 0
27 #define profiling_noinline __attribute__((noinline))
28 #else
29 #define profiling_noinline
30 #endif
32 #define gi_granularity 4
33 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
36 static void
37 board_setup(struct board *b)
39 memset(b, 0, sizeof(*b));
41 struct move m = { pass, S_NONE };
42 b->last_move = b->last_move2 = b->last_move3 = b->last_move4 = b->last_ko = b->ko = m;
45 struct board *
46 board_init(char *fbookfile)
48 struct board *b = malloc2(sizeof(struct board));
49 board_setup(b);
51 b->fbookfile = fbookfile;
53 // Default setup
54 b->size = 9 + 2;
55 board_clear(b);
57 return b;
60 static size_t
61 board_alloc(struct board *board)
63 /* We do not allocate the board structure itself but we allocate
64 * all the arrays with board contents. */
66 int bsize = board_size2(board) * sizeof(*board->b);
67 int gsize = board_size2(board) * sizeof(*board->g);
68 int fsize = board_size2(board) * sizeof(*board->f);
69 int nsize = board_size2(board) * sizeof(*board->n);
70 int psize = board_size2(board) * sizeof(*board->p);
71 int hsize = board_size2(board) * 2 * sizeof(*board->h);
72 int gisize = board_size2(board) * sizeof(*board->gi);
73 #ifdef WANT_BOARD_C
74 int csize = board_size2(board) * sizeof(*board->c);
75 #else
76 int csize = 0;
77 #endif
78 #ifdef BOARD_SPATHASH
79 int ssize = board_size2(board) * sizeof(*board->spathash);
80 #else
81 int ssize = 0;
82 #endif
83 #ifdef BOARD_PAT3
84 int p3size = board_size2(board) * sizeof(*board->pat3);
85 #else
86 int p3size = 0;
87 #endif
88 #ifdef BOARD_TRAITS
89 int tsize = board_size2(board) * sizeof(*board->t);
90 int tqsize = board_size2(board) * sizeof(*board->t);
91 #else
92 int tsize = 0;
93 int tqsize = 0;
94 #endif
95 int cdsize = board_size2(board) * sizeof(*board->coord);
97 size_t size = bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + cdsize;
98 void *x = malloc2(size);
100 /* board->b must come first */
101 board->b = x; x += bsize;
102 board->g = x; x += gsize;
103 board->f = x; x += fsize;
104 board->p = x; x += psize;
105 board->n = x; x += nsize;
106 board->h = x; x += hsize;
107 board->gi = x; x += gisize;
108 #ifdef WANT_BOARD_C
109 board->c = x; x += csize;
110 #endif
111 #ifdef BOARD_SPATHASH
112 board->spathash = x; x += ssize;
113 #endif
114 #ifdef BOARD_PAT3
115 board->pat3 = x; x += p3size;
116 #endif
117 #ifdef BOARD_TRAITS
118 board->t = x; x += tsize;
119 board->tq = x; x += tqsize;
120 #endif
121 board->coord = x; x += cdsize;
123 return size;
127 board_cmp(struct board *b1, struct board *b2)
129 void **p1 = (void**)b1, **p2 = (void**)b2;
130 for (unsigned int i = 0; i < sizeof(struct board) / sizeof(void*); i++)
131 if (p1[i] != p2[i] &&
132 &p1[i] != (void**)&b1->b &&
133 &p1[i] != (void**)&b1->g &&
134 &p1[i] != (void**)&b1->f &&
135 &p1[i] != (void**)&b1->n &&
136 &p1[i] != (void**)&b1->p &&
137 &p1[i] != (void**)&b1->h &&
138 &p1[i] != (void**)&b1->gi &&
139 #ifdef WANT_BOARD_C
140 &p1[i] != (void**)&b1->c &&
141 #endif
142 #ifdef BOARD_SPATHASH
143 &p1[i] != (void**)&b1->spathash &&
144 #endif
145 #ifdef BOARD_PAT3
146 &p1[i] != (void**)&b1->pat3 &&
147 #endif
148 #ifdef BOARD_TRAITS
149 &p1[i] != (void**)&b1->t &&
150 &p1[i] != (void**)&b1->tq &&
151 #endif
152 &p1[i] != (void**)&b1->coord)
153 return 1;
155 /* Find alloc size */
156 struct board tmp;
157 board_setup(&tmp);
158 size_t size = board_alloc(&tmp);
159 board_done_noalloc(&tmp);
161 return memcmp(b1->b, b2->b, size);
165 board_quick_cmp(struct board *b1, struct board *b2)
167 if (b1->size != b2->size ||
168 b1->size2 != b2->size2 ||
169 b1->bits2 != b2->bits2 ||
170 b1->captures[S_BLACK] != b2->captures[S_BLACK] ||
171 b1->captures[S_WHITE] != b2->captures[S_WHITE] ||
172 b1->moves != b2->moves) {
173 fprintf(stderr, "differs in main vars\n");
174 return 1;
176 if (move_cmp(&b1->last_move, &b2->last_move) ||
177 move_cmp(&b1->last_move2, &b2->last_move2)) {
178 fprintf(stderr, "differs in last_move\n");
179 return 1;
181 if (move_cmp(&b1->ko, &b2->ko) ||
182 move_cmp(&b1->last_ko, &b2->last_ko) ||
183 b1->last_ko_age != b2->last_ko_age) {
184 fprintf(stderr, "differs in ko\n");
185 return 1;
188 int bsize = board_size2(b1) * sizeof(*b1->b);
189 int gsize = board_size2(b1) * sizeof(*b1->g);
190 //int fsize = board_size2(b1) * sizeof(*b1->f);
191 int nsize = board_size2(b1) * sizeof(*b1->n);
192 int psize = board_size2(b1) * sizeof(*b1->p);
193 //int hsize = board_size2(b1) * 2 * sizeof(*b1->h);
194 int gisize = board_size2(b1) * sizeof(*b1->gi);
195 //int csize = board_size2(board) * sizeof(*b1->c);
196 //int ssize = board_size2(board) * sizeof(*b1->spathash);
197 //int p3size = board_size2(board) * sizeof(*b1->pat3);
198 //int tsize = board_size2(board) * sizeof(*b1->t);
199 //int tqsize = board_size2(board) * sizeof(*b1->t);
201 //int cdsize = board_size2(b1) * sizeof(*b1->coord);
203 if (memcmp(b1->b, b2->b, bsize)) {
204 fprintf(stderr, "differs in b\n"); return 1; }
205 if (memcmp(b1->g, b2->g, gsize)) {
206 fprintf(stderr, "differs in g\n"); return 1; }
207 if (memcmp(b1->n, b2->n, nsize)) {
208 fprintf(stderr, "differs in n\n"); return 1; }
209 if (memcmp(b1->p, b2->p, psize)) {
210 fprintf(stderr, "differs in p\n"); return 1; }
211 if (memcmp(b1->gi, b2->gi, gisize)) {
212 fprintf(stderr, "differs in gi\n"); return 1; }
214 return 0;
218 struct board *
219 board_copy(struct board *b2, struct board *b1)
221 memcpy(b2, b1, sizeof(struct board));
223 size_t size = board_alloc(b2);
224 memcpy(b2->b, b1->b, size);
226 // XXX: Special semantics.
227 b2->fbook = NULL;
229 return b2;
232 void
233 board_done_noalloc(struct board *board)
235 if (board->b) free(board->b);
236 if (board->fbook) fbook_done(board->fbook);
239 void
240 board_done(struct board *board)
242 board_done_noalloc(board);
243 free(board);
246 void
247 board_resize(struct board *board, int size)
249 #ifdef BOARD_SIZE
250 assert(board_size(board) == size + 2);
251 #endif
252 assert(size <= BOARD_MAX_SIZE);
253 board->size = size + 2 /* S_OFFBOARD margin */;
254 board->size2 = board_size(board) * board_size(board);
256 board->bits2 = 1;
257 while ((1 << board->bits2) < board->size2) board->bits2++;
259 if (board->b)
260 free(board->b);
262 size_t asize = board_alloc(board);
263 memset(board->b, 0, asize);
266 static void
267 board_init_data(struct board *board)
269 int size = board_size(board);
271 board_setup(board);
272 board_resize(board, size - 2 /* S_OFFBOARD margin */);
274 /* Setup neighborhood iterators */
275 board->nei8[0] = -size - 1; // (-1,-1)
276 board->nei8[1] = 1;
277 board->nei8[2] = 1;
278 board->nei8[3] = size - 2; // (-1,0)
279 board->nei8[4] = 2;
280 board->nei8[5] = size - 2; // (-1,1)
281 board->nei8[6] = 1;
282 board->nei8[7] = 1;
283 board->dnei[0] = -size - 1;
284 board->dnei[1] = 2;
285 board->dnei[2] = size*2 - 2;
286 board->dnei[3] = 2;
288 /* Setup initial symmetry */
289 if (size % 2) {
290 board->symmetry.d = 1;
291 board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2;
292 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
293 board->symmetry.type = SYM_FULL;
294 } else {
295 /* TODO: We do not handle board symmetry on boards
296 * with no tengen yet. */
297 board->symmetry.d = 0;
298 board->symmetry.x1 = board->symmetry.y1 = 1;
299 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
300 board->symmetry.type = SYM_NONE;
303 /* Set up coordinate cache */
304 foreach_point(board) {
305 board->coord[c][0] = c % board_size(board);
306 board->coord[c][1] = c / board_size(board);
307 } foreach_point_end;
309 /* Draw the offboard margin */
310 int top_row = board_size2(board) - board_size(board);
311 int i;
312 for (i = 0; i < board_size(board); i++)
313 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
314 for (i = 0; i <= top_row; i += board_size(board))
315 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
317 foreach_point(board) {
318 coord_t coord = c;
319 if (board_at(board, coord) == S_OFFBOARD)
320 continue;
321 foreach_neighbor(board, c, {
322 inc_neighbor_count_at(board, coord, board_at(board, c));
323 } );
324 } foreach_point_end;
326 /* All positions are free! Except the margin. */
327 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
328 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
329 board->f[board->flen++] = i;
331 /* Initialize zobrist hashtable. */
332 /* We will need these to be stable across Pachi runs for
333 * certain kinds of pattern matching, thus we do not use
334 * fast_random() for this. */
335 hash_t hseed = 0x3121110101112131;
336 foreach_point(board) {
337 board->h[c * 2] = (hseed *= 16807);
338 if (!board->h[c * 2])
339 board->h[c * 2] = 1;
340 /* And once again for white */
341 board->h[c * 2 + 1] = (hseed *= 16807);
342 if (!board->h[c * 2 + 1])
343 board->h[c * 2 + 1] = 1;
344 } foreach_point_end;
346 #ifdef BOARD_SPATHASH
347 /* Initialize spatial hashes. */
348 foreach_point(board) {
349 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
350 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
351 ptcoords_at(x, y, c, board, j);
352 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
353 pthashes[0][j][board_at(board, c)];
354 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
355 pthashes[0][j][stone_other(board_at(board, c))];
358 } foreach_point_end;
359 #endif
360 #ifdef BOARD_PAT3
361 /* Initialize 3x3 pattern codes. */
362 foreach_point(board) {
363 if (board_at(board, c) == S_NONE)
364 board->pat3[c] = pattern3_hash(board, c);
365 } foreach_point_end;
366 #endif
367 #ifdef BOARD_TRAITS
368 /* Initialize traits. */
369 foreach_point(board) {
370 trait_at(board, c, S_BLACK).cap = 0;
371 trait_at(board, c, S_WHITE).cap = 0;
372 trait_at(board, c, S_BLACK).cap1 = 0;
373 trait_at(board, c, S_WHITE).cap1 = 0;
374 #ifdef BOARD_TRAIT_SAFE
375 trait_at(board, c, S_BLACK).safe = true;
376 trait_at(board, c, S_WHITE).safe = true;
377 #endif
378 } foreach_point_end;
379 #endif
382 void
383 board_clear(struct board *board)
385 int size = board_size(board);
386 floating_t komi = board->komi;
387 char *fbookfile = board->fbookfile;
388 enum go_ruleset rules = board->rules;
390 board_done_noalloc(board);
392 static struct board bcache[BOARD_MAX_SIZE + 2];
393 assert(size > 0 && size <= BOARD_MAX_SIZE + 2);
394 if (bcache[size - 1].size == size) {
395 board_copy(board, &bcache[size - 1]);
396 } else {
397 board_init_data(board);
398 board_copy(&bcache[size - 1], board);
401 board->komi = komi;
402 board->fbookfile = fbookfile;
403 board->rules = rules;
405 if (board->fbookfile) {
406 board->fbook = fbook_init(board->fbookfile, board);
410 static char *
411 board_print_top(struct board *board, char *s, char *end, int c)
413 for (int i = 0; i < c; i++) {
414 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
415 s += snprintf(s, end - s, " ");
416 for (int x = 1; x < board_size(board) - 1; x++)
417 s += snprintf(s, end - s, "%c ", asdf[x - 1]);
418 s += snprintf(s, end -s, " ");
420 s += snprintf(s, end - s, "\n");
421 for (int i = 0; i < c; i++) {
422 s += snprintf(s, end - s, " +-");
423 for (int x = 1; x < board_size(board) - 1; x++)
424 s += snprintf(s, end - s, "--");
425 s += snprintf(s, end - s, "+");
427 s += snprintf(s, end - s, "\n");
428 return s;
431 static char *
432 board_print_bottom(struct board *board, char *s, char *end, int c)
434 for (int i = 0; i < c; i++) {
435 s += snprintf(s, end - s, " +-");
436 for (int x = 1; x < board_size(board) - 1; x++)
437 s += snprintf(s, end - s, "--");
438 s += snprintf(s, end - s, "+");
440 s += snprintf(s, end - s, "\n");
441 return s;
444 static char *
445 board_print_row(struct board *board, int y, char *s, char *end, board_cprint cprint, void *data)
447 s += snprintf(s, end - s, " %2d | ", y);
448 for (int x = 1; x < board_size(board) - 1; x++) {
449 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
450 s += snprintf(s, end - s, "%c)", stone2char(board_atxy(board, x, y)));
451 else
452 s += snprintf(s, end - s, "%c ", stone2char(board_atxy(board, x, y)));
454 s += snprintf(s, end - s, "|");
455 if (cprint) {
456 s += snprintf(s, end - s, " %2d | ", y);
457 for (int x = 1; x < board_size(board) - 1; x++) {
458 s = cprint(board, coord_xy(board, x, y), s, end, data);
460 s += snprintf(s, end - s, "|");
462 s += snprintf(s, end - s, "\n");
463 return s;
466 void
467 board_print_custom(struct board *board, FILE *f, board_cprint cprint, void *data)
469 char buf[10240];
470 char *s = buf;
471 char *end = buf + sizeof(buf);
472 s += snprintf(s, end - s, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
473 board->moves, board->komi, board->handicap,
474 board->captures[S_BLACK], board->captures[S_WHITE]);
475 s = board_print_top(board, s, end, 1 + !!cprint);
476 for (int y = board_size(board) - 2; y >= 1; y--)
477 s = board_print_row(board, y, s, end, cprint, data);
478 board_print_bottom(board, s, end, 1 + !!cprint);
479 fprintf(f, "%s\n", buf);
482 static char *
483 board_hprint_row(struct board *board, int y, char *s, char *end, board_print_handler handler, void *data)
485 s += snprintf(s, end - s, " %2d | ", y);
486 for (int x = 1; x < board_size(board) - 1; x++) {
487 char *stone_str = handler(board, coord_xy(board, x, y), data);
488 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
489 s += snprintf(s, end - s, "%s)", stone_str);
490 else
491 s += snprintf(s, end - s, "%s ", stone_str);
493 s += snprintf(s, end - s, "|");
494 s += snprintf(s, end - s, "\n");
495 return s;
498 void
499 board_hprint(struct board *board, FILE *f, board_print_handler handler, void *data)
501 char buf[10240];
502 char *s = buf;
503 char *end = buf + sizeof(buf);
504 s += snprintf(s, end - s, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
505 board->moves, board->komi, board->handicap,
506 board->captures[S_BLACK], board->captures[S_WHITE]);
507 s = board_print_top(board, s, end, 1);
508 for (int y = board_size(board) - 2; y >= 1; y--)
509 s = board_hprint_row(board, y, s, end, handler, data);
510 board_print_bottom(board, s, end, 1);
511 fprintf(f, "%s\n", buf);
514 static char *
515 cprint_group(struct board *board, coord_t c, char *s, char *end, void *data)
517 s += snprintf(s, end - s, "%d ", group_base(group_at(board, c)));
518 return s;
521 void
522 board_print(struct board *board, FILE *f)
524 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL, NULL);
528 #ifdef BOARD_TRAITS
530 #if BOARD_TRAIT_SAFE == 1
531 static bool
532 board_trait_safe(struct board *board, coord_t coord, enum stone color)
534 return board_safe_to_play(board, coord, color);
536 #elif BOARD_TRAIT_SAFE == 2
537 static bool
538 board_trait_safe(struct board *board, coord_t coord, enum stone color)
540 return !is_bad_selfatari(board, color, coord);
542 #endif
544 static void
545 board_trait_recompute(struct board *board, coord_t coord)
547 int sfb = -1, sfw = -1;
548 #ifdef BOARD_TRAIT_SAFE
549 sfb = trait_at(board, coord, S_BLACK).safe = board_trait_safe(board, coord, S_BLACK);
550 sfw = trait_at(board, coord, S_WHITE).safe = board_trait_safe(board, coord, S_WHITE);
551 #endif
552 if (DEBUGL(8)) {
553 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
554 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
555 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).cap1, sfb,
556 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).cap1, sfw);
559 #endif
561 /* Recompute traits for dirty points that we have previously touched
562 * somehow (libs of their neighbors changed or so). */
563 static void
564 board_traits_recompute(struct board *board)
566 #ifdef BOARD_TRAITS
567 for (int i = 0; i < board->tqlen; i++) {
568 coord_t coord = board->tq[i];
569 trait_at(board, coord, S_BLACK).dirty = false;
570 if (board_at(board, coord) != S_NONE)
571 continue;
572 board_trait_recompute(board, coord);
574 board->tqlen = 0;
575 #endif
578 /* Queue traits of given point for recomputing. */
579 static void
580 board_trait_queue(struct board *board, coord_t coord)
582 #ifdef BOARD_TRAITS
583 if (trait_at(board, coord, S_BLACK).dirty)
584 return;
585 board->tq[board->tqlen++] = coord;
586 trait_at(board, coord, S_BLACK).dirty = true;
587 #endif
591 /* Update board hash with given coordinate. */
592 static void profiling_noinline
593 board_hash_update(struct board *board, coord_t coord, enum stone color)
595 board->hash ^= hash_at(board, coord, color);
596 board->qhash[coord_quadrant(coord, board)] ^= hash_at(board, coord, color);
597 if (DEBUGL(8))
598 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);
600 #ifdef BOARD_SPATHASH
601 /* Gridcular metric is reflective, so we update all hashes
602 * of appropriate ditance in OUR circle. */
603 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
604 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
605 ptcoords_at(x, y, coord, board, j);
606 /* We either changed from S_NONE to color
607 * or vice versa; doesn't matter. */
608 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
609 pthashes[0][j][color] ^ pthashes[0][j][S_NONE];
610 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
611 pthashes[0][j][stone_other(color)] ^ pthashes[0][j][S_NONE];
614 #endif
616 #if defined(BOARD_PAT3)
617 /* @color is not what we need in case of capture. */
618 static const int ataribits[8] = { -1, 0, -1, 1, 2, -1, 3, -1 };
619 enum stone new_color = board_at(board, coord);
620 bool in_atari = false;
621 if (new_color == S_NONE) {
622 board->pat3[coord] = pattern3_hash(board, coord);
623 } else {
624 in_atari = (board_group_info(board, group_at(board, coord)).libs == 1);
626 foreach_8neighbor(board, coord) {
627 /* Internally, the loop uses fn__i=[0..7]. We can use
628 * it directly to address bits within the bitmap of the
629 * neighbors since the bitmap order is reverse to the
630 * loop order. */
631 if (board_at(board, c) != S_NONE)
632 continue;
633 board->pat3[c] &= ~(3 << (fn__i*2));
634 board->pat3[c] |= new_color << (fn__i*2);
635 if (ataribits[fn__i] >= 0) {
636 board->pat3[c] &= ~(1 << (16 + ataribits[fn__i]));
637 board->pat3[c] |= in_atari << (16 + ataribits[fn__i]);
639 #if defined(BOARD_TRAITS)
640 board_trait_queue(board, c);
641 #endif
642 } foreach_8neighbor_end;
643 #endif
646 /* Commit current board hash to history. */
647 static void profiling_noinline
648 board_hash_commit(struct board *board)
650 if (DEBUGL(8))
651 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
652 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
653 board->history_hash[board->hash & history_hash_mask] = board->hash;
654 } else {
655 hash_t i = board->hash;
656 while (board->history_hash[i & history_hash_mask]) {
657 if (board->history_hash[i & history_hash_mask] == board->hash) {
658 if (DEBUGL(5))
659 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
660 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
661 board->superko_violation = true;
662 return;
664 i = history_hash_next(i);
666 board->history_hash[i & history_hash_mask] = board->hash;
671 void
672 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
674 if (likely(symmetry->type == SYM_NONE)) {
675 /* Fully degenerated already. We do not support detection
676 * of restoring of symmetry, assuming that this is too rare
677 * a case to handle. */
678 return;
681 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
682 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
683 if (DEBUGL(6)) {
684 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
685 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
686 symmetry->d, symmetry->type, x, y);
689 switch (symmetry->type) {
690 case SYM_FULL:
691 if (x == t && y == t) {
692 /* Tengen keeps full symmetry. */
693 return;
695 /* New symmetry now? */
696 if (x == y) {
697 symmetry->type = SYM_DIAG_UP;
698 symmetry->x1 = symmetry->y1 = 1;
699 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
700 symmetry->d = 1;
701 } else if (dx == y) {
702 symmetry->type = SYM_DIAG_DOWN;
703 symmetry->x1 = symmetry->y1 = 1;
704 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
705 symmetry->d = 1;
706 } else if (x == t) {
707 symmetry->type = SYM_HORIZ;
708 symmetry->y1 = 1;
709 symmetry->y2 = board_size(b) - 1;
710 symmetry->d = 0;
711 } else if (y == t) {
712 symmetry->type = SYM_VERT;
713 symmetry->x1 = 1;
714 symmetry->x2 = board_size(b) - 1;
715 symmetry->d = 0;
716 } else {
717 break_symmetry:
718 symmetry->type = SYM_NONE;
719 symmetry->x1 = symmetry->y1 = 1;
720 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
721 symmetry->d = 0;
723 break;
724 case SYM_DIAG_UP:
725 if (x == y)
726 return;
727 goto break_symmetry;
728 case SYM_DIAG_DOWN:
729 if (dx == y)
730 return;
731 goto break_symmetry;
732 case SYM_HORIZ:
733 if (x == t)
734 return;
735 goto break_symmetry;
736 case SYM_VERT:
737 if (y == t)
738 return;
739 goto break_symmetry;
740 case SYM_NONE:
741 assert(0);
742 break;
745 if (DEBUGL(6)) {
746 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
747 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
748 symmetry->d, symmetry->type);
750 /* Whew. */
754 void
755 board_handicap_stone(struct board *board, int x, int y, FILE *f)
757 struct move m;
758 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
760 board_play(board, &m);
761 /* Simulate white passing; otherwise, UCT search can get confused since
762 * tree depth parity won't match the color to move. */
763 board->moves++;
765 char *str = coord2str(m.coord, board);
766 if (DEBUGL(1))
767 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
768 if (f) fprintf(f, "%s ", str);
769 free(str);
772 void
773 board_handicap(struct board *board, int stones, FILE *f)
775 int margin = 3 + (board_size(board) >= 13);
776 int min = margin;
777 int mid = board_size(board) / 2;
778 int max = board_size(board) - 1 - margin;
779 const int places[][2] = {
780 { min, min }, { max, max }, { min, max }, { max, min },
781 { min, mid }, { max, mid },
782 { mid, min }, { mid, max },
783 { mid, mid },
786 board->handicap = stones;
788 if (stones == 5 || stones == 7) {
789 board_handicap_stone(board, mid, mid, f);
790 stones--;
793 int i;
794 for (i = 0; i < stones; i++)
795 board_handicap_stone(board, places[i][0], places[i][1], f);
799 static void __attribute__((noinline))
800 check_libs_consistency(struct board *board, group_t g)
802 #ifdef DEBUG
803 if (!g) return;
804 struct group *gi = &board_group_info(board, g);
805 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
806 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
807 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
808 assert(0);
810 #endif
813 static void
814 check_pat3_consistency(struct board *board, coord_t coord)
816 #ifdef DEBUG
817 foreach_8neighbor(board, coord) {
818 if (board_at(board, c) == S_NONE && pattern3_hash(board, c) != board->pat3[c]) {
819 board_print(board, stderr);
820 fprintf(stderr, "%s(%d)->%s(%d) computed %x != stored %x (%d)\n", coord2sstr(coord, board), coord, coord2sstr(c, board), c, pattern3_hash(board, c), board->pat3[c], fn__i);
821 assert(0);
823 } foreach_8neighbor_end;
824 #endif
827 static void
828 board_capturable_add(struct board *board, group_t group, coord_t lib, bool onestone)
830 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
831 #ifdef BOARD_TRAITS
832 /* Increase capturable count trait of my last lib. */
833 enum stone capturing_color = stone_other(board_at(board, group));
834 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
835 foreach_neighbor(board, lib, {
836 if (DEBUGL(8) && group_at(board, c) == group)
837 fprintf(stderr, "%s[%d] %s cap bump bc of %s(%d) member %s onestone %d\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), onestone);
838 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
839 trait_at(board, lib, capturing_color).cap1 += (group_at(board, c) == group && onestone);
841 board_trait_queue(board, lib);
842 #endif
844 #ifdef BOARD_PAT3
845 int fn__i = 0;
846 foreach_neighbor(board, lib, {
847 board->pat3[lib] |= (group_at(board, c) == group) << (16 + 3 - fn__i);
848 fn__i++;
850 #endif
852 #ifdef WANT_BOARD_C
853 /* Update the list of capturable groups. */
854 assert(group);
855 assert(board->clen < board_size2(board));
856 board->c[board->clen++] = group;
857 #endif
859 static void
860 board_capturable_rm(struct board *board, group_t group, coord_t lib, bool onestone)
862 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
863 #ifdef BOARD_TRAITS
864 /* Decrease capturable count trait of my previously-last lib. */
865 enum stone capturing_color = stone_other(board_at(board, group));
866 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
867 foreach_neighbor(board, lib, {
868 if (DEBUGL(8) && group_at(board, c) == group)
869 fprintf(stderr, "%s[%d] cap dump bc of %s(%d) member %s onestone %d\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap, coord2sstr(group, board), board_group_info(board, group).libs, coord2sstr(c, board), onestone);
870 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
871 trait_at(board, lib, capturing_color).cap1 -= (group_at(board, c) == group && onestone);
873 board_trait_queue(board, lib);
874 #endif
876 #ifdef BOARD_PAT3
877 int fn__i = 0;
878 foreach_neighbor(board, lib, {
879 board->pat3[lib] &= ~((group_at(board, c) == group) << (16 + 3 - fn__i));
880 fn__i++;
882 #endif
884 #ifdef WANT_BOARD_C
885 /* Update the list of capturable groups. */
886 for (int i = 0; i < board->clen; i++) {
887 if (unlikely(board->c[i] == group)) {
888 board->c[i] = board->c[--board->clen];
889 return;
892 fprintf(stderr, "rm of bad group %d\n", group_base(group));
893 assert(0);
894 #endif
897 static void
898 board_atariable_add(struct board *board, group_t group, coord_t lib1, coord_t lib2)
900 #ifdef BOARD_TRAITS
901 board_trait_queue(board, lib1);
902 board_trait_queue(board, lib2);
903 #endif
905 static void
906 board_atariable_rm(struct board *board, group_t group, coord_t lib1, coord_t lib2)
908 #ifdef BOARD_TRAITS
909 board_trait_queue(board, lib1);
910 board_trait_queue(board, lib2);
911 #endif
914 static void
915 board_group_addlib(struct board *board, group_t group, coord_t coord, struct board_undo *u)
917 if (DEBUGL(7)) {
918 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
919 group_base(group), coord2sstr(group_base(group), board),
920 board_group_info(board, group).libs, coord2sstr(coord, board));
923 if (!u) check_libs_consistency(board, group);
925 struct group *gi = &board_group_info(board, group);
926 bool onestone = group_is_onestone(board, group);
927 if (gi->libs < GROUP_KEEP_LIBS) {
928 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
929 #if 0
930 /* Seems extra branch just slows it down */
931 if (!gi->lib[i])
932 break;
933 #endif
934 if (unlikely(gi->lib[i] == coord))
935 return;
937 if (!u) {
938 if (gi->libs == 0) {
939 board_capturable_add(board, group, coord, onestone);
940 } else if (gi->libs == 1) {
941 board_capturable_rm(board, group, gi->lib[0], onestone);
942 board_atariable_add(board, group, gi->lib[0], coord);
943 } else if (gi->libs == 2) {
944 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
947 gi->lib[gi->libs++] = coord;
950 if (!u) check_libs_consistency(board, group);
953 static void
954 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
956 /* Add extra liberty from the board to our liberty list. */
957 unsigned char watermark[board_size2(board) / 8];
958 memset(watermark, 0, sizeof(watermark));
959 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
960 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
962 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
963 watermark_set(gi->lib[i]);
964 watermark_set(avoid);
966 foreach_in_group(board, group) {
967 coord_t coord2 = c;
968 foreach_neighbor(board, coord2, {
969 if (board_at(board, c) + watermark_get(c) != S_NONE)
970 continue;
971 watermark_set(c);
972 gi->lib[gi->libs++] = c;
973 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
974 return;
975 } );
976 } foreach_in_group_end;
977 #undef watermark_get
978 #undef watermark_set
981 static void
982 board_group_rmlib(struct board *board, group_t group, coord_t coord, struct board_undo *u)
984 if (DEBUGL(7)) {
985 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
986 group_base(group), coord2sstr(group_base(group), board),
987 board_group_info(board, group).libs, coord2sstr(coord, board));
990 struct group *gi = &board_group_info(board, group);
991 bool onestone = group_is_onestone(board, group);
992 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
993 #if 0
994 /* Seems extra branch just slows it down */
995 if (!gi->lib[i])
996 break;
997 #endif
998 if (likely(gi->lib[i] != coord))
999 continue;
1001 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
1002 gi->lib[gi->libs] = 0;
1004 if (!u) check_libs_consistency(board, group);
1006 /* Postpone refilling lib[] until we need to. */
1007 assert(GROUP_REFILL_LIBS > 1);
1008 if (gi->libs > GROUP_REFILL_LIBS)
1009 return;
1010 if (gi->libs == GROUP_REFILL_LIBS)
1011 board_group_find_extra_libs(board, group, gi, coord);
1012 if (u) return;
1014 if (gi->libs == 2) {
1015 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
1016 } else if (gi->libs == 1) {
1017 board_capturable_add(board, group, gi->lib[0], onestone);
1018 board_atariable_rm(board, group, gi->lib[0], lib);
1019 } else if (gi->libs == 0)
1020 board_capturable_rm(board, group, lib, onestone);
1021 return;
1024 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
1025 * can call this multiple times per coord. */
1026 if (!u) check_libs_consistency(board, group);
1027 return;
1031 /* This is a low-level routine that doesn't maintain consistency
1032 * of all the board data structures. */
1033 static void
1034 board_remove_stone(struct board *board, group_t group, coord_t c, struct board_undo *u)
1036 enum stone color = board_at(board, c);
1037 board_at(board, c) = S_NONE;
1038 group_at(board, c) = 0;
1039 if (!u) {
1040 board_hash_update(board, c, color);
1041 #ifdef BOARD_TRAITS
1042 /* We mark as cannot-capture now. If this is a ko/snapback,
1043 * we will get incremented later in board_group_addlib(). */
1044 trait_at(board, c, S_BLACK).cap = trait_at(board, c, S_BLACK).cap1 = 0;
1045 trait_at(board, c, S_WHITE).cap = trait_at(board, c, S_WHITE).cap1 = 0;
1046 board_trait_queue(board, c);
1047 #endif
1050 /* Increase liberties of surrounding groups */
1051 coord_t coord = c;
1052 foreach_neighbor(board, coord, {
1053 dec_neighbor_count_at(board, c, color);
1054 if (!u) board_trait_queue(board, c);
1055 group_t g = group_at(board, c);
1056 if (g && g != group)
1057 board_group_addlib(board, g, coord, u);
1059 if (u) return;
1061 #ifdef BOARD_PAT3
1062 /* board_hash_update() might have seen the freed up point as able
1063 * to capture another group in atari that only after the loop
1064 * above gained enough liberties. Reset pat3 again. */
1065 board->pat3[c] = pattern3_hash(board, c);
1066 #endif
1068 if (DEBUGL(6))
1069 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
1070 board->f[board->flen++] = c;
1073 static int profiling_noinline
1074 board_group_capture(struct board *board, group_t group, struct board_undo *u)
1076 int stones = 0;
1078 foreach_in_group(board, group) {
1079 board->captures[stone_other(board_at(board, c))]++;
1080 board_remove_stone(board, group, c, u);
1081 stones++;
1082 } foreach_in_group_end;
1084 struct group *gi = &board_group_info(board, group);
1085 assert(gi->libs == 0);
1086 memset(gi, 0, sizeof(*gi));
1088 return stones;
1092 static void profiling_noinline
1093 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord, struct board_undo *u)
1095 #ifdef BOARD_TRAITS
1096 struct group *gi = &board_group_info(board, group);
1097 bool onestone = group_is_onestone(board, group);
1099 if (!u && gi->libs == 1) {
1100 /* Our group is temporarily in atari; make sure the capturable
1101 * counts also correspond to the newly added stone before we
1102 * start adding liberties again so bump-dump ops match. */
1103 enum stone capturing_color = stone_other(board_at(board, group));
1104 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1106 coord_t lib = board_group_info(board, group).lib[0];
1107 if (coord_is_adjecent(lib, coord, board)) {
1108 if (DEBUGL(8))
1109 fprintf(stderr, "add_to_group %s: %s[%d] bump\n", coord2sstr(group, board), coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
1110 trait_at(board, lib, capturing_color).cap++;
1111 /* This is never a 1-stone group, obviously. */
1112 board_trait_queue(board, lib);
1115 if (onestone) {
1116 /* We are not 1-stone group anymore, update the cap1
1117 * counter specifically. */
1118 foreach_neighbor(board, group, {
1119 if (board_at(board, c) != S_NONE) continue;
1120 trait_at(board, c, capturing_color).cap1--;
1121 board_trait_queue(board, c);
1125 #endif
1127 group_at(board, coord) = group;
1128 groupnext_at(board, coord) = groupnext_at(board, prevstone);
1129 groupnext_at(board, prevstone) = coord;
1131 foreach_neighbor(board, coord, {
1132 if (board_at(board, c) == S_NONE)
1133 board_group_addlib(board, group, c, u);
1136 if (DEBUGL(8))
1137 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1138 coord_x(prevstone, board), coord_y(prevstone, board),
1139 coord_x(coord, board), coord_y(coord, board),
1140 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
1141 group_base(group));
1144 static void profiling_noinline
1145 merge_groups(struct board *board, group_t group_to, group_t group_from, struct board_undo *u)
1147 if (DEBUGL(7))
1148 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
1149 group_base(group_from), group_base(group_to));
1150 struct group *gi_from = &board_group_info(board, group_from);
1151 struct group *gi_to = &board_group_info(board, group_to);
1152 bool onestone_from = group_is_onestone(board, group_from);
1153 bool onestone_to = group_is_onestone(board, group_to);
1155 if (!u) {
1156 /* We do this early before the group info is rewritten. */
1157 if (gi_from->libs == 2)
1158 board_atariable_rm(board, group_from, gi_from->lib[0], gi_from->lib[1]);
1159 else if (gi_from->libs == 1)
1160 board_capturable_rm(board, group_from, gi_from->lib[0], onestone_from);
1163 if (DEBUGL(7))
1164 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
1166 if (gi_to->libs < GROUP_KEEP_LIBS) {
1167 for (int i = 0; i < gi_from->libs; i++) {
1168 for (int j = 0; j < gi_to->libs; j++)
1169 if (gi_to->lib[j] == gi_from->lib[i])
1170 goto next_from_lib;
1171 if (!u) {
1172 if (gi_to->libs == 0) {
1173 board_capturable_add(board, group_to, gi_from->lib[i], onestone_to);
1174 } else if (gi_to->libs == 1) {
1175 board_capturable_rm(board, group_to, gi_to->lib[0], onestone_to);
1176 board_atariable_add(board, group_to, gi_to->lib[0], gi_from->lib[i]);
1177 } else if (gi_to->libs == 2) {
1178 board_atariable_rm(board, group_to, gi_to->lib[0], gi_to->lib[1]);
1181 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
1182 if (gi_to->libs >= GROUP_KEEP_LIBS)
1183 break;
1184 next_from_lib:;
1188 if (!u && gi_to->libs == 1) {
1189 coord_t lib = board_group_info(board, group_to).lib[0];
1190 #ifdef BOARD_TRAITS
1191 enum stone capturing_color = stone_other(board_at(board, group_to));
1192 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1194 /* Our group is currently in atari; make sure we properly
1195 * count in even the neighbors from the other group in the
1196 * capturable counter. */
1197 foreach_neighbor(board, lib, {
1198 if (DEBUGL(8) && group_at(board, c) == group_from)
1199 fprintf(stderr, "%s[%d] cap bump\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
1200 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
1201 /* This is never a 1-stone group, obviously. */
1203 board_trait_queue(board, lib);
1205 if (onestone_to) {
1206 /* We are not 1-stone group anymore, update the cap1
1207 * counter specifically. */
1208 foreach_neighbor(board, group_to, {
1209 if (board_at(board, c) != S_NONE) continue;
1210 trait_at(board, c, capturing_color).cap1--;
1211 board_trait_queue(board, c);
1214 #endif
1215 #ifdef BOARD_PAT3
1216 if (gi_from->libs == 1) {
1217 /* We removed group_from from capturable groups,
1218 * therefore switching the atari flag off.
1219 * We need to set it again since group_to is also
1220 * capturable. */
1221 int fn__i = 0;
1222 foreach_neighbor(board, lib, {
1223 board->pat3[lib] |= (group_at(board, c) == group_from) << (16 + 3 - fn__i);
1224 fn__i++;
1227 #endif
1230 coord_t last_in_group;
1231 foreach_in_group(board, group_from) {
1232 last_in_group = c;
1233 group_at(board, c) = group_to;
1234 } foreach_in_group_end;
1236 if (u) u->merged[++u->nmerged_tmp].last = last_in_group;
1237 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
1238 groupnext_at(board, group_base(group_to)) = group_base(group_from);
1239 memset(gi_from, 0, sizeof(struct group));
1241 if (DEBUGL(7))
1242 fprintf(stderr, "board_play_raw: merged group: %d\n",
1243 group_base(group_to));
1246 static group_t profiling_noinline
1247 new_group(struct board *board, coord_t coord, struct board_undo *u)
1249 group_t group = coord;
1250 struct group *gi = &board_group_info(board, group);
1251 foreach_neighbor(board, coord, {
1252 if (board_at(board, c) == S_NONE)
1253 /* board_group_addlib is ridiculously expensive for us */
1254 #if GROUP_KEEP_LIBS < 4
1255 if (gi->libs < GROUP_KEEP_LIBS)
1256 #endif
1257 gi->lib[gi->libs++] = c;
1260 group_at(board, coord) = group;
1261 groupnext_at(board, coord) = 0;
1263 if (!u) {
1264 if (gi->libs == 2)
1265 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
1266 else if (gi->libs == 1)
1267 board_capturable_add(board, group, gi->lib[0], true);
1268 check_libs_consistency(board, group);
1271 if (DEBUGL(8))
1272 fprintf(stderr, "new_group: added %d,%d to group %d\n",
1273 coord_x(coord, board), coord_y(coord, board),
1274 group_base(group));
1276 return group;
1279 static inline void
1280 undo_save_merge(struct board *b, struct board_undo *u, group_t g, coord_t c)
1282 if (g == u->merged[0].group || g == u->merged[1].group ||
1283 g == u->merged[2].group || g == u->merged[3].group)
1284 return;
1286 int i = u->nmerged++;
1287 if (!i)
1288 u->inserted = c;
1289 u->merged[i].group = g;
1290 u->merged[i].last = 0; // can remove
1291 u->merged[i].info = board_group_info(b, g);
1294 static inline void
1295 undo_save_enemy(struct board *b, struct board_undo *u, group_t g)
1297 if (g == u->enemies[0].group || g == u->enemies[1].group ||
1298 g == u->enemies[2].group || g == u->enemies[3].group)
1299 return;
1301 int i = u->nenemies++;
1302 u->enemies[i].group = g;
1303 u->enemies[i].info = board_group_info(b, g);
1305 int j = 0;
1306 coord_t *stones = u->enemies[i].stones;
1307 if (board_group_info(b, g).libs <= 1) { // Will be captured
1308 foreach_in_group(b, g) {
1309 stones[j++] = c;
1310 } foreach_in_group_end;
1311 u->captures += j;
1313 stones[j] = 0;
1316 static void
1317 undo_save_group_info(struct board *b, coord_t coord, enum stone color, struct board_undo *u)
1319 u->next_at = groupnext_at(b, coord);
1321 foreach_neighbor(b, coord, {
1322 group_t g = group_at(b, c);
1324 if (board_at(b, c) == color)
1325 undo_save_merge(b, u, g, c);
1326 else if (board_at(b, c) == stone_other(color))
1327 undo_save_enemy(b, u, g);
1331 static void
1332 undo_save_suicide(struct board *b, coord_t coord, enum stone color, struct board_undo *u)
1334 foreach_neighbor(b, coord, {
1335 if (board_at(b, c) == color) {
1336 // Handle suicide as a capture ...
1337 undo_save_enemy(b, u, group_at(b, c));
1338 return;
1341 assert(0);
1344 static inline group_t
1345 play_one_neighbor(struct board *board,
1346 coord_t coord, enum stone color, enum stone other_color,
1347 coord_t c, group_t group, struct board_undo *u)
1349 enum stone ncolor = board_at(board, c);
1350 group_t ngroup = group_at(board, c);
1352 inc_neighbor_count_at(board, c, color);
1353 /* We can be S_NONE, in that case we need to update the safety
1354 * trait since we might be left with only one liberty. */
1355 if (!u) board_trait_queue(board, c);
1357 if (!ngroup)
1358 return group;
1360 board_group_rmlib(board, ngroup, coord, u);
1361 if (DEBUGL(7))
1362 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1363 group_base(ngroup), ncolor, color, other_color);
1365 if (ncolor == color && ngroup != group) {
1366 if (!group) {
1367 group = ngroup;
1368 add_to_group(board, group, c, coord, u);
1369 } else {
1370 merge_groups(board, group, ngroup, u);
1372 } else if (ncolor == other_color) {
1373 if (DEBUGL(8)) {
1374 struct group *gi = &board_group_info(board, ngroup);
1375 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1376 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1377 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1378 fprintf(stderr, "\n");
1380 if (unlikely(board_group_captured(board, ngroup)))
1381 board_group_capture(board, ngroup, u);
1383 return group;
1386 /* We played on a place with at least one liberty. We will become a member of
1387 * some group for sure. */
1388 static group_t profiling_noinline
1389 board_play_outside(struct board *board, struct move *m, int f, struct board_undo *u)
1391 coord_t coord = m->coord;
1392 enum stone color = m->color;
1393 enum stone other_color = stone_other(color);
1394 group_t group = 0;
1396 if (u)
1397 undo_save_group_info(board, coord, color, u);
1398 else {
1399 board->f[f] = board->f[--board->flen];
1400 if (DEBUGL(6))
1401 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1403 #if defined(BOARD_TRAITS) && defined(DEBUG)
1404 /* Sanity check that cap matches reality. */
1406 int a = 0, b = 0;
1407 foreach_neighbor(board, coord, {
1408 group_t g = group_at(board, c);
1409 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1410 b += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1) && group_is_onestone(board, g);
1412 assert(a == trait_at(board, coord, color).cap);
1413 assert(b == trait_at(board, coord, color).cap1);
1414 #ifdef BOARD_TRAIT_SAFE
1415 assert(board_trait_safe(board, coord, color) == trait_at(board, coord, color).safe);
1416 #endif
1418 #endif
1420 foreach_neighbor(board, coord, {
1421 group = play_one_neighbor(board, coord, color, other_color, c, group, u);
1424 board_at(board, coord) = color;
1425 if (unlikely(!group))
1426 group = new_group(board, coord, u);
1428 if (!u) {
1429 board->last_move4 = board->last_move3;
1430 board->last_move3 = board->last_move2;
1432 board->last_move2 = board->last_move;
1433 board->last_move = *m;
1434 board->moves++;
1435 if (!u) {
1436 board_hash_update(board, coord, color);
1437 board_symmetry_update(board, &board->symmetry, coord);
1439 struct move ko = { pass, S_NONE };
1440 board->ko = ko;
1442 if (!u) check_pat3_consistency(board, coord);
1444 return group;
1447 /* We played in an eye-like shape. Either we capture at least one of the eye
1448 * sides in the process of playing, or return -1. */
1449 static int profiling_noinline
1450 board_play_in_eye(struct board *board, struct move *m, int f, struct board_undo *u)
1452 coord_t coord = m->coord;
1453 enum stone color = m->color;
1454 /* Check ko: Capture at a position of ko capture one move ago */
1455 if (unlikely(color == board->ko.color && coord == board->ko.coord)) {
1456 if (DEBUGL(5))
1457 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1458 return -1;
1459 } else if (DEBUGL(6)) {
1460 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1461 color, coord_x(coord, board), coord_y(coord, board),
1462 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1465 struct move ko = { pass, S_NONE };
1467 int captured_groups = 0;
1469 foreach_neighbor(board, coord, {
1470 group_t g = group_at(board, c);
1471 if (DEBUGL(7))
1472 fprintf(stderr, "board_check: group %d has %d libs\n",
1473 g, board_group_info(board, g).libs);
1474 captured_groups += (board_group_info(board, g).libs == 1);
1477 if (likely(captured_groups == 0)) {
1478 if (DEBUGL(5)) {
1479 if (DEBUGL(6))
1480 board_print(board, stderr);
1481 fprintf(stderr, "board_check: one-stone suicide\n");
1484 return -1;
1487 if (!u) {
1488 #ifdef BOARD_TRAITS
1489 /* We _will_ for sure capture something. */
1490 assert(trait_at(board, coord, color).cap > 0);
1491 #ifdef BOARD_TRAIT_SAFE
1492 assert(trait_at(board, coord, color).safe == board_trait_safe(board, coord, color));
1493 #endif
1494 #endif
1496 board->f[f] = board->f[--board->flen];
1497 if (DEBUGL(6))
1498 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1500 else
1501 undo_save_group_info(board, coord, color, u);
1503 int ko_caps = 0;
1504 coord_t cap_at = pass;
1505 foreach_neighbor(board, coord, {
1506 inc_neighbor_count_at(board, c, color);
1507 /* Originally, this could not have changed any trait
1508 * since no neighbors were S_NONE, however by now some
1509 * of them might be removed from the board. */
1510 if (!u) board_trait_queue(board, c);
1512 group_t group = group_at(board, c);
1513 if (!group)
1514 continue;
1516 board_group_rmlib(board, group, coord, u);
1517 if (DEBUGL(7))
1518 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1519 group_base(group));
1521 if (board_group_captured(board, group)) {
1522 ko_caps += board_group_capture(board, group, u);
1523 cap_at = c;
1526 if (ko_caps == 1) {
1527 ko.color = stone_other(color);
1528 ko.coord = cap_at; // unique
1529 board->last_ko = ko;
1530 board->last_ko_age = board->moves;
1531 if (DEBUGL(5))
1532 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1535 board_at(board, coord) = color;
1536 group_t group = new_group(board, coord, u);
1538 if (!u) {
1539 board->last_move4 = board->last_move3;
1540 board->last_move3 = board->last_move2;
1542 board->last_move2 = board->last_move;
1543 board->last_move = *m;
1544 board->moves++;
1545 if (!u) {
1546 board_hash_update(board, coord, color);
1547 board_hash_commit(board);
1548 board_traits_recompute(board);
1549 board_symmetry_update(board, &board->symmetry, coord);
1551 board->ko = ko;
1553 if (!u) check_pat3_consistency(board, coord);
1555 return !!group;
1558 static int __attribute__((flatten))
1559 board_play_f(struct board *board, struct move *m, int f, struct board_undo *u)
1561 if (DEBUGL(7)) {
1562 fprintf(stderr, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m->coord, board), coord_x(m->coord, board), coord_y(m->coord, board));
1564 if (likely(!board_is_eyelike(board, m->coord, stone_other(m->color)))) {
1565 /* NOT playing in an eye. Thus this move has to succeed. (This
1566 * is thanks to New Zealand rules. Otherwise, multi-stone
1567 * suicide might fail.) */
1568 group_t group = board_play_outside(board, m, f, u);
1569 if (unlikely(board_group_captured(board, group))) {
1570 if (u) undo_save_suicide(board, m->coord, m->color, u);
1571 board_group_capture(board, group, u);
1573 if (!u) {
1574 board_hash_commit(board);
1575 board_traits_recompute(board);
1577 return 0;
1578 } else {
1579 return board_play_in_eye(board, m, f, u);
1583 static void
1584 undo_init(struct board *b, struct move *m, struct board_undo *u)
1586 // Paranoid uninitialized mem test
1587 // memset(u, 0xff, sizeof(*u));
1589 u->last_move2 = b->last_move2;
1590 u->ko = b->ko;
1591 u->last_ko = b->last_ko;
1592 u->last_ko_age = b->last_ko_age;
1593 u->captures = 0;
1595 u->nmerged = u->nmerged_tmp = u->nenemies = 0;
1596 for (int i = 0; i < 4; i++)
1597 u->merged[i].group = u->enemies[i].group = 0;
1600 static int
1601 board_play_(struct board *board, struct move *m, struct board_undo *u)
1603 #ifdef BOARD_UNDO_CHECKS
1604 assert(u || !board->quicked);
1605 #endif
1607 if (u) undo_init(board, m, u);
1609 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1610 if (is_pass(m->coord) && board->rules == RULES_SIMING) {
1611 /* On pass, the player gives a pass stone
1612 * to the opponent. */
1613 board->captures[stone_other(m->color)]++;
1615 struct move nomove = { pass, S_NONE };
1616 board->ko = nomove;
1617 if (!u) {
1618 board->last_move4 = board->last_move3;
1619 board->last_move3 = board->last_move2;
1621 board->last_move2 = board->last_move;
1622 board->last_move = *m;
1623 return 0;
1626 if (u)
1627 return board_play_f(board, m, -1, u);
1629 int f;
1630 for (f = 0; f < board->flen; f++)
1631 if (board->f[f] == m->coord)
1632 return board_play_f(board, m, f, u);
1634 if (DEBUGL(7))
1635 fprintf(stderr, "board_check: stone exists\n");
1636 return -1;
1640 board_play(struct board *board, struct move *m)
1642 return board_play_(board, m, NULL);
1646 board_quick_play(struct board *board, struct move *m, struct board_undo *u)
1648 int r = board_play_(board, m, u);
1649 #ifdef BOARD_UNDO_CHECKS
1650 if (r >= 0)
1651 board->quicked++;
1652 #endif
1653 return r;
1656 static inline void
1657 undo_merge(struct board *b, struct board_undo *u, struct move *m)
1659 coord_t coord = m->coord;
1660 group_t group = group_at(b, coord);
1661 struct undo_merge *merged = u->merged;
1663 // Others groups, in reverse order ...
1664 for (int i = u->nmerged - 1; i > 0; i--) {
1665 group_t old_group = merged[i].group;
1667 board_group_info(b, old_group) = merged[i].info;
1669 groupnext_at(b, group_base(group)) = groupnext_at(b, merged[i].last);
1670 groupnext_at(b, merged[i].last) = 0;
1672 #if 0
1673 printf("merged_group[%i]: (last: %s)", i, coord2sstr(merged[i].last, b));
1674 foreach_in_group(b, old_group) {
1675 printf("%s ", coord2sstr(c, b));
1676 } foreach_in_group_end;
1677 printf("\n");
1678 #endif
1680 foreach_in_group(b, old_group) {
1681 group_at(b, c) = old_group;
1682 } foreach_in_group_end;
1685 // Restore first group
1686 groupnext_at(b, u->inserted) = groupnext_at(b, coord);
1687 board_group_info(b, merged[0].group) = merged[0].info;
1689 #if 0
1690 printf("merged_group[0]: ");
1691 foreach_in_group(b, merged[0].group) {
1692 printf("%s ", coord2sstr(c, b));
1693 } foreach_in_group_end;
1694 printf("\n");
1695 #endif
1699 static inline void
1700 restore_enemies(struct board *b, struct board_undo *u, struct move *m)
1702 enum stone color = m->color;
1703 enum stone other_color = stone_other(m->color);
1705 struct undo_enemy *enemy = u->enemies;
1706 for (int i = 0; i < u->nenemies; i++) {
1707 group_t old_group = enemy[i].group;
1709 board_group_info(b, old_group) = enemy[i].info;
1711 coord_t *stones = enemy[i].stones;
1712 for (int j = 0; stones[j]; j++) {
1713 board_at(b, stones[j]) = other_color;
1714 group_at(b, stones[j]) = old_group;
1715 groupnext_at(b, stones[j]) = stones[j + 1];
1717 foreach_neighbor(b, stones[j], {
1718 inc_neighbor_count_at(b, c, other_color);
1721 // Update liberties of neighboring groups
1722 foreach_neighbor(b, stones[j], {
1723 if (board_at(b, c) != color)
1724 continue;
1725 group_t g = group_at(b, c);
1726 if (g == u->merged[0].group || g == u->merged[1].group || g == u->merged[2].group || g == u->merged[3].group)
1727 continue;
1728 board_group_rmlib(b, g, stones[j], u);
1734 static void
1735 board_undo_stone(struct board *b, struct board_undo *u, struct move *m)
1737 coord_t coord = m->coord;
1738 enum stone color = m->color;
1739 /* - update groups
1740 * - put captures back
1743 //printf("nmerged: %i\n", u->nmerged);
1745 // Restore merged groups
1746 if (u->nmerged)
1747 undo_merge(b, u, m);
1748 else // Single stone group undo
1749 memset(&board_group_info(b, group_at(b, coord)), 0, sizeof(struct group));
1751 board_at(b, coord) = S_NONE;
1752 group_at(b, coord) = 0;
1753 groupnext_at(b, coord) = u->next_at;
1755 foreach_neighbor(b, coord, {
1756 dec_neighbor_count_at(b, c, color);
1759 // Restore enemy groups
1760 if (u->nenemies) {
1761 b->captures[color] -= u->captures;
1762 restore_enemies(b, u, m);
1766 static inline void
1767 restore_suicide(struct board *b, struct board_undo *u, struct move *m)
1769 enum stone color = m->color;
1770 enum stone other_color = stone_other(m->color);
1772 struct undo_enemy *enemy = u->enemies;
1773 for (int i = 0; i < u->nenemies; i++) {
1774 group_t old_group = enemy[i].group;
1776 board_group_info(b, old_group) = enemy[i].info;
1778 coord_t *stones = enemy[i].stones;
1779 for (int j = 0; stones[j]; j++) {
1780 board_at(b, stones[j]) = other_color;
1781 group_at(b, stones[j]) = old_group;
1782 groupnext_at(b, stones[j]) = stones[j + 1];
1784 foreach_neighbor(b, stones[j], {
1785 inc_neighbor_count_at(b, c, other_color);
1788 // Update liberties of neighboring groups
1789 foreach_neighbor(b, stones[j], {
1790 if (board_at(b, c) != color)
1791 continue;
1792 group_t g = group_at(b, c);
1793 if (g == u->enemies[0].group || g == u->enemies[1].group ||
1794 g == u->enemies[2].group || g == u->enemies[3].group)
1795 continue;
1796 board_group_rmlib(b, g, stones[j], u);
1803 static void
1804 board_undo_suicide(struct board *b, struct board_undo *u, struct move *m)
1806 coord_t coord = m->coord;
1807 enum stone other_color = stone_other(m->color);
1809 // Pretend it's capture ...
1810 struct move m2 = { .coord = m->coord, .color = other_color };
1811 b->captures[other_color] -= u->captures;
1813 restore_suicide(b, u, &m2);
1815 undo_merge(b, u, m);
1817 board_at(b, coord) = S_NONE;
1818 group_at(b, coord) = 0;
1819 groupnext_at(b, coord) = u->next_at;
1821 foreach_neighbor(b, coord, {
1822 dec_neighbor_count_at(b, c, m->color);
1828 void
1829 board_quick_undo(struct board *b, struct move *m, struct board_undo *u)
1831 #ifdef BOARD_UNDO_CHECKS
1832 b->quicked--;
1833 #endif
1835 b->last_move = b->last_move2;
1836 b->last_move2 = u->last_move2;
1837 b->ko = u->ko;
1838 b->last_ko = u->last_ko;
1839 b->last_ko_age = u->last_ko_age;
1841 if (unlikely(is_pass(m->coord) || is_resign(m->coord)))
1842 return;
1844 b->moves--;
1846 if (likely(board_at(b, m->coord) == m->color))
1847 board_undo_stone(b, u, m);
1848 else if (board_at(b, m->coord) == S_NONE)
1849 board_undo_suicide(b, u, m);
1850 else
1851 assert(0); /* Anything else doesn't make sense */
1855 /* Undo, supported only for pass moves. This form of undo is required by KGS
1856 * to settle disputes on dead groups. See also fast_board_undo() */
1857 int board_undo(struct board *board)
1859 if (!is_pass(board->last_move.coord))
1860 return -1;
1861 if (board->rules == RULES_SIMING) {
1862 /* Return pass stone to the passing player. */
1863 board->captures[stone_other(board->last_move.color)]--;
1865 board->last_move = board->last_move2;
1866 board->last_move2 = board->last_move3;
1867 board->last_move3 = board->last_move4;
1868 if (board->last_ko_age == board->moves)
1869 board->ko = board->last_ko;
1870 return 0;
1873 static inline bool
1874 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1876 *coord = b->f[f];
1877 struct move m = { *coord, color };
1878 if (DEBUGL(6))
1879 fprintf(stderr, "trying random move %d: %d,%d %s %d\n", f, coord_x(*coord, b), coord_y(*coord, b), coord2sstr(*coord, b), board_is_valid_move(b, &m));
1880 if (unlikely(board_is_one_point_eye(b, *coord, color)) /* bad idea to play into one, usually */
1881 || !board_is_valid_move(b, &m)
1882 || (permit && !permit(permit_data, b, &m)))
1883 return false;
1884 if (m.coord == *coord) {
1885 return likely(board_play_f(b, &m, f, NULL) >= 0);
1886 } else {
1887 *coord = m.coord; // permit modified the coordinate
1888 return likely(board_play(b, &m) >= 0);
1892 void
1893 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1895 if (unlikely(b->flen == 0))
1896 goto pass;
1898 int base = fast_random(b->flen), f;
1899 for (f = base; f < b->flen; f++)
1900 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1901 return;
1902 for (f = 0; f < base; f++)
1903 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1904 return;
1906 pass:
1907 *coord = pass;
1908 struct move m = { pass, color };
1909 board_play(b, &m);
1913 bool
1914 board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color)
1916 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1918 /* XXX: We attempt false eye detection but we will yield false
1919 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1921 foreach_diag_neighbor(board, coord) {
1922 color_diag_libs[(enum stone) board_at(board, c)]++;
1923 } foreach_diag_neighbor_end;
1924 /* For false eye, we need two enemy stones diagonally in the
1925 * middle of the board, or just one enemy stone at the edge
1926 * or in the corner. */
1927 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1928 return color_diag_libs[stone_other(eye_color)] >= 2;
1931 bool
1932 board_is_one_point_eye(struct board *board, coord_t coord, enum stone eye_color)
1934 return board_is_eyelike(board, coord, eye_color)
1935 && !board_is_false_eyelike(board, coord, eye_color);
1938 enum stone
1939 board_get_one_point_eye(struct board *board, coord_t coord)
1941 if (board_is_one_point_eye(board, coord, S_WHITE))
1942 return S_WHITE;
1943 else if (board_is_one_point_eye(board, coord, S_BLACK))
1944 return S_BLACK;
1945 else
1946 return S_NONE;
1950 floating_t
1951 board_fast_score(struct board *board)
1953 int scores[S_MAX];
1954 memset(scores, 0, sizeof(scores));
1956 foreach_point(board) {
1957 enum stone color = board_at(board, c);
1958 if (color == S_NONE && board->rules != RULES_STONES_ONLY)
1959 color = board_get_one_point_eye(board, c);
1960 scores[color]++;
1961 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1962 } foreach_point_end;
1964 return board->komi + (board->rules != RULES_SIMING ? board->handicap : 0) + scores[S_WHITE] - scores[S_BLACK];
1967 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1969 /* One flood-fill iteration; returns true if next iteration
1970 * is required. */
1971 static bool
1972 board_tromp_taylor_iter(struct board *board, int *ownermap)
1974 bool needs_update = false;
1975 foreach_free_point(board) {
1976 /* Ignore occupied and already-dame positions. */
1977 assert(board_at(board, c) == S_NONE);
1978 if (board->rules == RULES_STONES_ONLY)
1979 ownermap[c] = 3;
1980 if (ownermap[c] == 3)
1981 continue;
1982 /* Count neighbors. */
1983 int nei[4] = {0};
1984 foreach_neighbor(board, c, {
1985 nei[ownermap[c]]++;
1987 /* If we have neighbors of both colors, or dame,
1988 * we are dame too. */
1989 if ((nei[1] && nei[2]) || nei[3]) {
1990 ownermap[c] = 3;
1991 /* Speed up the propagation. */
1992 foreach_neighbor(board, c, {
1993 if (board_at(board, c) == S_NONE)
1994 ownermap[c] = 3;
1996 needs_update = true;
1997 continue;
1999 /* If we have neighbors of one color, we are owned
2000 * by that color, too. */
2001 if (!ownermap[c] && (nei[1] || nei[2])) {
2002 int newowner = nei[1] ? 1 : 2;
2003 ownermap[c] = newowner;
2004 /* Speed up the propagation. */
2005 foreach_neighbor(board, c, {
2006 if (board_at(board, c) == S_NONE && !ownermap[c])
2007 ownermap[c] = newowner;
2009 needs_update = true;
2010 continue;
2012 } foreach_free_point_end;
2013 return needs_update;
2016 /* Tromp-Taylor Counting */
2017 floating_t
2018 board_official_score(struct board *board, struct move_queue *q)
2021 /* A point P, not colored C, is said to reach C, if there is a path of
2022 * (vertically or horizontally) adjacent points of P's color from P to
2023 * a point of color C.
2025 * A player's score is the number of points of her color, plus the
2026 * number of empty points that reach only her color. */
2028 int ownermap[board_size2(board)];
2029 int s[4] = {0};
2030 const int o[4] = {0, 1, 2, 0};
2031 foreach_point(board) {
2032 ownermap[c] = o[board_at(board, c)];
2033 s[board_at(board, c)]++;
2034 } foreach_point_end;
2036 if (q) {
2037 /* Process dead groups. */
2038 for (unsigned int i = 0; i < q->moves; i++) {
2039 foreach_in_group(board, q->move[i]) {
2040 enum stone color = board_at(board, c);
2041 ownermap[c] = o[stone_other(color)];
2042 s[color]--; s[stone_other(color)]++;
2043 } foreach_in_group_end;
2047 /* We need to special-case empty board. */
2048 if (!s[S_BLACK] && !s[S_WHITE])
2049 return board->komi;
2051 while (board_tromp_taylor_iter(board, ownermap))
2052 /* Flood-fill... */;
2054 int scores[S_MAX];
2055 memset(scores, 0, sizeof(scores));
2057 foreach_point(board) {
2058 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
2059 if (ownermap[c] == 3)
2060 continue;
2061 scores[ownermap[c]]++;
2062 } foreach_point_end;
2064 return board->komi + (board->rules != RULES_SIMING ? board->handicap : 0) + scores[S_WHITE] - scores[S_BLACK];
2067 bool
2068 board_set_rules(struct board *board, char *name)
2070 if (!strcasecmp(name, "japanese")) {
2071 board->rules = RULES_JAPANESE;
2072 } else if (!strcasecmp(name, "chinese")) {
2073 board->rules = RULES_CHINESE;
2074 } else if (!strcasecmp(name, "aga")) {
2075 board->rules = RULES_AGA;
2076 } else if (!strcasecmp(name, "new_zealand")) {
2077 board->rules = RULES_NEW_ZEALAND;
2078 } else if (!strcasecmp(name, "siming") || !strcasecmp(name, "simplified_ing")) {
2079 board->rules = RULES_SIMING;
2080 } else {
2081 return false;
2083 return true;