UCT: Progressive Unpruning support (for all policies, tunable)
[pachi.git] / replay / replay.c
blob55539ab4f1f48f64e82fa7a270be62e76509eae7
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/elo.h"
11 #include "playout/light.h"
12 #include "playout/moggy.h"
13 #include "replay/replay.h"
16 /* Internal engine state. */
17 struct replay {
18 int debug_level;
19 struct playout_policy *playout;
23 static coord_t *
24 replay_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
26 struct replay *r = e->data;
27 struct playout_setup s; memset(&s, 0, sizeof(s));
29 coord_t coord = r->playout->choose(r->playout, &s, b, color);
31 if (!is_pass(coord)) {
32 struct move m;
33 m.coord = coord; m.color = color;
34 if (board_play(b, &m) >= 0)
35 goto have_move;
37 if (DEBUGL(2)) {
38 fprintf(stderr, "Pre-picked move %d,%d is ILLEGAL:\n",
39 coord_x(coord, b), coord_y(coord, b));
40 board_print(b, stderr);
44 /* Defer to uniformly random move choice. */
45 board_play_random(b, color, &coord, (ppr_permit) r->playout->permit, r->playout);
47 have_move:
48 if (!group_at(b, coord)) {
49 /* This was suicide. Just pass. */
50 /* XXX: We should check for non-suicide alternatives. */
51 return coord_pass();
54 return coord_copy(coord);
58 struct replay *
59 replay_state_init(char *arg, struct board *b)
61 struct replay *r = calloc2(1, sizeof(struct replay));
63 r->debug_level = 1;
65 if (arg) {
66 char *optspec, *next = arg;
67 while (*next) {
68 optspec = next;
69 next += strcspn(next, ",");
70 if (*next) { *next++ = 0; } else { *next = 0; }
72 char *optname = optspec;
73 char *optval = strchr(optspec, '=');
74 if (optval) *optval++ = 0;
76 if (!strcasecmp(optname, "debug")) {
77 if (optval)
78 r->debug_level = atoi(optval);
79 else
80 r->debug_level++;
81 } else if (!strcasecmp(optname, "playout") && optval) {
82 char *playoutarg = strchr(optval, ':');
83 if (playoutarg)
84 *playoutarg++ = 0;
85 if (!strcasecmp(optval, "moggy")) {
86 r->playout = playout_moggy_init(playoutarg, b, joseki_load(b->size));
87 } else if (!strcasecmp(optval, "light")) {
88 r->playout = playout_light_init(playoutarg, b);
89 } else if (!strcasecmp(optval, "elo")) {
90 r->playout = playout_elo_init(playoutarg, b);
91 } else {
92 fprintf(stderr, "Replay: Invalid playout policy %s\n", optval);
94 } else {
95 fprintf(stderr, "Replay: Invalid engine argument %s or missing value\n", optname);
100 if (!r->playout)
101 r->playout = playout_light_init(NULL, b);
102 r->playout->debug_level = r->debug_level;
104 return r;
107 struct engine *
108 engine_replay_init(char *arg, struct board *b)
110 struct replay *r = replay_state_init(arg, b);
111 struct engine *e = calloc2(1, sizeof(struct engine));
112 e->name = "PlayoutReplay Engine";
113 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.";
114 e->genmove = replay_genmove;
115 e->data = r;
117 return e;