From 5bab4e0f0027f58042137e2b436f9cff528d67f2 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 5 May 2010 13:15:39 +0200 Subject: [PATCH] Random: Rewrite genmove to deal with suicides reasonably Pointed out by Vlad Vlastin. --- random/random.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/random/random.c b/random/random.c index 0b8ca60..c0c9356 100644 --- a/random/random.c +++ b/random/random.c @@ -9,19 +9,27 @@ static coord_t * random_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive) { - struct board b2; - board_copy(&b2, b); - + /* Play a random coordinate. However, we must also guard + * against suicide moves; repeat playing while it's a suicide + * unless we keep suiciding; in that case, we probably don't + * have any other moves available and we pass. */ coord_t coord; - board_play_random(&b2, color, &coord, NULL, NULL); - if (!group_at(&b2, coord)) { - /* This was suicide. Just pass. */ - /* XXX: We should check for non-suicide alternatives. */ - return coord_pass(); - } - - board_done_noalloc(&b2); - return coord_copy(coord); + int i = 0; bool suicide = false; + + do { + /* board_play_random() actually plays the move too; + * this is desirable for MC simulations but not within + * the genmove. Make a scratch new board for it. */ + struct board b2; + board_copy(&b2, b); + + board_play_random(&b2, color, &coord, NULL, NULL); + + suicide = (coord != pass && !group_at(&b2, coord)); + board_done_noalloc(&b2); + } while (suicide && i++ < 100); + + return coord_copy(suicide ? pass : coord); } struct engine * -- 2.11.4.GIT