1 /* Playout policy by stochastically applying a fixed set of decision
2 * rules in given order - modelled after the intelligent playouts
3 * in the Mogo engine. */
13 #include "joseki/base.h"
17 #include "playout/moggy.h"
19 #include "tactics/1lib.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)
26 /* Whether to avoid capturing/atariing doomed groups (this is big
27 * performance hit and may reduce playouts balance; it does increase
28 * the strength, but not quite proportionally to the performance). */
29 //#define NO_DOOMED_GROUPS
32 /* Move queue tags: */
44 /* Note that the context can be shared by multiple threads! */
47 unsigned int lcapturerate
, atarirate
, capturerate
, patternrate
, korate
, josekirate
;
48 unsigned int selfatarirate
, alwaysccaprate
;
49 unsigned int fillboardtries
;
51 /* Whether to look for patterns around second-to-last move. */
53 /* Whether, when self-atari attempt is detected, to play the other
54 * group's liberty if that is non-self-atari. */
56 /* Whether to always pick from moves capturing all groups in
57 * global_atari_check(). */
60 struct joseki_dict
*jdict
;
61 struct pattern3s patterns
;
63 /* Gamma values for queue tags - correspond to probabilities. */
65 double mq_prob
[MQ_MAX
], tenuki_prob
;
69 static char moggy_patterns_src
[][11] = {
70 /* hane pattern - enclosing hane */
74 /* hane pattern - non-cutting hane */
78 /* hane pattern - magari */
82 /* hane pattern - thin hane */
86 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
90 /* cut1 pattern (kiri) - unprotected cut */
94 /* cut1 pattern (kiri) - peeped cut */
98 /* cut2 pattern (de) */
102 /* cut keima (not in Mogo) */
105 "???", /* o?? has some pathological tsumego cases */
106 /* side pattern - chase */
110 /* side pattern - block side cut */
114 /* side pattern - block side connection */
118 /* side pattern - sagari (SUSPICIOUS) */
120 "x.x" /* Mogo has "x.?" */
121 "###" /* Mogo has "X" */,
122 /* side pattern - throw-in (SUSPICIOUS) */
128 /* side pattern - cut (SUSPICIOUS) */
131 "###" /* Mogo has "X" */,
133 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
136 test_pattern3_here(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
138 struct moggy_policy
*pp
= p
->data
;
139 /* Check if 3x3 pattern is matched by given move... */
140 if (!pattern3_move_here(&pp
->patterns
, b
, m
))
142 /* ...and the move is not obviously stupid. */
143 if (is_bad_selfatari(b
, m
->color
, m
->coord
))
145 /* Ladder moves are stupid. */
146 group_t atari_neighbor
= board_get_atari_neighbor(b
, m
->coord
, m
->color
);
147 if (atari_neighbor
&& is_ladder(b
, m
->coord
, atari_neighbor
))
153 apply_pattern_here(struct playout_policy
*p
, struct board
*b
, coord_t c
, enum stone color
, struct move_queue
*q
)
155 struct move m2
= { .coord
= c
, .color
= color
};
156 if (board_is_valid_move(b
, &m2
) && test_pattern3_here(p
, b
, &m2
))
157 mq_add(q
, c
, 1<<MQ_PAT3
);
160 /* Check if we match any pattern around given move (with the other color to play). */
162 apply_pattern(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move
*mm
, struct move_queue
*q
)
164 /* Suicides do not make any patterns and confuse us. */
165 if (board_at(b
, m
->coord
) == S_NONE
|| board_at(b
, m
->coord
) == S_OFFBOARD
)
168 foreach_8neighbor(b
, m
->coord
) {
169 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
170 } foreach_8neighbor_end
;
172 if (mm
) { /* Second move for pattern searching */
173 foreach_8neighbor(b
, mm
->coord
) {
174 if (coord_is_8adjecent(m
->coord
, c
, b
))
176 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
177 } foreach_8neighbor_end
;
181 mq_print(q
, b
, "Pattern");
186 joseki_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
188 struct moggy_policy
*pp
= p
->data
;
192 for (int i
= 0; i
< 4; i
++) {
193 hash_t h
= b
->qhash
[i
] & joseki_hash_mask
;
194 coord_t
*cc
= pp
->jdict
->patterns
[h
].moves
[to_play
];
196 for (; !is_pass(*cc
); cc
++) {
197 if (coord_quadrant(*cc
, b
) != i
)
199 mq_add(q
, *cc
, 1<<MQ_JOSEKI
);
203 if (q
->moves
> 0 && PLDEBUGL(5))
204 mq_print(q
, b
, "Joseki");
208 global_atari_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
213 struct moggy_policy
*pp
= p
->data
;
214 if (pp
->capcheckall
) {
215 for (int g
= 0; g
< b
->clen
; g
++)
216 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, 1<<MQ_GATARI
);
218 mq_print(q
, b
, "Global atari");
222 int g_base
= fast_random(b
->clen
);
223 for (int g
= g_base
; g
< b
->clen
; g
++) {
224 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, 1<<MQ_GATARI
);
226 /* XXX: Try carrying on. */
228 mq_print(q
, b
, "Global atari");
232 for (int g
= 0; g
< g_base
; g
++) {
233 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, 1<<MQ_GATARI
);
235 /* XXX: Try carrying on. */
237 mq_print(q
, b
, "Global atari");
245 local_atari_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
247 struct moggy_policy
*pp
= p
->data
;
249 /* Did the opponent play a self-atari? */
250 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 1) {
251 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), q
, NULL
, 1<<MQ_LATARI
);
254 foreach_neighbor(b
, m
->coord
, {
255 group_t g
= group_at(b
, c
);
256 if (!g
|| board_group_info(b
, g
).libs
!= 1)
258 group_atari_check(pp
->alwaysccaprate
, b
, g
, stone_other(m
->color
), q
, NULL
, 1<<MQ_LATARI
);
262 mq_print(q
, b
, "Local atari");
266 miai_2lib(struct board
*b
, group_t group
, enum stone color
)
268 bool can_connect
= false, can_pull_out
= false;
269 /* We have miai if we can either connect on both libs,
270 * or connect on one lib and escape on another. (Just
271 * having two escape routes can be risky.) We must make
272 * sure that we don't consider following as miai:
275 * O O X O - left dot would be pull-out, right dot connect */
276 foreach_neighbor(b
, board_group_info(b
, group
).lib
[0], {
277 enum stone cc
= board_at(b
, c
);
278 if (cc
== S_NONE
&& cc
!= board_at(b
, board_group_info(b
, group
).lib
[1])) {
280 } else if (cc
!= color
) {
284 group_t cg
= group_at(b
, c
);
285 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
288 foreach_neighbor(b
, board_group_info(b
, group
).lib
[1], {
289 enum stone cc
= board_at(b
, c
);
290 if (c
== board_group_info(b
, group
).lib
[0])
292 if (cc
== S_NONE
&& can_connect
) {
294 } else if (cc
!= color
) {
298 group_t cg
= group_at(b
, c
);
299 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
300 return (can_connect
|| can_pull_out
);
306 check_group_atari(struct board
*b
, group_t group
, enum stone owner
,
307 enum stone to_play
, struct move_queue
*q
)
309 for (int i
= 0; i
< 2; i
++) {
310 coord_t lib
= board_group_info(b
, group
).lib
[i
];
311 assert(board_at(b
, lib
) == S_NONE
);
312 if (!board_is_valid_play(b
, to_play
, lib
))
315 /* Don't play at the spot if it is extremely short
317 /* XXX: This looks harmful, could significantly
318 * prefer atari to throwin:
324 if (neighbor_count_at(b
, lib
, stone_other(owner
)) + immediate_liberty_count(b
, lib
) < 2)
328 /* If the move is too "lumpy", do not play it:
331 * ..O.X.X <- always play the left one!
333 if (neighbor_count_at(b
, lib
, stone_other(owner
)) + neighbor_count_at(b
, lib
, S_OFFBOARD
) == 3)
336 #ifdef NO_DOOMED_GROUPS
337 /* If the owner can't play at the spot, we don't want
338 * to bother either. */
339 if (is_bad_selfatari(b
, owner
, lib
))
343 /* Of course we don't want to play bad selfatari
344 * ourselves, if we are the attacker... */
346 #ifdef NO_DOOMED_GROUPS
349 is_bad_selfatari(b
, to_play
, lib
))
352 /* Tasty! Crispy! Good! */
353 mq_add(q
, lib
, 1<<MQ_L2LIB
);
359 group_2lib_check(struct board
*b
, group_t group
, enum stone to_play
, struct move_queue
*q
)
361 enum stone color
= board_at(b
, group_base(group
));
362 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
365 fprintf(stderr
, "[%s] 2lib check of color %d\n",
366 coord2sstr(group
, b
), color
);
368 /* Do not try to atari groups that cannot be harmed. */
369 if (miai_2lib(b
, group
, color
))
372 check_group_atari(b
, group
, color
, to_play
, q
);
374 /* Can we counter-atari another group, if we are the defender? */
375 if (to_play
!= color
)
377 foreach_in_group(b
, group
) {
378 foreach_neighbor(b
, c
, {
379 if (board_at(b
, c
) != stone_other(color
))
381 group_t g2
= group_at(b
, c
);
382 if (board_group_info(b
, g2
).libs
== 1) {
383 /* We can capture a neighbor. */
384 mq_add(q
, board_group_info(b
, g2
).lib
[0], 1<<MQ_L2LIB
);
388 if (board_group_info(b
, g2
).libs
!= 2)
390 check_group_atari(b
, g2
, color
, to_play
, q
);
392 } foreach_in_group_end
;
396 local_2lib_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
398 /* Does the opponent have just two liberties? */
399 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 2) {
400 group_2lib_check(b
, group_at(b
, m
->coord
), stone_other(m
->color
), q
);
402 /* We always prefer to take off an enemy chain liberty
403 * before pulling out ourselves. */
404 /* XXX: We aren't guaranteed to return to that group
407 return q
->move
[fast_random(q
->moves
)];
411 /* Then he took a third liberty from neighboring chain? */
412 foreach_neighbor(b
, m
->coord
, {
413 group_t g
= group_at(b
, c
);
414 if (!g
|| board_group_info(b
, g
).libs
!= 2)
416 group_2lib_check(b
, g
, stone_other(m
->color
), q
);
420 mq_print(q
, b
, "Local 2lib");
424 fillboard_check(struct playout_policy
*p
, struct board
*b
)
426 struct moggy_policy
*pp
= p
->data
;
427 unsigned int fbtries
= b
->flen
/ 8;
428 if (pp
->fillboardtries
< fbtries
)
429 fbtries
= pp
->fillboardtries
;
431 for (unsigned int i
= 0; i
< fbtries
; i
++) {
432 coord_t coord
= b
->f
[fast_random(b
->flen
)];
433 if (immediate_liberty_count(b
, coord
) != 4)
435 foreach_diag_neighbor(b
, coord
) {
436 if (board_at(b
, c
) != S_NONE
)
438 } foreach_diag_neighbor_end
;
446 playout_moggy_partchoose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
448 struct moggy_policy
*pp
= p
->data
;
451 board_print(b
, stderr
);
454 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
455 && b
->moves
- b
->last_ko_age
< pp
->koage
456 && pp
->korate
> fast_random(100)) {
457 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
458 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
459 return b
->last_ko
.coord
;
463 if (!is_pass(b
->last_move
.coord
)) {
464 /* Local group in atari? */
465 if (pp
->lcapturerate
> fast_random(100)) {
466 struct move_queue q
; q
.moves
= 0;
467 local_atari_check(p
, b
, &b
->last_move
, &q
);
472 /* Local group can be PUT in atari? */
473 if (pp
->atarirate
> fast_random(100)) {
474 struct move_queue q
; q
.moves
= 0;
475 local_2lib_check(p
, b
, &b
->last_move
, &q
);
480 /* Check for patterns we know */
481 if (pp
->patternrate
> fast_random(100)) {
482 struct move_queue q
; q
.moves
= 0;
483 apply_pattern(p
, b
, &b
->last_move
,
484 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
493 /* Any groups in atari? */
494 if (pp
->capturerate
> fast_random(100)) {
495 struct move_queue q
; q
.moves
= 0;
496 global_atari_check(p
, b
, to_play
, &q
);
502 if (pp
->josekirate
> fast_random(100)) {
503 struct move_queue q
; q
.moves
= 0;
504 joseki_check(p
, b
, to_play
, &q
);
510 if (pp
->fillboardtries
> 0) {
511 coord_t c
= fillboard_check(p
, b
);
519 /* Pick a move from queue q, giving different likelihoods to moves
520 * based on their tags. */
522 mq_tagged_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
524 struct moggy_policy
*pp
= p
->data
;
526 /* First, merge all entries for a move. */
527 /* We use a naive O(N^2) since the average length of the queue
529 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
530 for (unsigned int j
= i
+ 1; j
< q
->moves
; j
++) {
531 if (q
->move
[i
] != q
->move
[j
])
533 q
->tag
[i
] |= q
->tag
[j
];
535 q
->tag
[j
] = q
->tag
[q
->moves
];
536 q
->move
[j
] = q
->move
[q
->moves
];
540 /* Now, construct a probdist. */
543 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
545 assert(q
->tag
[i
] != 0);
546 for (int j
= 1; j
< MQ_MAX
; j
++)
547 if (q
->tag
[i
] & (1<<j
)) {
548 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
549 val
*= pp
->mq_prob
[j
];
551 pd
[i
] = double_to_fixp(val
);
554 total
+= double_to_fixp(pp
->tenuki_prob
);
556 /* Finally, pick a move! */
557 fixp_t stab
= fast_irandom(total
);
558 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
559 //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));
566 assert(stab
< double_to_fixp(pp
->tenuki_prob
));
571 playout_moggy_fullchoose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
573 struct moggy_policy
*pp
= p
->data
;
574 struct move_queue q
; q
.moves
= 0;
577 board_print(b
, stderr
);
580 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
581 && b
->moves
- b
->last_ko_age
< pp
->koage
582 && pp
->korate
> fast_random(100)) {
583 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
584 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
585 mq_add(&q
, b
->last_ko
.coord
, 1<<MQ_KO
);
589 if (!is_pass(b
->last_move
.coord
)) {
590 /* Local group in atari? */
591 if (pp
->lcapturerate
> fast_random(100)) {
592 local_atari_check(p
, b
, &b
->last_move
, &q
);
595 /* Local group can be PUT in atari? */
596 if (pp
->atarirate
> fast_random(100)) {
597 local_2lib_check(p
, b
, &b
->last_move
, &q
);
600 /* Check for patterns we know */
601 if (pp
->patternrate
> fast_random(100)) {
602 apply_pattern(p
, b
, &b
->last_move
,
603 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
610 /* Any groups in atari? */
611 if (pp
->capturerate
> fast_random(100)) {
612 global_atari_check(p
, b
, to_play
, &q
);
616 if (pp
->josekirate
> fast_random(100)) {
617 joseki_check(p
, b
, to_play
, &q
);
621 /* Average length of the queue is 1.4 move. */
622 printf("MQL %d ", q
.moves
);
623 for (unsigned int i
= 0; i
< q
.moves
; i
++)
624 printf("%s ", coord2sstr(q
.move
[i
], b
));
629 return mq_tagged_choose(p
, b
, to_play
, &q
);
632 if (pp
->fillboardtries
> 0) {
633 coord_t c
= fillboard_check(p
, b
);
643 selfatari_cousin(struct board
*b
, enum stone color
, coord_t coord
)
645 group_t groups
[4]; int groups_n
= 0;
646 foreach_neighbor(b
, coord
, {
647 enum stone s
= board_at(b
, c
);
648 if (s
!= color
) continue;
649 group_t g
= group_at(b
, c
);
650 if (board_group_info(b
, g
).libs
== 2)
651 groups
[groups_n
++] = g
;
656 group_t group
= groups
[fast_random(groups_n
)];
658 coord_t lib2
= board_group_other_lib(b
, group
, coord
);
659 if (is_bad_selfatari(b
, color
, lib2
))
665 playout_moggy_assess_group(struct playout_policy
*p
, struct prior_map
*map
, group_t g
, int games
)
667 struct moggy_policy
*pp
= p
->data
;
668 struct board
*b
= map
->b
;
669 struct move_queue q
; q
.moves
= 0;
671 if (board_group_info(b
, g
).libs
> 2)
675 fprintf(stderr
, "ASSESS of group %s:\n", coord2sstr(g
, b
));
676 board_print(b
, stderr
);
679 if (board_group_info(b
, g
).libs
== 2) {
682 group_2lib_check(b
, g
, map
->to_play
, &q
);
684 coord_t coord
= q
.move
[q
.moves
];
686 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
687 int assess
= games
/ 2;
688 add_prior_value(map
, coord
, 1, assess
);
693 /* This group, sir, is in atari! */
695 coord_t ladder
= pass
;
696 group_atari_check(pp
->alwaysccaprate
, b
, g
, map
->to_play
, &q
, &ladder
, 0);
698 coord_t coord
= q
.move
[q
.moves
];
700 /* _Never_ play here if this move plays out
701 * a caught ladder. */
702 if (coord
== ladder
&& !board_playing_ko_threat(b
)) {
703 /* Note that the opposite is not guarded against;
704 * we do not advise against capturing a laddered
705 * group (but we don't encourage it either). Such
706 * a move can simplify tactical situations if we
708 if (map
->to_play
!= board_at(b
, g
))
710 /* FIXME: We give the malus even if this move
711 * captures another group. */
713 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
714 add_prior_value(map
, coord
, 0, games
);
718 if (!pp
->capturerate
&& !pp
->lcapturerate
)
722 fprintf(stderr
, "1.0: atari %s\n", coord2sstr(coord
, b
));
723 int assess
= games
* 2;
724 add_prior_value(map
, coord
, 1, assess
);
729 playout_moggy_assess_one(struct playout_policy
*p
, struct prior_map
*map
, coord_t coord
, int games
)
731 struct moggy_policy
*pp
= p
->data
;
732 struct board
*b
= map
->b
;
735 fprintf(stderr
, "ASSESS of move %s:\n", coord2sstr(coord
, b
));
736 board_print(b
, stderr
);
739 /* Is this move a self-atari? */
740 if (pp
->selfatarirate
) {
741 if (!board_playing_ko_threat(b
) && is_bad_selfatari(b
, map
->to_play
, coord
)) {
743 fprintf(stderr
, "0.0: self-atari\n");
744 add_prior_value(map
, coord
, 0, games
);
745 if (!pp
->selfatari_other
)
747 /* If we can play on the other liberty of the
748 * endangered group, do! */
749 coord
= selfatari_cousin(b
, map
->to_play
, coord
);
753 fprintf(stderr
, "1.0: self-atari redirect %s\n", coord2sstr(coord
, b
));
754 add_prior_value(map
, coord
, 1.0, games
);
760 if (pp
->patternrate
) {
761 struct move m
= { .color
= map
->to_play
, .coord
= coord
};
762 if (test_pattern3_here(p
, b
, &m
)) {
764 fprintf(stderr
, "1.0: pattern\n");
765 add_prior_value(map
, coord
, 1, games
);
773 playout_moggy_assess(struct playout_policy
*p
, struct prior_map
*map
, int games
)
775 struct moggy_policy
*pp
= p
->data
;
777 /* First, go through all endangered groups. */
778 for (group_t g
= 1; g
< board_size2(map
->b
); g
++)
779 if (group_at(map
->b
, g
) == g
)
780 playout_moggy_assess_group(p
, map
, g
, games
);
782 /* Then, assess individual moves. */
783 if (!pp
->patternrate
&& !pp
->selfatarirate
)
785 foreach_free_point(map
->b
) {
786 if (map
->consider
[c
])
787 playout_moggy_assess_one(p
, map
, c
, games
);
788 } foreach_free_point_end
;
792 playout_moggy_permit(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
794 struct moggy_policy
*pp
= p
->data
;
796 /* The idea is simple for now - never allow self-atari moves.
797 * They suck in general, but this also permits us to actually
798 * handle seki in the playout stage. */
800 if (fast_random(100) >= pp
->selfatarirate
) {
802 fprintf(stderr
, "skipping sar test\n");
805 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
808 fprintf(stderr
, "__ Prohibiting self-atari %s %s\n",
809 stone2str(m
->color
), coord2sstr(m
->coord
, b
));
810 if (pp
->selfatari_other
) {
811 /* Ok, try the other liberty of the atari'd group. */
812 coord_t c
= selfatari_cousin(b
, m
->color
, m
->coord
);
813 if (is_pass(c
)) return false;
815 fprintf(stderr
, "___ Redirecting to other lib %s\n",
826 struct playout_policy
*
827 playout_moggy_init(char *arg
, struct board
*b
, struct joseki_dict
*jdict
)
829 struct playout_policy
*p
= calloc2(1, sizeof(*p
));
830 struct moggy_policy
*pp
= calloc2(1, sizeof(*pp
));
832 p
->choose
= playout_moggy_partchoose
;
833 p
->assess
= playout_moggy_assess
;
834 p
->permit
= playout_moggy_permit
;
840 pp
->lcapturerate
= pp
->atarirate
= pp
->capturerate
= pp
->patternrate
841 = pp
->selfatarirate
= pp
->josekirate
= -1U;
842 pp
->korate
= 0; pp
->koage
= 4;
843 pp
->alwaysccaprate
= 0;
844 pp
->selfatari_other
= true;
847 double mq_prob_default
[MQ_MAX
] = {
855 memcpy(pp
->mq_prob
, mq_prob_default
, sizeof(pp
->mq_prob
));
858 char *optspec
, *next
= arg
;
861 next
+= strcspn(next
, ":");
862 if (*next
) { *next
++ = 0; } else { *next
= 0; }
864 char *optname
= optspec
;
865 char *optval
= strchr(optspec
, '=');
866 if (optval
) *optval
++ = 0;
868 if (!strcasecmp(optname
, "debug") && optval
) {
869 p
->debug_level
= atoi(optval
);
870 } else if (!strcasecmp(optname
, "lcapturerate") && optval
) {
871 pp
->lcapturerate
= atoi(optval
);
872 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
873 pp
->atarirate
= atoi(optval
);
874 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
875 pp
->capturerate
= atoi(optval
);
876 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
877 pp
->patternrate
= atoi(optval
);
878 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
879 pp
->selfatarirate
= atoi(optval
);
880 } else if (!strcasecmp(optname
, "korate") && optval
) {
881 pp
->korate
= atoi(optval
);
882 } else if (!strcasecmp(optname
, "josekirate") && optval
) {
883 pp
->josekirate
= atoi(optval
);
884 } else if (!strcasecmp(optname
, "alwaysccaprate") && optval
) {
885 pp
->alwaysccaprate
= atoi(optval
);
886 } else if (!strcasecmp(optname
, "rate") && optval
) {
888 } else if (!strcasecmp(optname
, "fillboardtries")) {
889 pp
->fillboardtries
= atoi(optval
);
890 } else if (!strcasecmp(optname
, "koage") && optval
) {
891 pp
->koage
= atoi(optval
);
892 } else if (!strcasecmp(optname
, "pattern2")) {
893 pp
->pattern2
= optval
&& *optval
== '0' ? false : true;
894 } else if (!strcasecmp(optname
, "selfatari_other")) {
895 pp
->selfatari_other
= optval
&& *optval
== '0' ? false : true;
896 } else if (!strcasecmp(optname
, "capcheckall")) {
897 pp
->capcheckall
= optval
&& *optval
== '0' ? false : true;
898 } else if (!strcasecmp(optname
, "fullchoose")) {
899 p
->choose
= optval
&& *optval
== '0' ? playout_moggy_partchoose
: playout_moggy_fullchoose
;
900 } else if (!strcasecmp(optname
, "mqprob") && optval
) {
901 /* KO%LATARI%L2LIB%PAT3%GATARI%JOSEKI */
902 for (int i
= 1; *optval
&& i
< MQ_MAX
; i
++, optval
+= strcspn(optval
, "%")) {
904 pp
->mq_prob
[i
] = atof(optval
);
906 } else if (!strcasecmp(optname
, "tenukiprob") && optval
) {
907 pp
->tenuki_prob
= atof(optval
);
909 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
914 if (pp
->lcapturerate
== -1U) pp
->lcapturerate
= rate
;
915 if (pp
->atarirate
== -1U) pp
->atarirate
= rate
;
916 if (pp
->capturerate
== -1U) pp
->capturerate
= rate
;
917 if (pp
->patternrate
== -1U) pp
->patternrate
= rate
;
918 if (pp
->selfatarirate
== -1U) pp
->selfatarirate
= rate
;
919 if (pp
->korate
== -1U) pp
->korate
= rate
;
920 if (pp
->josekirate
== -1U) pp
->josekirate
= rate
;
921 if (pp
->alwaysccaprate
== -1U) pp
->alwaysccaprate
= rate
;
923 pattern3s_init(&pp
->patterns
, moggy_patterns_src
, moggy_patterns_src_n
);