Distributed engine: Support merging stats received from slaves
[pachi.git] / replay / replay.c
blobdd7c72d6a4d0555f8a3089c2e141643d0a418480
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "board.h"
5 #include "debug.h"
6 #include "engine.h"
7 #include "move.h"
8 #include "playout.h"
9 #include "playout/elo.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;
27 coord_t coord = r->playout->choose(r->playout, b, color);
29 if (!is_pass(coord)) {
30 struct move m;
31 m.coord = coord; m.color = color;
32 if (board_play(b, &m) >= 0)
33 goto have_move;
35 if (DEBUGL(2)) {
36 fprintf(stderr, "Pre-picked move %d,%d is ILLEGAL:\n",
37 coord_x(coord, b), coord_y(coord, b));
38 board_print(b, stderr);
42 /* Defer to uniformly random move choice. */
43 board_play_random(b, color, &coord, (ppr_permit) r->playout->permit, r->playout);
45 have_move:
46 if (!group_at(b, coord)) {
47 /* This was suicide. Just pass. */
48 /* XXX: We should check for non-suicide alternatives. */
49 return coord_pass();
52 return coord_copy(coord);
56 struct replay *
57 replay_state_init(char *arg, struct board *b)
59 struct replay *r = calloc2(1, sizeof(struct replay));
61 r->debug_level = 1;
63 if (arg) {
64 char *optspec, *next = arg;
65 while (*next) {
66 optspec = next;
67 next += strcspn(next, ",");
68 if (*next) { *next++ = 0; } else { *next = 0; }
70 char *optname = optspec;
71 char *optval = strchr(optspec, '=');
72 if (optval) *optval++ = 0;
74 if (!strcasecmp(optname, "debug")) {
75 if (optval)
76 r->debug_level = atoi(optval);
77 else
78 r->debug_level++;
79 } else if (!strcasecmp(optname, "playout") && optval) {
80 char *playoutarg = strchr(optval, ':');
81 if (playoutarg)
82 *playoutarg++ = 0;
83 if (!strcasecmp(optval, "moggy")) {
84 r->playout = playout_moggy_init(playoutarg, b);
85 } else if (!strcasecmp(optval, "light")) {
86 r->playout = playout_light_init(playoutarg, b);
87 } else if (!strcasecmp(optval, "elo")) {
88 r->playout = playout_elo_init(playoutarg, b);
89 } else {
90 fprintf(stderr, "Replay: Invalid playout policy %s\n", optval);
92 } else {
93 fprintf(stderr, "Replay: Invalid engine argument %s or missing value\n", optname);
98 if (!r->playout)
99 r->playout = playout_light_init(NULL, b);
100 r->playout->debug_level = r->debug_level;
102 return r;
105 struct engine *
106 engine_replay_init(char *arg, struct board *b)
108 struct replay *r = replay_state_init(arg, b);
109 struct engine *e = calloc2(1, sizeof(struct engine));
110 e->name = "PlayoutReplay Engine";
111 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.";
112 e->genmove = replay_genmove;
113 e->data = r;
115 return e;