10 #include "playout/moggy.h"
13 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
17 bool ladders
, ladderassess
, borderladders
;
18 int lcapturerate
, capturerate
, patternrate
;
29 mq_nodup(struct move_queue
*q
)
31 if ((q
->moves
> 1 && q
->move
[q
->moves
- 2] == q
->move
[q
->moves
- 1])
32 || (q
->moves
> 2 && q
->move
[q
->moves
- 3] == q
->move
[q
->moves
- 1])
33 || (q
->moves
> 3 && q
->move
[q
->moves
- 4] == q
->move
[q
->moves
- 1]))
39 * X: black; O: white; .: empty; #: edge
40 * x: !black; o: !white; ?: any
42 * extra X: pattern valid only for one side;
43 * middle point ignored. */
45 static char moggy_patterns_src
[][11] = {
46 /* hane pattern - enclosing hane */
50 /* hane pattern - non-cutting hane */
54 /* hane pattern - magari */
58 /* hane pattern - thin hane */
62 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
66 /* cut1 pattern (kiri) - unprotected cut */
70 /* cut1 pattern (kiri) - peeped cut */
74 /* cut2 pattern (de) */
78 /* cut keima (not in Mogo) */
82 /* side pattern - chase */
86 /* side pattern - weirdness (SUSPICIOUS) */
90 /* side pattern - sagari (SUSPICIOUS) */
92 "x.x" /* Mogo has "x.?" */
93 "###" /* Mogo has "X" */,
94 /* side pattern - weirdcut (SUSPICIOUS) */
100 /* side pattern - cut (SUSPICIOUS) */
103 "###" /* Mogo has "X" */,
105 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
107 /* Hashtable: 2*8 bits (ignore middle point, 2 bits per intersection) */
108 /* Value: 0: no pattern, 1: black pattern, 2: white pattern, 3: both patterns */
109 static char moggy_patterns
[65536];
112 _record_pattern(char *table
, char *str
, int pat
, int fixed_color
)
114 /* Original color assignment */
115 table
[pat
] = fixed_color
? fixed_color
: 3;
116 //fprintf(stderr, "[%s] %04x %d\n", str, pat, fixed_color);
118 /* Reverse color assignment - achieved by swapping odd and even bits */
119 pat
= ((pat
>> 1) & 0x5555) | ((pat
& 0x5555) << 1);
120 table
[pat
] = fixed_color
? 2 - (fixed_color
== 2) : 3;
121 //fprintf(stderr, "[%s] %04x %d\n", str, pat, fixed_color);
125 _pat_vmirror(int pat
)
127 /* V mirror pattern; reverse order of 3-2-3 chunks */
128 return ((pat
& 0xfc00) >> 10) | (pat
& 0x03c0) | ((pat
& 0x003f) << 10);
132 _pat_hmirror(int pat
)
134 /* H mirror pattern; reverse order of 2-bit values within the chunks */
135 #define rev3(p) ((p >> 4) | (p & 0xc) | ((p & 0x3) << 4))
136 #define rev2(p) ((p >> 2) | ((p & 0x3) << 2))
137 return (rev3((pat
& 0xfc00) >> 10) << 10)
138 | (rev2((pat
& 0x03c0) >> 6) << 6)
139 | rev3((pat
& 0x003f));
147 /* Rotate by 90 degrees:
151 /* I'm too lazy to optimize this :) */
153 for (int i
= 0; i
< 8; i
++)
154 vals
[i
] = (pat
>> (i
* 2)) & 0x3;
156 vals2
[0] = vals
[5]; vals2
[1] = vals
[3]; vals2
[2] = vals
[0];
157 vals2
[3] = vals
[6]; vals2
[4] = vals
[1];
158 vals2
[5] = vals
[7]; vals2
[6] = vals
[4]; vals2
[7] = vals
[2];
160 for (int i
= 0; i
< 8; i
++)
161 p2
|= vals2
[i
] << (i
* 2);
166 _gen_pattern(char *table
, int pat
, char *src
, int srclen
, int fixed_color
)
168 for (; srclen
> 0; src
++, srclen
--) {
171 int patofs
= (srclen
> 5 ? srclen
- 1 : srclen
) - 1;
174 *src
= '.'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
175 *src
= 'X'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
176 *src
= 'O'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
177 *src
= '#'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
178 *src
= '?'; // for future recursions
181 *src
= '.'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
182 *src
= 'O'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
183 *src
= '#'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
184 *src
= 'x'; // for future recursions
187 *src
= '.'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
188 *src
= 'X'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
189 *src
= '#'; _gen_pattern(table
, pat
, src
, srclen
, fixed_color
);
190 *src
= 'o'; // for future recursions
192 case '.': /* 0 */ break;
193 case 'X': pat
|= S_BLACK
<< (patofs
* 2); break;
194 case 'O': pat
|= S_WHITE
<< (patofs
* 2); break;
195 case '#': pat
|= S_OFFBOARD
<< (patofs
* 2); break;
199 /* Original pattern, all transpositions and rotations */
200 _record_pattern(table
, src
- 9, pat
, fixed_color
);
201 _record_pattern(table
, src
- 9, _pat_vmirror(pat
), fixed_color
);
202 _record_pattern(table
, src
- 9, _pat_hmirror(pat
), fixed_color
);
203 _record_pattern(table
, src
- 9, _pat_vmirror(_pat_hmirror(pat
)), fixed_color
);
204 _record_pattern(table
, src
- 9, _pat_90rot(pat
), fixed_color
);
205 _record_pattern(table
, src
- 9, _pat_90rot(_pat_vmirror(pat
)), fixed_color
);
206 _record_pattern(table
, src
- 9, _pat_90rot(_pat_hmirror(pat
)), fixed_color
);
207 _record_pattern(table
, src
- 9, _pat_90rot(_pat_vmirror(_pat_hmirror(pat
))), fixed_color
);
210 static void __attribute__((constructor
))
213 for (int i
= 0; i
< moggy_patterns_src_n
; i
++) {
215 switch (moggy_patterns_src
[i
][9]) {
216 case 'X': fixed_color
= S_BLACK
; break;
217 case 'O': fixed_color
= S_WHITE
; break;
219 //fprintf(stderr, "** %s **\n", moggy_patterns_src[i]);
220 _gen_pattern(moggy_patterns
, 0, moggy_patterns_src
[i
], 9, fixed_color
);
225 /* Check if we match any pattern centered on given move. */
227 test_pattern_here(struct playout_policy
*p
, char *hashtable
,
228 struct board
*b
, struct move
*m
)
231 int x
= coord_x(m
->coord
, b
), y
= coord_y(m
->coord
, b
);
232 pat
|= (board_atxy(b
, x
- 1, y
- 1) << 14)
233 | (board_atxy(b
, x
, y
- 1) << 12)
234 | (board_atxy(b
, x
+ 1, y
- 1) << 10);
235 pat
|= (board_atxy(b
, x
- 1, y
) << 8)
236 | (board_atxy(b
, x
+ 1, y
) << 6);
237 pat
|= (board_atxy(b
, x
- 1, y
+ 1) << 4)
238 | (board_atxy(b
, x
, y
+ 1) << 2)
239 | (board_atxy(b
, x
+ 1, y
+ 1));
240 //fprintf(stderr, "(%d,%d) hashtable[%04x] = %d\n", x, y, pat, hashtable[pat]);
241 return (hashtable
[pat
] & m
->color
) && !is_selfatari(b
, m
->color
, m
->coord
);
245 apply_pattern_here(struct playout_policy
*p
, char *hashtable
,
246 struct board
*b
, struct move
*m
, struct move_queue
*q
)
248 if (test_pattern_here(p
, hashtable
, b
, m
))
249 q
->move
[q
->moves
++] = m
->coord
;
252 /* Check if we match any pattern around given move (with the other color to play). */
254 apply_pattern(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
256 //struct moggy_policy *pp = p->data;
260 /* Suicides do not make any patterns and confuse us. */
261 if (board_at(b
, m
->coord
) == S_NONE
|| board_at(b
, m
->coord
) == S_OFFBOARD
)
264 foreach_neighbor(b
, m
->coord
, {
265 struct move m2
; m2
.coord
= c
; m2
.color
= stone_other(m
->color
);
266 if (board_at(b
, c
) == S_NONE
)
267 apply_pattern_here(p
, moggy_patterns
, b
, &m2
, &q
);
269 foreach_diag_neighbor(b
, m
->coord
) {
270 struct move m2
; m2
.coord
= c
; m2
.color
= stone_other(m
->color
);
271 if (board_at(b
, c
) == S_NONE
)
272 apply_pattern_here(p
, moggy_patterns
, b
, &m2
, &q
);
273 } foreach_diag_neighbor_end
;
276 fprintf(stderr
, "Pattern candidate moves: ");
277 for (int i
= 0; i
< q
.moves
; i
++) {
278 fprintf(stderr
, "%s ", coord2sstr(q
.move
[i
], b
));
280 fprintf(stderr
, "\n");
283 int i
= fast_random(q
.moves
);
284 return q
.moves
? q
.move
[i
] : pass
;
289 /* Is this ladder breaker friendly for the one who catches ladder. */
291 ladder_catcher(struct board
*b
, int x
, int y
, enum stone laddered
)
293 enum stone breaker
= board_atxy(b
, x
, y
);
294 return breaker
== stone_other(laddered
) || breaker
== S_OFFBOARD
;
298 ladder_catches(struct playout_policy
*p
, struct board
*b
, coord_t coord
, group_t laddered
)
300 struct moggy_policy
*pp
= p
->data
;
302 /* This is very trivial and gets a lot of corner cases wrong.
303 * We need this to be just very fast. One important point is
304 * that we sometimes might not notice a ladder but if we do,
305 * it should always work; thus we can use this for strong
306 * negative hinting safely. */
307 //fprintf(stderr, "ladder check\n");
309 enum stone lcolor
= board_at(b
, group_base(laddered
));
310 int x
= coord_x(coord
, b
), y
= coord_y(coord
, b
);
312 /* First, special-case first-line "ladders". This is a huge chunk
313 * of ladders we actually meet and want to play. */
314 if (pp
->borderladders
315 && neighbor_count_at(b
, coord
, S_OFFBOARD
) == 1
316 && neighbor_count_at(b
, coord
, lcolor
) == 1) {
318 fprintf(stderr
, "border ladder\n");
319 /* Direction along border; xd is horiz. border, yd vertical. */
321 if (board_atxy(b
, x
+ 1, y
) == S_OFFBOARD
|| board_atxy(b
, x
- 1, y
) == S_OFFBOARD
)
325 /* Direction from the border; -1 is above/left, 1 is below/right. */
326 int dd
= (board_atxy(b
, x
+ yd
, y
+ xd
) == S_OFFBOARD
) ? 1 : -1;
328 fprintf(stderr
, "xd %d yd %d dd %d\n", xd
, yd
, dd
);
334 /* This is normally caught, unless we have friends both above
336 if (board_atxy(b
, x
+ xd
* 2, y
+ yd
* 2) == lcolor
337 && board_atxy(b
, x
- xd
* 2, y
- yd
* 2) == lcolor
)
339 /* ...or can't block where we need because of shortage
341 int libs1
= board_group_info(b
, group_atxy(b
, x
+ xd
- yd
* dd
, y
+ yd
- xd
* dd
)).libs
;
342 int libs2
= board_group_info(b
, group_atxy(b
, x
- xd
- yd
* dd
, y
- yd
- xd
* dd
)).libs
;
344 fprintf(stderr
, "libs1 %d libs2 %d\n", libs1
, libs2
);
345 if (libs1
< 2 && libs2
< 2)
347 if (board_atxy(b
, x
+ xd
* 2, y
+ yd
* 2) == lcolor
&& libs1
< 3)
349 if (board_atxy(b
, x
- xd
* 2, y
- yd
* 2) == lcolor
&& libs2
< 3)
354 /* Figure out the ladder direction */
356 xd
= board_atxy(b
, x
+ 1, y
) == S_NONE
? 1 : board_atxy(b
, x
- 1, y
) == S_NONE
? -1 : 0;
357 yd
= board_atxy(b
, x
, y
+ 1) == S_NONE
? 1 : board_atxy(b
, x
, y
- 1) == S_NONE
? -1 : 0;
359 /* We do only tight ladders, not loose ladders. Furthermore,
360 * the ladders need to be simple:
362 * c O X supported . c O unsupported
366 /* For given (xd,yd), we have two possibilities where to move
367 * next. Consider (-1,1):
372 if (!xd
|| !yd
|| !(ladder_catcher(b
, x
- xd
, y
, lcolor
) ^ ladder_catcher(b
, x
, y
- yd
, lcolor
))) {
373 /* Silly situation, probably non-simple ladder or suicide. */
374 /* TODO: In case of basic non-simple ladder, play out both variants. */
376 fprintf(stderr
, "non-simple ladder\n");
380 #define ladder_check(xd1_, yd1_, xd2_, yd2_) \
381 if (board_atxy(b, x, y) != S_NONE) { \
382 /* Did we hit a stone when playing out ladder? */ \
383 if (ladder_catcher(b, x, y, lcolor)) \
384 return true; /* ladder works */ \
385 if (board_group_info(b, group_atxy(b, x, y)).lib[0] > 0) \
386 return false; /* friend that's not in atari himself */ \
388 /* No. So we are at new position. \
389 * We need to check indirect ladder breakers. */ \
391 * . x o O 1 <- only at O we can check for o at 2 \
392 * x o o x . otherwise x at O would be still deadly \
394 * We check for o and x at 1, these are vital. \
395 * We check only for o at 2; x at 2 would mean we \
396 * need to fork (one step earlier). */ \
397 enum stone s1 = board_atxy(b, x + (xd1_), y + (yd1_)); \
398 if (s1 == lcolor) return false; \
399 if (s1 == stone_other(lcolor)) return true; \
400 enum stone s2 = board_atxy(b, x + (xd2_), y + (yd2_)); \
401 if (s2 == lcolor) return false; \
403 #define ladder_horiz do { if (PLDEBUGL(6)) fprintf(stderr, "%d,%d horiz step %d\n", x, y, xd); x += xd; ladder_check(xd, 0, -2 * xd, yd); } while (0)
404 #define ladder_vert do { if (PLDEBUGL(6)) fprintf(stderr, "%d,%d vert step %d\n", x, y, yd); y += yd; ladder_check(0, yd, xd, -2 * yd); } while (0)
406 if (ladder_catcher(b
, x
- xd
, y
, lcolor
))
416 group_atari_check(struct playout_policy
*p
, struct board
*b
, group_t group
, struct move_queue
*q
)
418 struct moggy_policy
*pp
= p
->data
;
420 /* Do not bother with kos. */
421 if (group_is_onestone(b
, group
))
424 enum stone color
= board_at(b
, group_base(group
));
425 coord_t lib
= board_group_info(b
, group
).lib
[0];
427 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
429 fprintf(stderr
, "atariiiiiiiii %s of color %d\n", coord2sstr(lib
, b
), color
);
430 assert(board_at(b
, lib
) == S_NONE
);
432 /* Can we capture some neighbor? */
433 foreach_in_group(b
, group
) {
434 foreach_neighbor(b
, c
, {
435 if (board_at(b
, c
) != stone_other(color
)
436 || board_group_info(b
, group_at(b
, c
)).libs
> 1)
439 fprintf(stderr
, "can capture group %d\n", group_at(b
, c
));
440 /* If we are saving our group, capture! */
441 if (b
->last_move
.color
== stone_other(color
))
442 q
->move
[q
->moves
++] = board_group_info(b
, group_at(b
, c
)).lib
[0];
443 else /* If we chase the group, capture it now! */
444 q
->move
[q
->moves
++] = lib
;
445 /* Make sure capturing the group will actually
446 * expand our liberties if we are filling our
448 if (q
->move
[q
->moves
- 1] == lib
&& is_selfatari(b
, color
, lib
))
453 } foreach_in_group_end
;
455 /* Do not suicide... */
456 if (is_selfatari(b
, color
, lib
))
459 fprintf(stderr
, "...escape route valid\n");
461 /* ...or play out ladders. */
462 if (pp
->ladders
&& ladder_catches(p
, b
, lib
, group
)) {
466 fprintf(stderr
, "...no ladder\n");
468 q
->move
[q
->moves
++] = lib
;
473 global_atari_check(struct playout_policy
*p
, struct board
*b
)
481 int g_base
= fast_random(b
->clen
);
482 for (int g
= g_base
; g
< b
->clen
; g
++) {
483 group_atari_check(p
, b
, group_at(b
, group_base(b
->c
[g
])), &q
);
485 return q
.move
[fast_random(q
.moves
)];
487 for (int g
= 0; g
< g_base
; g
++) {
488 group_atari_check(p
, b
, group_at(b
, group_base(b
->c
[g
])), &q
);
490 return q
.move
[fast_random(q
.moves
)];
496 local_atari_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
501 /* Did the opponent play a self-atari? */
502 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 1) {
503 group_atari_check(p
, b
, group_at(b
, m
->coord
), &q
);
506 foreach_neighbor(b
, m
->coord
, {
507 group_t g
= group_at(b
, c
);
508 if (!g
|| board_group_info(b
, g
).libs
!= 1)
510 group_atari_check(p
, b
, g
, &q
);
514 fprintf(stderr
, "Local atari candidate moves: ");
515 for (int i
= 0; i
< q
.moves
; i
++) {
516 fprintf(stderr
, "%s ", coord2sstr(q
.move
[i
], b
));
518 fprintf(stderr
, "\n");
521 int i
= fast_random(q
.moves
);
522 return q
.moves
? q
.move
[i
] : pass
;
526 playout_moggy_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
528 struct moggy_policy
*pp
= p
->data
;
532 board_print(b
, stderr
);
535 if (!is_pass(b
->last_move
.coord
)) {
536 /* Local group in atari? */
537 if (pp
->lcapturerate
> fast_random(100)) {
538 c
= local_atari_check(p
, b
, &b
->last_move
);
543 /* Check for patterns we know */
544 if (pp
->patternrate
> fast_random(100)) {
545 c
= apply_pattern(p
, b
, &b
->last_move
);
553 /* Any groups in atari? */
554 if (pp
->capturerate
> fast_random(100)) {
555 c
= global_atari_check(p
, b
);
564 playout_moggy_assess(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
566 struct moggy_policy
*pp
= p
->data
;
568 if (is_pass(m
->coord
))
572 fprintf(stderr
, "ASSESS of %s:\n", coord2sstr(m
->coord
, b
));
573 board_print(b
, stderr
);
576 /* Are we dealing with atari? */
577 if (pp
->lcapturerate
|| pp
->capturerate
) {
580 foreach_neighbor(b
, m
->coord
, {
581 group_t g
= group_at(b
, c
);
582 if (!g
|| board_group_info(b
, g
).libs
!= 1)
585 struct move_queue q
; q
.moves
= 0;
586 group_atari_check(p
, b
, g
, &q
);
588 if (q
.move
[q
.moves
] == m
->coord
) {
590 fprintf(stderr
, "1.0: atari\n");
594 /* _Never_ play here if this move plays out
595 * a caught ladder. (Unless it captures another
597 if (pp
->ladderassess
&& ladder_catches(p
, b
, m
->coord
, g
)) {
598 /* Note that the opposite is not guarded against;
599 * we do not advise against capturing a laddered
600 * group (but we don't encourage it either). Such
601 * a move can simplify tactical situations if we
603 if (m
->color
== board_at(b
, c
))
610 fprintf(stderr
, "0.0: ladder\n");
616 if (pp
->patternrate
) {
617 if (test_pattern_here(p
, moggy_patterns
, b
, m
)) {
619 fprintf(stderr
, "1.0: pattern\n");
628 playout_moggy_permit(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
630 struct moggy_policy
*pp
= p
->data
;
632 /* The idea is simple for now - never allow self-atari moves.
633 * They suck in general, but this also permits us to actually
634 * handle seki in the playout stage. */
635 /* FIXME: We must allow self-atari in some basic nakade shapes. */
637 if (is_selfatari(b
, m
->color
, m
->coord
))
638 fprintf(stderr
, "__ Prohibiting self-atari %s %s\n", stone2str(m
->color
), coord2sstr(m
->coord
, b
));
640 return fast_random(100) >= pp
->selfatarirate
|| !is_selfatari(b
, m
->color
, m
->coord
);
644 struct playout_policy
*
645 playout_moggy_init(char *arg
)
647 struct playout_policy
*p
= calloc(1, sizeof(*p
));
648 struct moggy_policy
*pp
= calloc(1, sizeof(*pp
));
650 p
->choose
= playout_moggy_choose
;
651 p
->assess
= playout_moggy_assess
;
652 p
->permit
= playout_moggy_permit
;
654 pp
->lcapturerate
= 90;
655 pp
->capturerate
= 90;
656 pp
->patternrate
= 90;
657 pp
->selfatarirate
= 90;
658 pp
->ladders
= pp
->borderladders
= true;
659 pp
->ladderassess
= true;
662 char *optspec
, *next
= arg
;
665 next
+= strcspn(next
, ":");
666 if (*next
) { *next
++ = 0; } else { *next
= 0; }
668 char *optname
= optspec
;
669 char *optval
= strchr(optspec
, '=');
670 if (optval
) *optval
++ = 0;
672 if (!strcasecmp(optname
, "lcapturerate") && optval
) {
673 pp
->lcapturerate
= atoi(optval
);
674 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
675 pp
->capturerate
= atoi(optval
);
676 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
677 pp
->patternrate
= atoi(optval
);
678 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
679 pp
->selfatarirate
= atoi(optval
);
680 } else if (!strcasecmp(optname
, "ladders")) {
681 pp
->ladders
= optval
&& *optval
== '0' ? false : true;
682 } else if (!strcasecmp(optname
, "borderladders")) {
683 pp
->borderladders
= optval
&& *optval
== '0' ? false : true;
684 } else if (!strcasecmp(optname
, "ladderassess")) {
685 pp
->ladderassess
= optval
&& *optval
== '0' ? false : true;
687 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);