From bc9bca49ec4adfb9211e8429eb1d45021d9127c8 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sat, 20 Feb 2010 00:31:40 +0100 Subject: [PATCH] board_is_valid_play(): Introduce to avoid stupid struct move setups --- board.h | 19 +++++++++++++------ playout/moggy.c | 12 +++++------- uct/tree.c | 4 +--- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/board.h b/board.h index 7f91ec3..b9917b1 100644 --- a/board.h +++ b/board.h @@ -289,6 +289,7 @@ typedef bool (*ppr_permit)(void *data, struct board *b, struct move *m); void board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data); /* Returns true if given move can be played. */ +static bool board_is_valid_play(struct board *b, enum stone color, coord_t coord); static bool board_is_valid_move(struct board *b, struct move *m); /* Returns true if ko was just taken. */ static bool board_playing_ko_threat(struct board *b); @@ -389,21 +390,21 @@ board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color) } static inline bool -board_is_valid_move(struct board *board, struct move *m) +board_is_valid_play(struct board *board, enum stone color, coord_t coord) { - if (board_at(board, m->coord) != S_NONE) + if (board_at(board, coord) != S_NONE) return false; - if (!board_is_eyelike(board, &m->coord, stone_other(m->color))) + if (!board_is_eyelike(board, &coord, stone_other(color))) return true; /* Play within {true,false} eye-ish formation */ - if (board->ko.coord == m->coord && board->ko.color == m->color) + if (board->ko.coord == coord && board->ko.color == color) return false; #ifdef BOARD_TRAITS /* XXX: Disallows suicide. */ - return trait_at(board, m->coord, m->color).cap > 0; + return trait_at(board, coord, color).cap > 0; #else int groups_in_atari = 0; - foreach_neighbor(board, m->coord, { + foreach_neighbor(board, coord, { group_t g = group_at(board, c); groups_in_atari += (board_group_info(board, g).libs == 1); }); @@ -412,6 +413,12 @@ board_is_valid_move(struct board *board, struct move *m) } static inline bool +board_is_valid_move(struct board *board, struct move *m) +{ + return board_is_valid_play(board, m->color, m->coord); +} + +static inline bool board_playing_ko_threat(struct board *b) { return !is_pass(b->ko.coord); diff --git a/playout/moggy.c b/playout/moggy.c index 5f75902..0b3e6ff 100644 --- a/playout/moggy.c +++ b/playout/moggy.c @@ -297,8 +297,8 @@ can_play_on_lib(struct playout_policy *p, struct board_state *s, fprintf(stderr, "can capture group %d (%s)?\n", g, coord2sstr(capture, b)); /* Does playing on the liberty usefully capture the group? */ - struct move m; m.color = to_play; m.coord = capture; - if (board_is_valid_move(b, &m) && !is_bad_selfatari(b, to_play, capture)) { + if (board_is_valid_play(b, to_play, capture) + && !is_bad_selfatari(b, to_play, capture)) { group_trait_set(s, g, to_play, capturable, true); return true; } @@ -537,8 +537,7 @@ check_group_atari(struct board *b, group_t group, enum stone owner, for (int i = 0; i < 2; i++) { coord_t lib = board_group_info(b, group).lib[i]; assert(board_at(b, lib) == S_NONE); - struct move m; m.color = to_play; m.coord = lib; - if (!board_is_valid_move(b, &m)) + if (!board_is_valid_play(b, to_play, lib)) continue; /* Don't play at the spot if it is extremely short @@ -654,10 +653,9 @@ playout_moggy_choose(struct playout_policy *p, struct board *b, enum stone to_pl /* Ko fight check */ if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord) + && b->moves - b->last_ko_age < pp->koage && pp->korate > fast_random(100)) { - struct move pm = { .color = to_play, .coord = b->last_ko.coord }; - if (b->moves - b->last_ko_age < pp->koage - && board_is_valid_move(b, &pm) + if (board_is_valid_play(b, to_play, b->last_ko.coord) && !is_bad_selfatari(b, to_play, b->last_ko.coord)) return b->last_ko.coord; } diff --git a/uct/tree.c b/uct/tree.c index dc3e102..7dcd322 100644 --- a/uct/tree.c +++ b/uct/tree.c @@ -702,13 +702,11 @@ tree_expand_node(struct tree *t, struct tree_node *node, struct board *b, enum s bool map_consider[board_size2(b) + 1]; map.consider = &map_consider[1]; memset(map_prior, 0, sizeof(map_prior)); memset(map_consider, 0, sizeof(map_consider)); - struct move pm = { .color = color }; map.consider[pass] = true; foreach_point(b) { if (board_at(b, c) != S_NONE) continue; - pm.coord = c; - if (!board_is_valid_move(b, &pm)) + if (!board_is_valid_play(b, color, c)) continue; map.consider[c] = true; } foreach_point_end; -- 2.11.4.GIT