1 /* Heuristical playout (and tree prior) policy modelled primarily after
2 * the description of the Mogo engine. */
12 #include "joseki/base.h"
16 #include "playout/moggy.h"
18 #include "tactics/1lib.h"
19 #include "tactics/2lib.h"
20 #include "tactics/nlib.h"
21 #include "tactics/ladder.h"
22 #include "tactics/selfatari.h"
23 #include "uct/prior.h"
25 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
28 /* In case "seqchoose" move picker is enabled (i.e. no "fullchoose"
29 * parameter passed), we stochastically apply fixed set of decision
30 * rules in given order.
32 * In "fullchoose" mode, we instead build a move queue of variously
33 * tagged candidates, then consider a probability distribution over
34 * them and pick a move from that. */
36 /* Move queue tags. Some may be even undesirable - these moves then
37 * receive a penalty; penalty tags should be used only when it is
38 * certain the move would be considered anyway. */
51 /* Note that the context can be shared by multiple threads! */
54 unsigned int lcapturerate
, atarirate
, nlibrate
, capturerate
, patternrate
, korate
, josekirate
;
55 unsigned int selfatarirate
, alwaysccaprate
;
56 unsigned int fillboardtries
;
58 /* Whether to look for patterns around second-to-last move. */
60 /* Whether, when self-atari attempt is detected, to play the other
61 * group's liberty if that is non-self-atari. */
63 /* Whether to always pick from moves capturing all groups in
64 * global_atari_check(). */
67 bool atari_def_no_hopeless
;
72 struct joseki_dict
*jdict
;
73 struct pattern3s patterns
;
75 /* Gamma values for queue tags - correspond to probabilities. */
77 double mq_prob
[MQ_MAX
], tenuki_prob
;
81 static char moggy_patterns_src
[][11] = {
82 /* hane pattern - enclosing hane */
86 /* hane pattern - non-cutting hane */
90 /* hane pattern - magari */
94 /* hane pattern - thin hane */
98 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
102 /* cut1 pattern (kiri) - unprotected cut */
106 /* cut1 pattern (kiri) - peeped cut */
110 /* cut2 pattern (de) */
114 /* cut keima (not in Mogo) */
117 "???", /* o?? has some pathological tsumego cases */
118 /* side pattern - chase */
122 /* side pattern - block side cut */
126 /* side pattern - block side connection */
130 /* side pattern - sagari (SUSPICIOUS) */
132 "x.x" /* Mogo has "x.?" */
133 "###" /* Mogo has "X" */,
134 /* side pattern - throw-in (SUSPICIOUS) */
140 /* side pattern - cut (SUSPICIOUS) */
143 "###" /* Mogo has "X" */,
144 /* side pattern - eye piercing:
155 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
158 test_pattern3_here(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
160 struct moggy_policy
*pp
= p
->data
;
161 /* Check if 3x3 pattern is matched by given move... */
162 if (!pattern3_move_here(&pp
->patterns
, b
, m
))
164 /* ...and the move is not obviously stupid. */
165 if (is_bad_selfatari(b
, m
->color
, m
->coord
))
167 /* Ladder moves are stupid. */
168 group_t atari_neighbor
= board_get_atari_neighbor(b
, m
->coord
, m
->color
);
169 if (atari_neighbor
&& is_ladder(b
, m
->coord
, atari_neighbor
))
175 apply_pattern_here(struct playout_policy
*p
, struct board
*b
, coord_t c
, enum stone color
, struct move_queue
*q
)
177 struct move m2
= { .coord
= c
, .color
= color
};
178 if (board_is_valid_move(b
, &m2
) && test_pattern3_here(p
, b
, &m2
))
179 mq_add(q
, c
, 1<<MQ_PAT3
);
182 /* Check if we match any pattern around given move (with the other color to play). */
184 apply_pattern(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move
*mm
, struct move_queue
*q
)
186 /* Suicides do not make any patterns and confuse us. */
187 if (board_at(b
, m
->coord
) == S_NONE
|| board_at(b
, m
->coord
) == S_OFFBOARD
)
190 foreach_8neighbor(b
, m
->coord
) {
191 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
192 } foreach_8neighbor_end
;
194 if (mm
) { /* Second move for pattern searching */
195 foreach_8neighbor(b
, mm
->coord
) {
196 if (coord_is_8adjecent(m
->coord
, c
, b
))
198 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
199 } foreach_8neighbor_end
;
203 mq_print(q
, b
, "Pattern");
208 joseki_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
210 struct moggy_policy
*pp
= p
->data
;
214 for (int i
= 0; i
< 4; i
++) {
215 hash_t h
= b
->qhash
[i
] & joseki_hash_mask
;
216 coord_t
*cc
= pp
->jdict
->patterns
[h
].moves
[to_play
];
218 for (; !is_pass(*cc
); cc
++) {
219 if (coord_quadrant(*cc
, b
) != i
)
221 mq_add(q
, *cc
, 1<<MQ_JOSEKI
);
225 if (q
->moves
> 0 && PLDEBUGL(5))
226 mq_print(q
, b
, "Joseki");
230 global_atari_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
235 struct moggy_policy
*pp
= p
->data
;
236 if (pp
->capcheckall
) {
237 for (int g
= 0; g
< b
->clen
; g
++)
238 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, 1<<MQ_GATARI
);
240 mq_print(q
, b
, "Global atari");
244 int g_base
= fast_random(b
->clen
);
245 for (int g
= g_base
; g
< b
->clen
; g
++) {
246 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, 1<<MQ_GATARI
);
248 /* XXX: Try carrying on. */
250 mq_print(q
, b
, "Global atari");
254 for (int g
= 0; g
< g_base
; g
++) {
255 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, 1<<MQ_GATARI
);
257 /* XXX: Try carrying on. */
259 mq_print(q
, b
, "Global atari");
267 local_atari_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
269 struct moggy_policy
*pp
= p
->data
;
271 /* Did the opponent play a self-atari? */
272 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 1) {
273 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), q
, NULL
, 1<<MQ_LATARI
);
276 foreach_neighbor(b
, m
->coord
, {
277 group_t g
= group_at(b
, c
);
278 if (!g
|| board_group_info(b
, g
).libs
!= 1)
280 group_atari_check(pp
->alwaysccaprate
, b
, g
, stone_other(m
->color
), q
, NULL
, 1<<MQ_LATARI
);
284 mq_print(q
, b
, "Local atari");
289 local_2lib_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
291 struct moggy_policy
*pp
= p
->data
;
293 /* Does the opponent have just two liberties? */
294 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 2) {
295 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
);
297 /* We always prefer to take off an enemy chain liberty
298 * before pulling out ourselves. */
299 /* XXX: We aren't guaranteed to return to that group
302 return q
->move
[fast_random(q
->moves
)];
306 /* Then he took a third liberty from neighboring chain? */
307 foreach_neighbor(b
, m
->coord
, {
308 group_t g
= group_at(b
, c
);
309 if (!g
|| board_group_info(b
, g
).libs
!= 2)
311 group_2lib_check(b
, g
, stone_other(m
->color
), q
, 1<<MQ_L2LIB
, pp
->atari_miaisafe
, pp
->atari_def_no_hopeless
);
315 mq_print(q
, b
, "Local 2lib");
319 local_nlib_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
321 struct moggy_policy
*pp
= p
->data
;
322 enum stone color
= stone_other(m
->color
);
324 /* Attacking N-liberty groups in general is probably
325 * not feasible. What we are primarily concerned about is
326 * counter-attacking groups that have two physical liberties,
327 * but three effective liberties:
336 * The time for this to come is when the opponent took a liberty
337 * of ours, making a few-liberty group. Therefore, we focus
340 * There is a tradeoff - down to how many liberties we need to
341 * be to start looking? nlib_count=3 will work for the left black
342 * group (2lib-solver will suggest connecting the false eye), but
343 * not for top black group (it is too late to start playing 3-3
344 * capturing race). Also, we cannot prevent stupidly taking an
345 * outside liberty ourselves; the higher nlib_count, the higher
346 * the chance we withstand this.
348 * However, higher nlib_count means that we will waste more time
349 * checking non-urgent or alive groups, and we will play silly
350 * or wasted moves around alive groups. */
353 foreach_neighbor(b
, m
->coord
, {
354 group_t g
= group_at(b
, c
);
355 if (!g
|| group2
== g
|| board_at(b
, c
) != color
)
357 if (board_group_info(b
, g
).libs
< 3 || board_group_info(b
, g
).libs
> pp
->nlib_count
)
359 group_nlib_defense_check(b
, g
, color
, q
, 1<<MQ_LNLIB
);
360 group2
= g
; // prevent trivial repeated checks
364 mq_print(q
, b
, "Local nlib");
368 fillboard_check(struct playout_policy
*p
, struct board
*b
)
370 struct moggy_policy
*pp
= p
->data
;
371 unsigned int fbtries
= b
->flen
/ 8;
372 if (pp
->fillboardtries
< fbtries
)
373 fbtries
= pp
->fillboardtries
;
375 for (unsigned int i
= 0; i
< fbtries
; i
++) {
376 coord_t coord
= b
->f
[fast_random(b
->flen
)];
377 if (immediate_liberty_count(b
, coord
) != 4)
379 foreach_diag_neighbor(b
, coord
) {
380 if (board_at(b
, c
) != S_NONE
)
382 } foreach_diag_neighbor_end
;
390 playout_moggy_seqchoose(struct playout_policy
*p
, struct playout_setup
*s
, struct board
*b
, enum stone to_play
)
392 struct moggy_policy
*pp
= p
->data
;
395 board_print(b
, stderr
);
398 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
399 && b
->moves
- b
->last_ko_age
< pp
->koage
400 && pp
->korate
> fast_random(100)) {
401 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
402 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
403 return b
->last_ko
.coord
;
407 if (!is_pass(b
->last_move
.coord
)) {
408 /* Local group in atari? */
409 if (pp
->lcapturerate
> fast_random(100)) {
410 struct move_queue q
; q
.moves
= 0;
411 local_atari_check(p
, b
, &b
->last_move
, &q
);
416 /* Local group can be PUT in atari? */
417 if (pp
->atarirate
> fast_random(100)) {
418 struct move_queue q
; q
.moves
= 0;
419 local_2lib_check(p
, b
, &b
->last_move
, &q
);
424 /* Local group reduced some of our groups to 3 libs? */
425 if (pp
->nlibrate
> fast_random(100)) {
426 struct move_queue q
; q
.moves
= 0;
427 local_nlib_check(p
, b
, &b
->last_move
, &q
);
432 /* Check for patterns we know */
433 if (pp
->patternrate
> fast_random(100)) {
434 struct move_queue q
; q
.moves
= 0;
435 apply_pattern(p
, b
, &b
->last_move
,
436 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
445 /* Any groups in atari? */
446 if (pp
->capturerate
> fast_random(100)) {
447 struct move_queue q
; q
.moves
= 0;
448 global_atari_check(p
, b
, to_play
, &q
);
454 if (pp
->josekirate
> fast_random(100)) {
455 struct move_queue q
; q
.moves
= 0;
456 joseki_check(p
, b
, to_play
, &q
);
462 if (pp
->fillboardtries
> 0) {
463 coord_t c
= fillboard_check(p
, b
);
471 /* Pick a move from queue q, giving different likelihoods to moves
472 * based on their tags. */
474 mq_tagged_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
476 struct moggy_policy
*pp
= p
->data
;
478 /* First, merge all entries for a move. */
479 /* We use a naive O(N^2) since the average length of the queue
481 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
482 for (unsigned int j
= i
+ 1; j
< q
->moves
; j
++) {
483 if (q
->move
[i
] != q
->move
[j
])
485 q
->tag
[i
] |= q
->tag
[j
];
487 q
->tag
[j
] = q
->tag
[q
->moves
];
488 q
->move
[j
] = q
->move
[q
->moves
];
492 /* Now, construct a probdist. */
495 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
497 assert(q
->tag
[i
] != 0);
498 for (int j
= 0; j
< MQ_MAX
; j
++)
499 if (q
->tag
[i
] & (1<<j
)) {
500 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
501 val
*= pp
->mq_prob
[j
];
503 pd
[i
] = double_to_fixp(val
);
506 total
+= double_to_fixp(pp
->tenuki_prob
);
508 /* Finally, pick a move! */
509 fixp_t stab
= fast_irandom(total
);
510 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
511 //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));
518 assert(stab
< double_to_fixp(pp
->tenuki_prob
));
523 playout_moggy_fullchoose(struct playout_policy
*p
, struct playout_setup
*s
, struct board
*b
, enum stone to_play
)
525 struct moggy_policy
*pp
= p
->data
;
526 struct move_queue q
; q
.moves
= 0;
529 board_print(b
, stderr
);
532 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
533 && b
->moves
- b
->last_ko_age
< pp
->koage
) {
534 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
535 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
536 mq_add(&q
, b
->last_ko
.coord
, 1<<MQ_KO
);
540 if (!is_pass(b
->last_move
.coord
)) {
541 /* Local group in atari? */
542 local_atari_check(p
, b
, &b
->last_move
, &q
);
544 /* Local group can be PUT in atari? */
545 local_2lib_check(p
, b
, &b
->last_move
, &q
);
547 /* Local group reduced some of our groups to 3 libs? */
548 local_nlib_check(p
, b
, &b
->last_move
, &q
);
550 /* Check for patterns we know */
551 apply_pattern(p
, b
, &b
->last_move
,
552 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
558 /* Any groups in atari? */
559 global_atari_check(p
, b
, to_play
, &q
);
562 joseki_check(p
, b
, to_play
, &q
);
565 /* Average length of the queue is 1.4 move. */
566 printf("MQL %d ", q
.moves
);
567 for (unsigned int i
= 0; i
< q
.moves
; i
++)
568 printf("%s ", coord2sstr(q
.move
[i
], b
));
573 return mq_tagged_choose(p
, b
, to_play
, &q
);
576 if (pp
->fillboardtries
> 0) {
577 coord_t c
= fillboard_check(p
, b
);
587 playout_moggy_assess_group(struct playout_policy
*p
, struct prior_map
*map
, group_t g
, int games
)
589 struct moggy_policy
*pp
= p
->data
;
590 struct board
*b
= map
->b
;
591 struct move_queue q
; q
.moves
= 0;
593 if (board_group_info(b
, g
).libs
> pp
->nlib_count
)
597 fprintf(stderr
, "ASSESS of group %s:\n", coord2sstr(g
, b
));
598 board_print(b
, stderr
);
601 if (board_group_info(b
, g
).libs
> 2) {
604 if (board_at(b
, g
) != map
->to_play
)
605 return; // we do only defense
606 group_nlib_defense_check(b
, g
, map
->to_play
, &q
, 0);
608 coord_t coord
= q
.move
[q
.moves
];
610 fprintf(stderr
, "1.0: nlib %s\n", coord2sstr(coord
, b
));
611 int assess
= games
/ 2;
612 add_prior_value(map
, coord
, 1, assess
);
617 if (board_group_info(b
, g
).libs
== 2) {
620 group_2lib_check(b
, g
, map
->to_play
, &q
, 0, pp
->atari_miaisafe
, pp
->atari_def_no_hopeless
);
622 coord_t coord
= q
.move
[q
.moves
];
624 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
625 int assess
= games
/ 2;
626 add_prior_value(map
, coord
, 1, assess
);
631 /* This group, sir, is in atari! */
633 coord_t ladder
= pass
;
634 group_atari_check(pp
->alwaysccaprate
, b
, g
, map
->to_play
, &q
, &ladder
, 0);
636 coord_t coord
= q
.move
[q
.moves
];
638 /* _Never_ play here if this move plays out
639 * a caught ladder. */
640 if (coord
== ladder
&& !board_playing_ko_threat(b
)) {
641 /* Note that the opposite is not guarded against;
642 * we do not advise against capturing a laddered
643 * group (but we don't encourage it either). Such
644 * a move can simplify tactical situations if we
646 if (map
->to_play
!= board_at(b
, g
))
648 /* FIXME: We give the malus even if this move
649 * captures another group. */
651 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
652 add_prior_value(map
, coord
, 0, games
);
656 if (!pp
->capturerate
&& !pp
->lcapturerate
)
660 fprintf(stderr
, "1.0: atari %s\n", coord2sstr(coord
, b
));
661 int assess
= games
* 2;
662 add_prior_value(map
, coord
, 1, assess
);
667 playout_moggy_assess_one(struct playout_policy
*p
, struct prior_map
*map
, coord_t coord
, int games
)
669 struct moggy_policy
*pp
= p
->data
;
670 struct board
*b
= map
->b
;
673 fprintf(stderr
, "ASSESS of move %s:\n", coord2sstr(coord
, b
));
674 board_print(b
, stderr
);
677 /* Is this move a self-atari? */
678 if (pp
->selfatarirate
) {
679 if (!board_playing_ko_threat(b
) && is_bad_selfatari(b
, map
->to_play
, coord
)) {
681 fprintf(stderr
, "0.0: self-atari\n");
682 add_prior_value(map
, coord
, 0, games
);
683 if (!pp
->selfatari_other
)
685 /* If we can play on the other liberty of the
686 * endangered group, do! */
687 coord
= selfatari_cousin(b
, map
->to_play
, coord
);
691 fprintf(stderr
, "1.0: self-atari redirect %s\n", coord2sstr(coord
, b
));
692 add_prior_value(map
, coord
, 1.0, games
);
698 if (pp
->patternrate
) {
699 struct move m
= { .color
= map
->to_play
, .coord
= coord
};
700 if (test_pattern3_here(p
, b
, &m
)) {
702 fprintf(stderr
, "1.0: pattern\n");
703 add_prior_value(map
, coord
, 1, games
);
711 playout_moggy_assess(struct playout_policy
*p
, struct prior_map
*map
, int games
)
713 struct moggy_policy
*pp
= p
->data
;
715 /* First, go through all endangered groups. */
716 for (group_t g
= 1; g
< board_size2(map
->b
); g
++)
717 if (group_at(map
->b
, g
) == g
)
718 playout_moggy_assess_group(p
, map
, g
, games
);
720 /* Then, assess individual moves. */
721 if (!pp
->patternrate
&& !pp
->selfatarirate
)
723 foreach_free_point(map
->b
) {
724 if (map
->consider
[c
])
725 playout_moggy_assess_one(p
, map
, c
, games
);
726 } foreach_free_point_end
;
730 playout_moggy_permit(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
732 struct moggy_policy
*pp
= p
->data
;
734 /* The idea is simple for now - never allow self-atari moves.
735 * They suck in general, but this also permits us to actually
736 * handle seki in the playout stage. */
738 if (fast_random(100) >= pp
->selfatarirate
) {
740 fprintf(stderr
, "skipping sar test\n");
743 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
746 fprintf(stderr
, "__ Prohibiting self-atari %s %s\n",
747 stone2str(m
->color
), coord2sstr(m
->coord
, b
));
748 if (pp
->selfatari_other
) {
749 /* Ok, try the other liberty of the atari'd group. */
750 coord_t c
= selfatari_cousin(b
, m
->color
, m
->coord
);
751 if (is_pass(c
)) return false;
753 fprintf(stderr
, "___ Redirecting to other lib %s\n",
764 struct playout_policy
*
765 playout_moggy_init(char *arg
, struct board
*b
, struct joseki_dict
*jdict
)
767 struct playout_policy
*p
= calloc2(1, sizeof(*p
));
768 struct moggy_policy
*pp
= calloc2(1, sizeof(*pp
));
770 p
->choose
= playout_moggy_seqchoose
;
771 p
->assess
= playout_moggy_assess
;
772 p
->permit
= playout_moggy_permit
;
776 /* These settings are tuned for 19x19 play with several threads
777 * on reasonable time limits (i.e., rather large number of playouts).
778 * XXX: no 9x9 tuning has been done recently. */
779 int rate
= board_large(b
) ? 80 : 90;
781 pp
->lcapturerate
= pp
->atarirate
= pp
->nlibrate
= pp
->patternrate
782 = pp
->selfatarirate
= pp
->josekirate
= -1U;
783 if (board_large(b
)) {
784 pp
->lcapturerate
= pp
->patternrate
= 100;
788 pp
->korate
= 20; pp
->koage
= 4;
789 pp
->alwaysccaprate
= 20;
790 pp
->selfatari_other
= true;
791 pp
->atari_def_no_hopeless
= !board_large(b
);
792 pp
->atari_miaisafe
= true;
796 double mq_prob_default
[MQ_MAX
] = {
805 memcpy(pp
->mq_prob
, mq_prob_default
, sizeof(pp
->mq_prob
));
808 char *optspec
, *next
= arg
;
811 next
+= strcspn(next
, ":");
812 if (*next
) { *next
++ = 0; } else { *next
= 0; }
814 char *optname
= optspec
;
815 char *optval
= strchr(optspec
, '=');
816 if (optval
) *optval
++ = 0;
818 if (!strcasecmp(optname
, "debug") && optval
) {
819 p
->debug_level
= atoi(optval
);
820 } else if (!strcasecmp(optname
, "lcapturerate") && optval
) {
821 pp
->lcapturerate
= atoi(optval
);
822 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
823 pp
->atarirate
= atoi(optval
);
824 } else if (!strcasecmp(optname
, "nlibrate") && optval
) {
825 pp
->nlibrate
= atoi(optval
);
826 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
827 pp
->capturerate
= atoi(optval
);
828 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
829 pp
->patternrate
= atoi(optval
);
830 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
831 pp
->selfatarirate
= atoi(optval
);
832 } else if (!strcasecmp(optname
, "korate") && optval
) {
833 pp
->korate
= atoi(optval
);
834 } else if (!strcasecmp(optname
, "josekirate") && optval
) {
835 pp
->josekirate
= atoi(optval
);
836 } else if (!strcasecmp(optname
, "alwaysccaprate") && optval
) {
837 pp
->alwaysccaprate
= atoi(optval
);
838 } else if (!strcasecmp(optname
, "rate") && optval
) {
840 } else if (!strcasecmp(optname
, "fillboardtries")) {
841 pp
->fillboardtries
= atoi(optval
);
842 } else if (!strcasecmp(optname
, "koage") && optval
) {
843 pp
->koage
= atoi(optval
);
844 } else if (!strcasecmp(optname
, "pattern2")) {
845 pp
->pattern2
= optval
&& *optval
== '0' ? false : true;
846 } else if (!strcasecmp(optname
, "selfatari_other")) {
847 pp
->selfatari_other
= optval
&& *optval
== '0' ? false : true;
848 } else if (!strcasecmp(optname
, "capcheckall")) {
849 pp
->capcheckall
= optval
&& *optval
== '0' ? false : true;
850 } else if (!strcasecmp(optname
, "atari_miaisafe")) {
851 pp
->atari_miaisafe
= optval
&& *optval
== '0' ? false : true;
852 } else if (!strcasecmp(optname
, "atari_def_no_hopeless")) {
853 pp
->atari_def_no_hopeless
= optval
&& *optval
== '0' ? false : true;
854 } else if (!strcasecmp(optname
, "nlib_count") && optval
) {
855 pp
->nlib_count
= atoi(optval
);
856 } else if (!strcasecmp(optname
, "fullchoose")) {
857 p
->choose
= optval
&& *optval
== '0' ? playout_moggy_seqchoose
: playout_moggy_fullchoose
;
858 } else if (!strcasecmp(optname
, "mqprob") && optval
) {
859 /* KO%LATARI%L2LIB%LNLIB%PAT3%GATARI%JOSEKI */
860 for (int i
= 0; *optval
&& i
< MQ_MAX
; i
++, optval
+= strcspn(optval
, "%")) {
862 pp
->mq_prob
[i
] = atof(optval
);
864 } else if (!strcasecmp(optname
, "tenukiprob") && optval
) {
865 pp
->tenuki_prob
= atof(optval
);
867 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
872 if (pp
->lcapturerate
== -1U) pp
->lcapturerate
= rate
;
873 if (pp
->atarirate
== -1U) pp
->atarirate
= rate
;
874 if (pp
->capturerate
== -1U) pp
->capturerate
= rate
;
875 if (pp
->patternrate
== -1U) pp
->patternrate
= rate
;
876 if (pp
->selfatarirate
== -1U) pp
->selfatarirate
= rate
;
877 if (pp
->korate
== -1U) pp
->korate
= rate
;
878 if (pp
->josekirate
== -1U) pp
->josekirate
= rate
;
879 if (pp
->alwaysccaprate
== -1U) pp
->alwaysccaprate
= rate
;
881 pattern3s_init(&pp
->patterns
, moggy_patterns_src
, moggy_patterns_src_n
);