15 /* Whether to set global debug level to the same as the playout
16 * has, in case it is different. This can make sure e.g. tactical
17 * reading produces proper level of debug prints during simulations.
18 * But it is safe to enable this only in single-threaded instances! */
19 //#define DEBUGL_BY_PLAYOUT
21 #define PLDEBUGL(n) DEBUGL_(policy->debug_level, n)
25 play_random_game(struct playout_setup
*setup
,
26 struct board
*b
, enum stone starting_color
,
27 struct playout_amafmap
*amafmap
,
28 struct board_ownermap
*ownermap
,
29 struct playout_policy
*policy
)
31 assert(setup
&& policy
);
33 int gamelen
= setup
->gamelen
- b
->moves
;
38 policy
->setboard(policy
, b
);
39 #ifdef DEBUGL_BY_PLAYOUT
40 int debug_level_orig
= debug_level
;
41 debug_level
= policy
->debug_level
;
44 enum stone color
= starting_color
;
46 int passes
= is_pass(b
->last_move
.coord
) && b
->moves
> 0;
48 while (gamelen
-- && passes
< 2) {
51 if (setup
->prepolicy_hook
) {
52 coord
= setup
->prepolicy_hook(policy
, setup
, b
, color
);
53 // fprintf(stderr, "prehook: %s\n", coord2sstr(coord, b));
57 coord
= policy
->choose(policy
, setup
, b
, color
);
58 // fprintf(stderr, "policy: %s\n", coord2sstr(coord, b));
61 if (is_pass(coord
) && setup
->postpolicy_hook
) {
62 coord
= setup
->postpolicy_hook(policy
, setup
, b
, color
);
63 // fprintf(stderr, "posthook: %s\n", coord2sstr(coord, b));
68 /* Defer to uniformly random move choice. */
69 /* This must never happen if the policy is tracking
70 * internal board state, obviously. */
71 assert(!policy
->setboard
);
72 board_play_random(b
, color
, &coord
, (ppr_permit
) policy
->permit
, policy
);
76 m
.coord
= coord
; m
.color
= color
;
77 if (board_play(b
, &m
) < 0) {
79 fprintf(stderr
, "Pre-picked move %d,%d is ILLEGAL:\n",
80 coord_x(coord
, b
), coord_y(coord
, b
));
81 board_print(b
, stderr
);
88 /* For UCT, superko test here is downright harmful since
89 * in superko-likely situation we throw away literally
90 * 95% of our playouts; UCT will deal with this fine by
92 if (unlikely(b
->superko_violation
)) {
93 /* We ignore superko violations that are suicides. These
94 * are common only at the end of the game and are
95 * rather harmless. (They will not go through as a root
97 if (group_at(b
, coord
)) {
99 fprintf(stderr
, "Superko fun at %d,%d in\n", coord_x(coord
, b
), coord_y(coord
, b
));
101 board_print(b
, stderr
);
106 fprintf(stderr
, "Ignoring superko at %d,%d in\n", coord_x(coord
, b
), coord_y(coord
, b
));
107 board_print(b
, stderr
);
109 b
->superko_violation
= false;
115 fprintf(stderr
, "%s %s\n", stone2str(color
), coord2sstr(coord
, b
));
117 board_print(b
, stderr
);
120 if (unlikely(is_pass(coord
))) {
123 /* We don't care about nakade counters, since we want
124 * to avoid taking pre-nakade moves into account only
125 * if they happenned in the tree before nakade nodes;
126 * but this is always out of the tree. */
128 if (amafmap
->map
[coord
] == S_NONE
|| amafmap
->map
[coord
] == color
)
129 amafmap
->map
[coord
] = color
;
130 else if (amafmap
->record_nakade
)
131 amaf_op(amafmap
->map
[coord
], +);
132 amafmap
->game
[amafmap
->gamelen
].coord
= coord
;
133 amafmap
->game
[amafmap
->gamelen
].color
= color
;
135 assert(amafmap
->gamelen
< sizeof(amafmap
->game
) / sizeof(amafmap
->game
[0]));
141 if (setup
->mercymin
&& abs(b
->captures
[S_BLACK
] - b
->captures
[S_WHITE
]) > setup
->mercymin
)
144 color
= stone_other(color
);
147 floating_t score
= board_fast_score(b
);
148 int result
= (starting_color
== S_WHITE
? score
* 2 : - (score
* 2));
151 fprintf(stderr
, "Random playout result: %d (W %f)\n", result
, score
);
153 board_print(b
, stderr
);
157 board_ownermap_fill(ownermap
, b
);
162 #ifdef DEBUGL_BY_PLAYOUT
163 debug_level
= debug_level_orig
;