Moggy ko check: Aside of connecting the ko, also consider countercapturing a neighbor
[pachi/json.git] / playout / moggy.c
blobc0b2fa63d53e2b570facddf314385071aa5a5cde
1 /* Heuristical playout (and tree prior) policy modelled primarily after
2 * the description of the Mogo engine. */
4 #include <assert.h>
5 #include <math.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 #define DEBUG
10 #include "board.h"
11 #include "debug.h"
12 #include "joseki/base.h"
13 #include "mq.h"
14 #include "pattern3.h"
15 #include "playout.h"
16 #include "playout/moggy.h"
17 #include "random.h"
18 #include "tactics/1lib.h"
19 #include "tactics/2lib.h"
20 #include "tactics/ladder.h"
21 #include "tactics/selfatari.h"
22 #include "uct/prior.h"
24 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
27 /* In case "seqchoose" move picker is enabled (i.e. no "fullchoose"
28 * parameter passed), we stochastically apply fixed set of decision
29 * rules in given order.
31 * In "fullchoose" mode, we instead build a move queue of variously
32 * tagged candidates, then consider a probability distribution over
33 * them and pick a move from that. */
35 /* Move queue tags. Some may be even undesirable - these moves then
36 * receive a penalty; penalty tags should be used only when it is
37 * certain the move would be considered anyway. */
38 enum mq_tag {
39 MQ_KO = 0,
40 MQ_LATARI,
41 MQ_L2LIB,
42 MQ_PAT3,
43 MQ_GATARI,
44 MQ_JOSEKI,
45 MQ_MAX
49 /* Note that the context can be shared by multiple threads! */
51 struct moggy_policy {
52 unsigned int lcapturerate, atarirate, capturerate, patternrate, korate, josekirate;
53 unsigned int selfatarirate, alwaysccaprate;
54 unsigned int fillboardtries;
55 int koage;
56 /* Whether to look for patterns around second-to-last move. */
57 bool pattern2;
58 /* Whether, when self-atari attempt is detected, to play the other
59 * group's liberty if that is non-self-atari. */
60 bool selfatari_other;
61 /* Whether to always pick from moves capturing all groups in
62 * global_atari_check(). */
63 bool capcheckall;
64 /* 2lib settings: */
65 bool atari_def_no_hopeless;
66 bool atari_miaisafe;
68 struct joseki_dict *jdict;
69 struct pattern3s patterns;
71 /* Gamma values for queue tags - correspond to probabilities. */
72 /* XXX: Tune. */
73 double mq_prob[MQ_MAX], tenuki_prob;
77 static char moggy_patterns_src[][11] = {
78 /* hane pattern - enclosing hane */
79 "XOX"
80 "..."
81 "???",
82 /* hane pattern - non-cutting hane */
83 "YO."
84 "..."
85 "?.?",
86 /* hane pattern - magari */
87 "XO?"
88 "X.."
89 "x.?",
90 /* hane pattern - thin hane */
91 "XOO"
92 "..."
93 "?.?" "X",
94 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
95 ".Q."
96 "Y.."
97 "...",
98 /* cut1 pattern (kiri) - unprotected cut */
99 "XO?"
100 "O.o"
101 "?o?",
102 /* cut1 pattern (kiri) - peeped cut */
103 "XO?"
104 "O.X"
105 "???",
106 /* cut2 pattern (de) */
107 "?X?"
108 "O.O"
109 "ooo",
110 /* cut keima (not in Mogo) */
111 "OX?"
112 "o.O"
113 "???", /* o?? has some pathological tsumego cases */
114 /* side pattern - chase */
115 "X.?"
116 "O.?"
117 "##?",
118 /* side pattern - block side cut */
119 "OX?"
120 "X.O"
121 "###",
122 /* side pattern - block side connection */
123 "?X?"
124 "x.O"
125 "###",
126 /* side pattern - sagari (SUSPICIOUS) */
127 "?XQ"
128 "x.x" /* Mogo has "x.?" */
129 "###" /* Mogo has "X" */,
130 /* side pattern - throw-in (SUSPICIOUS) */
131 #if 0
132 "?OX"
133 "o.O"
134 "?##" "X",
135 #endif
136 /* side pattern - cut (SUSPICIOUS) */
137 "?OY"
138 "Y.O"
139 "###" /* Mogo has "X" */,
140 /* side pattern - eye piercing:
141 * # O O O .
142 * # O . O .
143 * # . . . .
144 * # # # # # */
145 #if 0
146 "Oxx"
147 "..."
148 "###",
149 #endif
151 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
153 static inline bool
154 test_pattern3_here(struct playout_policy *p, struct board *b, struct move *m)
156 struct moggy_policy *pp = p->data;
157 /* Check if 3x3 pattern is matched by given move... */
158 if (!pattern3_move_here(&pp->patterns, b, m))
159 return false;
160 /* ...and the move is not obviously stupid. */
161 if (is_bad_selfatari(b, m->color, m->coord))
162 return false;
163 /* Ladder moves are stupid. */
164 group_t atari_neighbor = board_get_atari_neighbor(b, m->coord, m->color);
165 if (atari_neighbor && is_ladder(b, m->coord, atari_neighbor))
166 return false;
167 return true;
170 static void
171 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q)
173 struct move m2 = { .coord = c, .color = color };
174 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2))
175 mq_add(q, c, 1<<MQ_PAT3);
178 /* Check if we match any pattern around given move (with the other color to play). */
179 static void
180 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q)
182 /* Suicides do not make any patterns and confuse us. */
183 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
184 return;
186 foreach_8neighbor(b, m->coord) {
187 apply_pattern_here(p, b, c, stone_other(m->color), q);
188 } foreach_8neighbor_end;
190 if (mm) { /* Second move for pattern searching */
191 foreach_8neighbor(b, mm->coord) {
192 if (coord_is_8adjecent(m->coord, c, b))
193 continue;
194 apply_pattern_here(p, b, c, stone_other(m->color), q);
195 } foreach_8neighbor_end;
198 if (PLDEBUGL(5))
199 mq_print(q, b, "Pattern");
203 static void
204 ko_check(struct playout_policy *p, struct board *b, struct move *m, enum stone to_play, struct move_queue *q)
206 if (!board_is_valid_play(b, to_play, m->coord)) {
207 /* The opponent has closed the ko. */
208 return;
211 if (!is_bad_selfatari(b, to_play, m->coord))
212 mq_add(q, m->coord, 1<<MQ_KO);
214 /* If we are not re-taking the ko, aside of connecting it,
215 * it may be also good idea to close it by capturing something
216 * else. Look for our stones that play the ko. */
217 coord_t ko[4]; int ko_n = 0;
218 foreach_neighbor(b, m->coord, {
219 group_t g = group_at(b, c);
220 if (board_at(b, c) != to_play || board_group_info(b, g).libs != 1)
221 continue;
222 ko[ko_n++] = c;
225 /* Try to counter-capture a neighbor of our ko stone. */
226 for (int i = 0; i < ko_n; i++) {
227 foreach_neighbor(b, ko[i], {
228 group_t g = group_at(b, c);
229 if (board_at(b, c) != stone_other(to_play) || board_group_info(b, g).libs != 1)
230 continue;
231 coord_t lib = board_group_info(b, g).lib[0];
232 if (!is_bad_selfatari(b, to_play, lib)) {
233 mq_add(q, lib, 1<<MQ_KO);
234 mq_nodup(q);
239 if (PLDEBUGL(5))
240 mq_print(q, b, "Ko");
243 static void
244 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
246 struct moggy_policy *pp = p->data;
247 if (!pp->jdict)
248 return;
250 for (int i = 0; i < 4; i++) {
251 hash_t h = b->qhash[i] & joseki_hash_mask;
252 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
253 if (!cc) continue;
254 for (; !is_pass(*cc); cc++) {
255 if (coord_quadrant(*cc, b) != i)
256 continue;
257 mq_add(q, *cc, 1<<MQ_JOSEKI);
261 if (q->moves > 0 && PLDEBUGL(5))
262 mq_print(q, b, "Joseki");
265 static void
266 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
268 if (b->clen == 0)
269 return;
271 struct moggy_policy *pp = p->data;
272 if (pp->capcheckall) {
273 for (int g = 0; g < b->clen; g++)
274 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
275 if (PLDEBUGL(5))
276 mq_print(q, b, "Global atari");
277 return;
280 int g_base = fast_random(b->clen);
281 for (int g = g_base; g < b->clen; g++) {
282 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
283 if (q->moves > 0) {
284 /* XXX: Try carrying on. */
285 if (PLDEBUGL(5))
286 mq_print(q, b, "Global atari");
287 return;
290 for (int g = 0; g < g_base; g++) {
291 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
292 if (q->moves > 0) {
293 /* XXX: Try carrying on. */
294 if (PLDEBUGL(5))
295 mq_print(q, b, "Global atari");
296 return;
299 return;
302 static void
303 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
305 struct moggy_policy *pp = p->data;
307 /* Did the opponent play a self-atari? */
308 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
309 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, 1<<MQ_LATARI);
312 foreach_neighbor(b, m->coord, {
313 group_t g = group_at(b, c);
314 if (!g || board_group_info(b, g).libs != 1)
315 continue;
316 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, 1<<MQ_LATARI);
319 if (PLDEBUGL(5))
320 mq_print(q, b, "Local atari");
324 static void
325 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
327 struct moggy_policy *pp = p->data;
329 /* Does the opponent have just two liberties? */
330 if (board_group_info(b, group_at(b, m->coord)).libs == 2) {
331 group_2lib_check(b, group_at(b, m->coord), stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
332 #if 0
333 /* We always prefer to take off an enemy chain liberty
334 * before pulling out ourselves. */
335 /* XXX: We aren't guaranteed to return to that group
336 * later. */
337 if (q->moves)
338 return q->move[fast_random(q->moves)];
339 #endif
342 /* Then he took a third liberty from neighboring chain? */
343 foreach_neighbor(b, m->coord, {
344 group_t g = group_at(b, c);
345 if (!g || board_group_info(b, g).libs != 2)
346 continue;
347 group_2lib_check(b, g, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
350 if (PLDEBUGL(5))
351 mq_print(q, b, "Local 2lib");
354 coord_t
355 fillboard_check(struct playout_policy *p, struct board *b)
357 struct moggy_policy *pp = p->data;
358 unsigned int fbtries = b->flen / 8;
359 if (pp->fillboardtries < fbtries)
360 fbtries = pp->fillboardtries;
362 for (unsigned int i = 0; i < fbtries; i++) {
363 coord_t coord = b->f[fast_random(b->flen)];
364 if (immediate_liberty_count(b, coord) != 4)
365 continue;
366 foreach_diag_neighbor(b, coord) {
367 if (board_at(b, c) != S_NONE)
368 goto next_try;
369 } foreach_diag_neighbor_end;
370 return coord;
371 next_try:;
373 return pass;
376 coord_t
377 playout_moggy_seqchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
379 struct moggy_policy *pp = p->data;
381 if (PLDEBUGL(5))
382 board_print(b, stderr);
384 /* Ko fight check */
385 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
386 && b->moves - b->last_ko_age < pp->koage
387 && pp->korate > fast_random(100)) {
388 struct move_queue q; q.moves = 0;
389 ko_check(p, b, &b->last_ko, to_play, &q);
390 if (q.moves > 0)
391 return mq_pick(&q);
394 /* Local checks */
395 if (!is_pass(b->last_move.coord)) {
396 /* Local group in atari? */
397 if (pp->lcapturerate > fast_random(100)) {
398 struct move_queue q; q.moves = 0;
399 local_atari_check(p, b, &b->last_move, &q);
400 if (q.moves > 0)
401 return mq_pick(&q);
404 /* Local group can be PUT in atari? */
405 if (pp->atarirate > fast_random(100)) {
406 struct move_queue q; q.moves = 0;
407 local_2lib_check(p, b, &b->last_move, &q);
408 if (q.moves > 0)
409 return mq_pick(&q);
412 /* Check for patterns we know */
413 if (pp->patternrate > fast_random(100)) {
414 struct move_queue q; q.moves = 0;
415 apply_pattern(p, b, &b->last_move,
416 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
417 &q);
418 if (q.moves > 0)
419 return mq_pick(&q);
423 /* Global checks */
425 /* Any groups in atari? */
426 if (pp->capturerate > fast_random(100)) {
427 struct move_queue q; q.moves = 0;
428 global_atari_check(p, b, to_play, &q);
429 if (q.moves > 0)
430 return mq_pick(&q);
433 /* Joseki moves? */
434 if (pp->josekirate > fast_random(100)) {
435 struct move_queue q; q.moves = 0;
436 joseki_check(p, b, to_play, &q);
437 if (q.moves > 0)
438 return mq_pick(&q);
441 /* Fill board */
442 if (pp->fillboardtries > 0) {
443 coord_t c = fillboard_check(p, b);
444 if (!is_pass(c))
445 return c;
448 return pass;
451 /* Pick a move from queue q, giving different likelihoods to moves
452 * based on their tags. */
453 coord_t
454 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
456 struct moggy_policy *pp = p->data;
458 /* First, merge all entries for a move. */
459 /* We use a naive O(N^2) since the average length of the queue
460 * is about 1.4. */
461 for (unsigned int i = 0; i < q->moves; i++) {
462 for (unsigned int j = i + 1; j < q->moves; j++) {
463 if (q->move[i] != q->move[j])
464 continue;
465 q->tag[i] |= q->tag[j];
466 q->moves--;
467 q->tag[j] = q->tag[q->moves];
468 q->move[j] = q->move[q->moves];
472 /* Now, construct a probdist. */
473 fixp_t total = 0;
474 fixp_t pd[q->moves];
475 for (unsigned int i = 0; i < q->moves; i++) {
476 double val = 1.0;
477 assert(q->tag[i] != 0);
478 for (int j = 0; j < MQ_MAX; j++)
479 if (q->tag[i] & (1<<j)) {
480 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
481 val *= pp->mq_prob[j];
483 pd[i] = double_to_fixp(val);
484 total += pd[i];
486 total += double_to_fixp(pp->tenuki_prob);
488 /* Finally, pick a move! */
489 fixp_t stab = fast_irandom(total);
490 for (unsigned int i = 0; i < q->moves; i++) {
491 //fprintf(stderr, "%s(%x) %f (%f/%f)\n", coord2sstr(q->move[i], b), q->tag[i], fixp_to_double(stab), fixp_to_double(pd[i]), fixp_to_double(total));
492 if (stab < pd[i])
493 return q->move[i];
494 stab -= pd[i];
497 /* Tenuki. */
498 assert(stab < double_to_fixp(pp->tenuki_prob));
499 return pass;
502 coord_t
503 playout_moggy_fullchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
505 struct moggy_policy *pp = p->data;
506 struct move_queue q; q.moves = 0;
508 if (PLDEBUGL(5))
509 board_print(b, stderr);
511 /* Ko fight check */
512 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
513 && b->moves - b->last_ko_age < pp->koage) {
514 ko_check(p, b, &b->last_ko, to_play, &q);
517 /* Local checks */
518 if (!is_pass(b->last_move.coord)) {
519 /* Local group in atari? */
520 local_atari_check(p, b, &b->last_move, &q);
522 /* Local group can be PUT in atari? */
523 local_2lib_check(p, b, &b->last_move, &q);
525 /* Check for patterns we know */
526 apply_pattern(p, b, &b->last_move,
527 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
528 &q);
531 /* Global checks */
533 /* Any groups in atari? */
534 global_atari_check(p, b, to_play, &q);
536 /* Joseki moves? */
537 joseki_check(p, b, to_play, &q);
539 #if 0
540 /* Average length of the queue is 1.4 move. */
541 printf("MQL %d ", q.moves);
542 for (unsigned int i = 0; i < q.moves; i++)
543 printf("%s ", coord2sstr(q.move[i], b));
544 printf("\n");
545 #endif
547 if (q.moves > 0)
548 return mq_tagged_choose(p, b, to_play, &q);
550 /* Fill board */
551 if (pp->fillboardtries > 0) {
552 coord_t c = fillboard_check(p, b);
553 if (!is_pass(c))
554 return c;
557 return pass;
561 void
562 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
564 struct moggy_policy *pp = p->data;
565 struct board *b = map->b;
566 struct move_queue q; q.moves = 0;
568 if (board_group_info(b, g).libs > 2)
569 return;
571 if (PLDEBUGL(5)) {
572 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
573 board_print(b, stderr);
576 if (board_group_info(b, g).libs == 2) {
577 if (!pp->atarirate)
578 return;
579 group_2lib_check(b, g, map->to_play, &q, 0, pp->atari_miaisafe, pp->atari_def_no_hopeless);
580 while (q.moves--) {
581 coord_t coord = q.move[q.moves];
582 if (PLDEBUGL(5))
583 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
584 int assess = games / 2;
585 add_prior_value(map, coord, 1, assess);
587 return;
590 /* This group, sir, is in atari! */
592 coord_t ladder = pass;
593 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, 0);
594 while (q.moves--) {
595 coord_t coord = q.move[q.moves];
597 /* _Never_ play here if this move plays out
598 * a caught ladder. */
599 if (coord == ladder && !board_playing_ko_threat(b)) {
600 /* Note that the opposite is not guarded against;
601 * we do not advise against capturing a laddered
602 * group (but we don't encourage it either). Such
603 * a move can simplify tactical situations if we
604 * can afford it. */
605 if (map->to_play != board_at(b, g))
606 continue;
607 /* FIXME: We give the malus even if this move
608 * captures another group. */
609 if (PLDEBUGL(5))
610 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
611 add_prior_value(map, coord, 0, games);
612 continue;
615 if (!pp->capturerate && !pp->lcapturerate)
616 continue;
618 if (PLDEBUGL(5))
619 fprintf(stderr, "1.0: atari %s\n", coord2sstr(coord, b));
620 int assess = games * 2;
621 add_prior_value(map, coord, 1, assess);
625 void
626 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
628 struct moggy_policy *pp = p->data;
629 struct board *b = map->b;
631 if (PLDEBUGL(5)) {
632 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
633 board_print(b, stderr);
636 /* Is this move a self-atari? */
637 if (pp->selfatarirate) {
638 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
639 if (PLDEBUGL(5))
640 fprintf(stderr, "0.0: self-atari\n");
641 add_prior_value(map, coord, 0, games);
642 if (!pp->selfatari_other)
643 return;
644 /* If we can play on the other liberty of the
645 * endangered group, do! */
646 coord = selfatari_cousin(b, map->to_play, coord);
647 if (is_pass(coord))
648 return;
649 if (PLDEBUGL(5))
650 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
651 add_prior_value(map, coord, 1.0, games);
652 return;
656 /* Pattern check */
657 if (pp->patternrate) {
658 struct move m = { .color = map->to_play, .coord = coord };
659 if (test_pattern3_here(p, b, &m)) {
660 if (PLDEBUGL(5))
661 fprintf(stderr, "1.0: pattern\n");
662 add_prior_value(map, coord, 1, games);
666 return;
669 void
670 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
672 struct moggy_policy *pp = p->data;
674 /* First, go through all endangered groups. */
675 for (group_t g = 1; g < board_size2(map->b); g++)
676 if (group_at(map->b, g) == g)
677 playout_moggy_assess_group(p, map, g, games);
679 /* Then, assess individual moves. */
680 if (!pp->patternrate && !pp->selfatarirate)
681 return;
682 foreach_free_point(map->b) {
683 if (map->consider[c])
684 playout_moggy_assess_one(p, map, c, games);
685 } foreach_free_point_end;
688 bool
689 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
691 struct moggy_policy *pp = p->data;
693 /* The idea is simple for now - never allow self-atari moves.
694 * They suck in general, but this also permits us to actually
695 * handle seki in the playout stage. */
697 if (fast_random(100) >= pp->selfatarirate) {
698 if (PLDEBUGL(5))
699 fprintf(stderr, "skipping sar test\n");
700 return true;
702 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
703 if (selfatari) {
704 if (PLDEBUGL(5))
705 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
706 stone2str(m->color), coord2sstr(m->coord, b));
707 if (pp->selfatari_other) {
708 /* Ok, try the other liberty of the atari'd group. */
709 coord_t c = selfatari_cousin(b, m->color, m->coord);
710 if (is_pass(c)) return false;
711 if (PLDEBUGL(5))
712 fprintf(stderr, "___ Redirecting to other lib %s\n",
713 coord2sstr(c, b));
714 m->coord = c;
715 return true;
717 return false;
719 return true;
723 struct playout_policy *
724 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
726 struct playout_policy *p = calloc2(1, sizeof(*p));
727 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
728 p->data = pp;
729 p->choose = playout_moggy_seqchoose;
730 p->assess = playout_moggy_assess;
731 p->permit = playout_moggy_permit;
733 pp->jdict = jdict;
735 /* These settings are tuned for 19x19 play with several threads
736 * on reasonable time limits (i.e., rather large number of playouts).
737 * XXX: no 9x9 tuning has been done recently. */
738 int rate = board_large(b) ? 80 : 90;
740 pp->lcapturerate = pp->atarirate = pp->capturerate = pp->patternrate
741 = pp->selfatarirate = pp->josekirate = -1U;
742 if (board_large(b)) {
743 pp->capturerate = 0;
744 pp->lcapturerate = pp->patternrate = 100;
746 pp->korate = 20; pp->koage = 4;
747 pp->alwaysccaprate = 20;
748 pp->selfatari_other = true;
749 pp->atari_def_no_hopeless = true;
750 pp->atari_miaisafe = true;
752 /* C is stupid. */
753 double mq_prob_default[MQ_MAX] = {
754 [MQ_KO] = 6.0,
755 [MQ_LATARI] = 5.0,
756 [MQ_L2LIB] = 4.0,
757 [MQ_PAT3] = 3.0,
758 [MQ_GATARI] = 2.0,
759 [MQ_JOSEKI] = 1.0,
761 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
763 if (arg) {
764 char *optspec, *next = arg;
765 while (*next) {
766 optspec = next;
767 next += strcspn(next, ":");
768 if (*next) { *next++ = 0; } else { *next = 0; }
770 char *optname = optspec;
771 char *optval = strchr(optspec, '=');
772 if (optval) *optval++ = 0;
774 if (!strcasecmp(optname, "debug") && optval) {
775 p->debug_level = atoi(optval);
776 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
777 pp->lcapturerate = atoi(optval);
778 } else if (!strcasecmp(optname, "atarirate") && optval) {
779 pp->atarirate = atoi(optval);
780 } else if (!strcasecmp(optname, "capturerate") && optval) {
781 pp->capturerate = atoi(optval);
782 } else if (!strcasecmp(optname, "patternrate") && optval) {
783 pp->patternrate = atoi(optval);
784 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
785 pp->selfatarirate = atoi(optval);
786 } else if (!strcasecmp(optname, "korate") && optval) {
787 pp->korate = atoi(optval);
788 } else if (!strcasecmp(optname, "josekirate") && optval) {
789 pp->josekirate = atoi(optval);
790 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
791 pp->alwaysccaprate = atoi(optval);
792 } else if (!strcasecmp(optname, "rate") && optval) {
793 rate = atoi(optval);
794 } else if (!strcasecmp(optname, "fillboardtries")) {
795 pp->fillboardtries = atoi(optval);
796 } else if (!strcasecmp(optname, "koage") && optval) {
797 pp->koage = atoi(optval);
798 } else if (!strcasecmp(optname, "pattern2")) {
799 pp->pattern2 = optval && *optval == '0' ? false : true;
800 } else if (!strcasecmp(optname, "selfatari_other")) {
801 pp->selfatari_other = optval && *optval == '0' ? false : true;
802 } else if (!strcasecmp(optname, "capcheckall")) {
803 pp->capcheckall = optval && *optval == '0' ? false : true;
804 } else if (!strcasecmp(optname, "atari_miaisafe")) {
805 pp->atari_miaisafe = optval && *optval == '0' ? false : true;
806 } else if (!strcasecmp(optname, "atari_def_no_hopeless")) {
807 pp->atari_def_no_hopeless = optval && *optval == '0' ? false : true;
808 } else if (!strcasecmp(optname, "fullchoose")) {
809 p->choose = optval && *optval == '0' ? playout_moggy_seqchoose : playout_moggy_fullchoose;
810 } else if (!strcasecmp(optname, "mqprob") && optval) {
811 /* KO%LATARI%L2LIB%PAT3%GATARI%JOSEKI */
812 for (int i = 0; *optval && i < MQ_MAX; i++, optval += strcspn(optval, "%")) {
813 optval++;
814 pp->mq_prob[i] = atof(optval);
816 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
817 pp->tenuki_prob = atof(optval);
818 } else {
819 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
820 exit(1);
824 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
825 if (pp->atarirate == -1U) pp->atarirate = rate;
826 if (pp->capturerate == -1U) pp->capturerate = rate;
827 if (pp->patternrate == -1U) pp->patternrate = rate;
828 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
829 if (pp->korate == -1U) pp->korate = rate;
830 if (pp->josekirate == -1U) pp->josekirate = rate;
831 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
833 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
835 return p;