is_bad_selfatari(): There are no self-atari when playing a ko threat
[pachi/t.git] / replay / replay.c
blob5d144d51093b30e0b61880ab841938b340c5ce1e
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "board.h"
5 #include "debug.h"
6 #include "engine.h"
7 #include "joseki/base.h"
8 #include "move.h"
9 #include "playout.h"
10 #include "playout/light.h"
11 #include "playout/moggy.h"
12 #include "replay/replay.h"
15 /* Internal engine state. */
16 struct replay {
17 int debug_level;
18 struct playout_policy *playout;
22 static coord_t *
23 replay_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
25 struct replay *r = e->data;
26 struct playout_setup s; memset(&s, 0, sizeof(s));
28 coord_t coord = r->playout->choose(r->playout, &s, b, color);
30 if (!is_pass(coord)) {
31 struct move m;
32 m.coord = coord; m.color = color;
33 if (board_play(b, &m) >= 0)
34 goto have_move;
36 if (DEBUGL(2)) {
37 fprintf(stderr, "Pre-picked move %d,%d is ILLEGAL:\n",
38 coord_x(coord, b), coord_y(coord, b));
39 board_print(b, stderr);
43 /* Defer to uniformly random move choice. */
44 board_play_random(b, color, &coord, (ppr_permit) r->playout->permit, r->playout);
46 have_move:
47 if (!group_at(b, coord)) {
48 /* This was suicide. Just pass. */
49 /* XXX: We should check for non-suicide alternatives. */
50 return coord_pass();
53 return coord_copy(coord);
57 struct replay *
58 replay_state_init(char *arg, struct board *b)
60 struct replay *r = calloc2(1, sizeof(struct replay));
62 r->debug_level = 1;
64 if (arg) {
65 char *optspec, *next = arg;
66 while (*next) {
67 optspec = next;
68 next += strcspn(next, ",");
69 if (*next) { *next++ = 0; } else { *next = 0; }
71 char *optname = optspec;
72 char *optval = strchr(optspec, '=');
73 if (optval) *optval++ = 0;
75 if (!strcasecmp(optname, "debug")) {
76 if (optval)
77 r->debug_level = atoi(optval);
78 else
79 r->debug_level++;
80 } else if (!strcasecmp(optname, "playout") && optval) {
81 char *playoutarg = strchr(optval, ':');
82 if (playoutarg)
83 *playoutarg++ = 0;
84 if (!strcasecmp(optval, "moggy")) {
85 r->playout = playout_moggy_init(playoutarg, b, joseki_load(b->size));
86 } else if (!strcasecmp(optval, "light")) {
87 r->playout = playout_light_init(playoutarg, b);
88 } else {
89 fprintf(stderr, "Replay: Invalid playout policy %s\n", optval);
91 } else {
92 fprintf(stderr, "Replay: Invalid engine argument %s or missing value\n", optname);
97 if (!r->playout)
98 r->playout = playout_light_init(NULL, b);
99 r->playout->debug_level = r->debug_level;
101 return r;
104 struct engine *
105 engine_replay_init(char *arg, struct board *b)
107 struct replay *r = replay_state_init(arg, b);
108 struct engine *e = calloc2(1, sizeof(struct engine));
109 e->name = "PlayoutReplay Engine";
110 e->comment = "I select moves blindly according to playout policy. I won't pass as long as there is a place on the board where I can play. When we both pass, I will consider all the stones on the board alive.";
111 e->genmove = replay_genmove;
112 e->data = r;
114 return e;