From f40be8a828fa1a19f55a229f23696c8d8936129f Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Mon, 22 Sep 2008 03:19:10 +0200 Subject: [PATCH] Add new engine method notify_play This enables UCT to do promotions properly and before the move is played, so that we can check symmetry appropriately. --- engine.h | 2 ++ gtp.c | 1 + uct/uct.c | 49 ++++++++++++++++++++++++++++--------------------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/engine.h b/engine.h index faf71c7..74959a3 100644 --- a/engine.h +++ b/engine.h @@ -6,11 +6,13 @@ struct board; struct engine; +typedef void (*engine_notify_play)(struct engine *e, struct board *b, struct move *m); typedef coord_t *(*engine_genmove)(struct engine *e, struct board *b, enum stone color); struct engine { char *name; char *comment; + engine_notify_play notify_play; engine_genmove genmove; void *data; }; diff --git a/gtp.c b/gtp.c index 8eed8e1..5114e66 100644 --- a/gtp.c +++ b/gtp.c @@ -130,6 +130,7 @@ gtp_parse(struct board *board, struct engine *engine, char *buf) if (DEBUGL(1)) fprintf(stderr, "got move %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board)); + engine->notify_play(engine, board, &m); if (board_play(board, &m) < 0) { if (DEBUGL(0)) { fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board)); diff --git a/uct/uct.c b/uct/uct.c index bed0a98..de13aa2 100644 --- a/uct/uct.c +++ b/uct/uct.c @@ -169,40 +169,46 @@ end: return result; } -static coord_t * -uct_genmove(struct engine *e, struct board *b, enum stone color) +static void +prepare_move(struct engine *e, struct board *b, enum stone color, coord_t promote) { struct uct *u = e->data; - bool loaded = false; - if (b->moves < 2 && u->t) { + if (!b->moves && u->t) { /* Stale state from last game */ tree_done(u->t); u->t = NULL; } if (!u->t) { -tree_init: u->t = tree_init(b, color); //board_print(b, stderr); + tree_load(u->t, b); + } - if (!b->moves) { - tree_load(u->t, b); - } else if (b->moves == 1 && !loaded) { - tree_load(u->t, b); - loaded = true; - goto promotion; - } - } else { - /* XXX: We hope that the opponent didn't suddenly play - * several moves in the row. */ -promotion: - if (!tree_promote_at(u->t, b, b->last_move.coord)) { - fprintf(stderr, "CANNOT FIND NODE TO PROMOTE!\n"); - tree_done(u->t); - goto tree_init; - } + /* XXX: We hope that the opponent didn't suddenly play + * several moves in the row. */ + if (!is_resign(promote) && !tree_promote_at(u->t, b, promote)) { + fprintf(stderr, "CANNOT FIND NODE TO PROMOTE!\n"); + /* Reset tree */ + tree_done(u->t); + u->t = tree_init(b, color); } +} + +static void +uct_notify_play(struct engine *e, struct board *b, struct move *m) +{ + prepare_move(e, b, stone_other(m->color), m->coord); +} + +static coord_t * +uct_genmove(struct engine *e, struct board *b, enum stone color) +{ + struct uct *u = e->data; + + /* Seed the tree. */ + prepare_move(e, b, color, resign); int i, games = u->games - (u->t->root->u.playouts / 1.5); for (i = 0; i < games; i++) { @@ -378,6 +384,7 @@ engine_uct_init(char *arg) e->name = "UCT Engine"; e->comment = "I'm playing UCT. When we both pass, I will consider all the stones on the board alive. If you are reading this, write 'yes'. Please bear with me at the game end, I need to fill the whole board; if you help me, we will both be happier. Filling the board will not lose points (NZ rules)."; e->genmove = uct_genmove; + e->notify_play = uct_notify_play; e->data = u; return e; -- 2.11.4.GIT