From 2da3fd0ec15e682cadd47debc7b4083feae6c47b Mon Sep 17 00:00:00 2001 From: lemonsqueeze Date: Sat, 2 Mar 2019 00:21:05 +0100 Subject: [PATCH] board: board_size() redefinition before: board_size(b) padded board size, ie 21 for 19x19 real_board_size(b) real board size, ie 19 for 19x19 board_size2(b) (board_size(b) * board_size(b)) now: board_rsize(b) real board size, ie 19 for 19x19 board_stride(b) padded board size, ie 21 for 19x19 board_max_coords(b) (board_stride(b) * board_stride(b)) api: board, joseki, move anything that mentions size is *real* board size --- board.c | 140 +++++++++++++++++++++----------------- board.h | 59 +++++++--------- board_play.h | 10 ++- dcnn.c | 6 +- dcnn.h | 2 +- distributed/distributed.c | 8 +-- engines/josekiscan.c | 6 +- engines/montecarlo.c | 29 ++++---- engines/replay.c | 10 +-- fbook.c | 27 ++++---- gogui.c | 2 +- joseki.c | 14 ++-- move.c | 7 +- move.h | 6 +- ownermap.c | 15 +--- ownermap.h | 3 +- pachi.c | 2 +- patternsp.c | 7 +- patternsp.h | 4 +- playout.c | 2 +- playout/moggy.c | 6 +- t-unit/board_regtest.c | 6 +- t-unit/test.c | 6 +- t-unit/test_undo.c | 4 +- tactics/seki.c | 4 +- tactics/seki.h | 4 +- tactics/util.c | 10 +-- tactics/util.h | 7 +- timeinfo.c | 6 +- uct/dynkomi.c | 6 +- uct/policy/ucb1amaf.c | 4 +- uct/search.c | 2 +- uct/slave.c | 2 +- uct/tree.c | 24 +++---- uct/uct.c | 4 +- uct/walk.c | 4 +- 36 files changed, 230 insertions(+), 228 deletions(-) diff --git a/board.c b/board.c index 8690227d8..b6b824518 100644 --- a/board.c +++ b/board.c @@ -44,19 +44,19 @@ board_setup(board_t *b) } void -board_init(board_t *b, int bsize, char *fbookfile) +board_init(board_t *b, int size, char *fbookfile) { board_setup(b); b->fbookfile = fbookfile; - b->size = bsize; + b->rsize = size; board_clear(b); } board_t * -board_new(int bsize, char *fbookfile) +board_new(int size, char *fbookfile) { board_t *b = malloc2(board_t); - board_init(b, bsize, fbookfile); + board_init(b, size, fbookfile); return b; } @@ -96,10 +96,10 @@ void board_resize(board_t *board, int size) { #ifdef BOARD_SIZE - assert(board_size(board) == size + 2); + assert(board_rsize(board) == size); #endif assert(size <= BOARD_MAX_SIZE); - board->size = size + 2; /* S_OFFBOARD margin */ + board->rsize = size; } board_statics_t board_statics = { 0, }; @@ -107,37 +107,39 @@ board_statics_t board_statics = { 0, }; static void board_statics_init(board_t *board) { - int size = board_size(board); + int size = board_rsize(board); + int stride = size + 2; board_statics_t *bs = &board_statics; - if (bs->size == size) + if (bs->rsize == size) return; memset(bs, 0, sizeof(*bs)); - bs->size = size; - bs->size2 = size * size; - bs->real_size2 = (size-2) * (size-2); + bs->rsize = size; + bs->stride = stride; + bs->max_coords = stride * stride; bs->bits2 = 1; - while ((1 << bs->bits2) < bs->size2) bs->bits2++; + while ((1 << bs->bits2) < bs->max_coords) bs->bits2++; /* Setup neighborhood iterators */ - bs->nei8[0] = -size - 1; // (-1,-1) + bs->nei8[0] = -stride - 1; // (-1,-1) bs->nei8[1] = 1; bs->nei8[2] = 1; - bs->nei8[3] = size - 2; // (-1,0) + bs->nei8[3] = stride - 2; // (-1,0) bs->nei8[4] = 2; - bs->nei8[5] = size - 2; // (-1,1) + bs->nei8[5] = stride - 2; // (-1,1) bs->nei8[6] = 1; bs->nei8[7] = 1; - bs->dnei[0] = -size - 1; + + bs->dnei[0] = -stride - 1; bs->dnei[1] = 2; - bs->dnei[2] = size*2 - 2; + bs->dnei[2] = stride*2 - 2; bs->dnei[3] = 2; /* Set up coordinate cache */ foreach_point(board) { - bs->coord[c][0] = c % board_size(board); - bs->coord[c][1] = c / board_size(board); + bs->coord[c][0] = c % stride; + bs->coord[c][1] = c / stride; } foreach_point_end; /* Initialize zobrist hashtable. */ @@ -173,33 +175,34 @@ board_statics_init(board_t *board) static void board_init_data(board_t *board) { - int size = board_size(board); + int size = board_rsize(board); + int stride = board_stride(board); board_setup(board); - board_resize(board, size - 2 /* S_OFFBOARD margin */); + board_resize(board, size); /* Setup initial symmetry */ if (size % 2) { board->symmetry.d = 1; - board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2; - board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1; + board->symmetry.x1 = board->symmetry.y1 = stride / 2; + board->symmetry.x2 = board->symmetry.y2 = stride - 1; board->symmetry.type = SYM_FULL; } else { /* TODO: We do not handle board symmetry on boards * with no tengen yet. */ board->symmetry.d = 0; board->symmetry.x1 = board->symmetry.y1 = 1; - board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1; + board->symmetry.x2 = board->symmetry.y2 = stride - 1; board->symmetry.type = SYM_NONE; } /* Draw the offboard margin */ - int top_row = board_size2(board) - board_size(board); + int top_row = board_max_coords(board) - stride; int i; - for (i = 0; i < board_size(board); i++) + for (i = 0; i < stride; i++) board->b[i] = board->b[top_row + i] = S_OFFBOARD; - for (i = 0; i <= top_row; i += board_size(board)) - board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD; + for (i = 0; i <= top_row; i += stride) + board->b[i] = board->b[i + stride - 1] = S_OFFBOARD; foreach_point(board) { coord_t coord = c; @@ -211,9 +214,11 @@ board_init_data(board_t *board) } foreach_point_end; /* All positions are free! Except the margin. */ - for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++) - if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1) - board_addf(board, i); + foreach_point(board) { + if (board_at(board, c) == S_NONE) + board_addf(board, c); + } foreach_point_end; + assert(board->flen == size * size); #ifdef BOARD_PAT3 /* Initialize 3x3 pattern codes. */ @@ -227,7 +232,7 @@ board_init_data(board_t *board) void board_clear(board_t *board) { - int size = board_size(board); + int size = board_rsize(board); floating_t komi = board->komi; char *fbookfile = board->fbookfile; enum rules rules = board->rules; @@ -236,8 +241,8 @@ board_clear(board_t *board) board_statics_init(board); static board_t bcache[BOARD_MAX_SIZE + 2]; - assert(size > 0 && size <= BOARD_MAX_SIZE + 2); - if (bcache[size - 1].size == size) + assert(size > 1 && size <= BOARD_MAX_SIZE); + if (bcache[size - 1].rsize == size) board_copy(board, &bcache[size - 1]); else { board_init_data(board); @@ -255,17 +260,18 @@ board_clear(board_t *board) static void board_print_top(board_t *board, strbuf_t *buf, int c) { + int size = board_rsize(board); for (int i = 0; i < c; i++) { char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ"; sbprintf(buf, " "); - for (int x = 1; x < board_size(board) - 1; x++) + for (int x = 1; x <= size; x++) sbprintf(buf, "%c ", asdf[x - 1]); sbprintf(buf, " "); } sbprintf(buf, "\n"); for (int i = 0; i < c; i++) { sbprintf(buf, " +-"); - for (int x = 1; x < board_size(board) - 1; x++) + for (int x = 1; x <= size; x++) sbprintf(buf, "--"); sbprintf(buf, "+"); } @@ -275,9 +281,10 @@ board_print_top(board_t *board, strbuf_t *buf, int c) static void board_print_bottom(board_t *board, strbuf_t *buf, int c) { + int size = board_rsize(board); for (int i = 0; i < c; i++) { sbprintf(buf, " +-"); - for (int x = 1; x < board_size(board) - 1; x++) + for (int x = 1; x <= size; x++) sbprintf(buf, "--"); sbprintf(buf, "+"); } @@ -287,8 +294,9 @@ board_print_bottom(board_t *board, strbuf_t *buf, int c) static void board_print_row(board_t *board, int y, strbuf_t *buf, board_cprint cprint, void *data) { + int size = board_rsize(board); sbprintf(buf, " %2d | ", y); - for (int x = 1; x < board_size(board) - 1; x++) + for (int x = 1; x <= size; x++) if (coord_x(last_move(board).coord) == x && coord_y(last_move(board).coord) == y) sbprintf(buf, "%c)", stone2char(board_atxy(board, x, y))); else @@ -296,7 +304,7 @@ board_print_row(board_t *board, int y, strbuf_t *buf, board_cprint cprint, void sbprintf(buf, "|"); if (cprint) { sbprintf(buf, " %2d | ", y); - for (int x = 1; x < board_size(board) - 1; x++) + for (int x = 1; x <= size; x++) cprint(board, coord_xy(x, y), buf, data); sbprintf(buf, "|"); } @@ -309,14 +317,18 @@ board_print_custom(board_t *board, FILE *f, board_cprint cprint, void *data) char buffer[10240]; strbuf_t strbuf; strbuf_t *buf = strbuf_init(&strbuf, buffer, sizeof(buffer)); + int size = board_rsize(board); + sbprintf(buf, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d ", board->moves, board->komi, board->handicap, - board->captures[S_BLACK], board->captures[S_WHITE]); + board->captures[S_BLACK], board->captures[S_WHITE]); + if (cprint) /* handler can add things to header when called with pass */ cprint(board, pass, buf, data); sbprintf(buf, "\n"); + board_print_top(board, buf, 1 + !!cprint); - for (int y = board_size(board) - 2; y >= 1; y--) + for (int y = size; y >= 1; y--) board_print_row(board, y, buf, cprint, data); board_print_bottom(board, buf, 1 + !!cprint); fprintf(f, "%s\n", buf->str); @@ -325,8 +337,9 @@ board_print_custom(board_t *board, FILE *f, board_cprint cprint, void *data) static void board_hprint_row(board_t *board, int y, strbuf_t *buf, board_print_handler handler, void *data) { + int size = board_rsize(board); sbprintf(buf, " %2d | ", y); - for (int x = 1; x < board_size(board) - 1; x++) { + for (int x = 1; x <= size; x++) { char *stone_str = handler(board, coord_xy(x, y), data); if (coord_x(last_move(board).coord) == x && coord_y(last_move(board).coord) == y) sbprintf(buf, "%s)", stone_str); @@ -342,11 +355,14 @@ board_hprint(board_t *board, FILE *f, board_print_handler handler, void *data) char buffer[10240]; strbuf_t strbuf; strbuf_t *buf = strbuf_init(&strbuf, buffer, sizeof(buffer)); + int size = board_rsize(board); + sbprintf(buf, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n", board->moves, board->komi, board->handicap, board->captures[S_BLACK], board->captures[S_WHITE]); + board_print_top(board, buf, 1); - for (int y = board_size(board) - 2; y >= 1; y--) + for (int y = size; y >= 1; y--) board_hprint_row(board, y, buf, handler, data); board_print_bottom(board, buf, 1); fprintf(f, "%s\n", buf->str); @@ -394,7 +410,7 @@ board_coord_in_symmetry(board_t *b, coord_t c) if (b->symmetry.d) { int x = coord_x(c); if (b->symmetry.type == SYM_DIAG_DOWN) - x = board_size(b) - 1 - x; + x = board_stride(b) - 1 - x; if (x > coord_y(c)) return false; } @@ -411,8 +427,8 @@ board_symmetry_update(board_t *b, board_symmetry_t *symmetry, coord_t c) return; } - int x = coord_x(c), y = coord_y(c), t = board_size(b) / 2; - int dx = board_size(b) - 1 - x; /* for SYM_DOWN */ + int x = coord_x(c), y = coord_y(c), t = board_stride(b) / 2; + int dx = board_stride(b) - 1 - x; /* for SYM_DOWN */ if (DEBUGL(6)) fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n", symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2, @@ -427,28 +443,28 @@ board_symmetry_update(board_t *b, board_symmetry_t *symmetry, coord_t c) if (x == y) { symmetry->type = SYM_DIAG_UP; symmetry->x1 = symmetry->y1 = 1; - symmetry->x2 = symmetry->y2 = board_size(b) - 1; + symmetry->x2 = symmetry->y2 = board_stride(b) - 1; symmetry->d = 1; } else if (dx == y) { symmetry->type = SYM_DIAG_DOWN; symmetry->x1 = symmetry->y1 = 1; - symmetry->x2 = symmetry->y2 = board_size(b) - 1; + symmetry->x2 = symmetry->y2 = board_stride(b) - 1; symmetry->d = 1; } else if (x == t) { symmetry->type = SYM_HORIZ; symmetry->y1 = 1; - symmetry->y2 = board_size(b) - 1; + symmetry->y2 = board_stride(b) - 1; symmetry->d = 0; } else if (y == t) { symmetry->type = SYM_VERT; symmetry->x1 = 1; - symmetry->x2 = board_size(b) - 1; + symmetry->x2 = board_stride(b) - 1; symmetry->d = 0; } else { break_symmetry: symmetry->type = SYM_NONE; symmetry->x1 = symmetry->y1 = 1; - symmetry->x2 = symmetry->y2 = board_size(b) - 1; + symmetry->x2 = symmetry->y2 = board_stride(b) - 1; symmetry->d = 0; } break; @@ -496,10 +512,10 @@ void board_handicap(board_t *board, int stones, move_queue_t *q) { assert(stones <= 9); - int margin = 3 + (board_size(board) >= 13); + int margin = 3 + (board_rsize(board) >= 13); int min = margin; - int mid = board_size(board) / 2; - int max = board_size(board) - 1 - margin; + int mid = board_stride(board) / 2; + int max = board_stride(board) - 1 - margin; const int places[][2] = { { min, min }, { max, max }, { max, min }, { min, max }, { min, mid }, { max, mid }, @@ -708,8 +724,9 @@ board_score(board_t *b, int scores[S_MAX]) void board_print_official_ownermap(board_t *b, int *final_ownermap) { - for (int y = board_size(b) - 2; y >= 1; y--) { - for (int x = 1; x < board_size(b) - 1; x++) { + int size = board_rsize(b); + for (int y = size; y >= 1; y--) { + for (int x = 1; x <= size; x++) { coord_t c = coord_xy(x, y); char *chars = ".XO:"; fprintf(stderr, "%c ", chars[final_ownermap[c]]); @@ -781,7 +798,7 @@ floating_t board_official_score(board_t *b, move_queue_t *dead) { int dame, seki; - int ownermap[board_size2(b)]; + int ownermap[board_max_coords(b)]; return board_official_score_details(b, dead, &dame, &seki, ownermap, NULL); } @@ -961,10 +978,11 @@ board_capturable_add(board_t *board, group_t group, coord_t lib) #ifdef WANT_BOARD_C /* Update the list of capturable groups. */ assert(group); - assert(board->clen < board_size2(board)); + assert(board->clen < BOARD_MAX_GROUPS); board->c[board->clen++] = group; #endif } + static void board_capturable_rm(board_t *board, group_t group, coord_t lib) { @@ -984,7 +1002,7 @@ board_capturable_rm(board_t *board, group_t group, coord_t lib) board->c[i] = board->c[--board->clen]; return; } - fprintf(stderr, "rm of bad group %d\n", group_base(group)); + fprintf(stderr, "rm of bad group %s\n", coord2sstr(group_base(group))); assert(0); #endif } @@ -999,6 +1017,6 @@ board_play(board_t *b, move_t *m) #ifdef BOARD_UNDO_CHECKS assert(!b->quicked); #endif - - return board_play_(b, m); + + return board_play_(b, m); } diff --git a/board.h b/board.h index 4b1c3c663..0ebfce733 100644 --- a/board.h +++ b/board.h @@ -33,7 +33,7 @@ struct ownermap; /* Maximum supported board size. (Without the S_OFFBOARD edges.) */ #define BOARD_MAX_SIZE 19 -#define BOARD_MAX_COORDS ((BOARD_MAX_SIZE+2) * (BOARD_MAX_SIZE+2) ) +#define BOARD_MAX_COORDS ((BOARD_MAX_SIZE+2) * (BOARD_MAX_SIZE+2)) #define BOARD_MAX_MOVES (BOARD_MAX_SIZE * BOARD_MAX_SIZE) #define BOARD_MAX_GROUPS (BOARD_MAX_SIZE * BOARD_MAX_SIZE * 2 / 3) /* For 19x19, max 19*2*6 = 228 groups (stacking b&w stones, each third line empty) */ @@ -122,10 +122,10 @@ enum rules { /* Data shared by all boards of a given size */ typedef struct { - int size; - int size2; /* size^2 */ + int rsize; /* real board size (19x19: 19) */ + int stride; /* padded board size (19x19: 21) */ + int max_coords; /* stride^2 */ int bits2; /* ceiling(log2(size2)) */ - int real_size2; /* real_board_size^2 */ int nei8[8], dnei[4]; /* Iterator offsets for foreach_neighbor*() */ @@ -149,7 +149,7 @@ extern board_statics_t board_statics; * Always call functions below if you want to change it. */ typedef struct board { - int size; /* Including S_OFFBOARD margin - see below. */ + int rsize; /* Real board size (19x19: 19) */ int moves; int captures[S_MAX]; @@ -217,27 +217,26 @@ FB_ONLY(hash_t history_hash)[history_hash_size]; /* Board "history" - hashes enc #ifdef BOARD_SIZE -#define the_board_size() (BOARD_SIZE + 2) -#define board_size(b) (BOARD_SIZE + 2) -#define board_size2(b) (board_size(b) * board_size(b)) -#define real_board_size2(b) (BOARD_SIZE * BOARD_SIZE) +#define board_rsize(b) BOARD_SIZE +#define the_board_rsize() BOARD_SIZE +#define the_board_stride() (BOARD_SIZE + 2) +#define board_max_coords(b) (board_stride(b) * board_stride(b)) #else -#define the_board_size() (board_statics.size) -#define board_size(b) ((b)->size) -#define board_size2(b) (board_statics.size2) -#define real_board_size2(b) (board_statics.real_size2) +#define board_rsize(b) ((b)->rsize) +#define the_board_rsize() (board_statics.rsize) +#define the_board_stride() (board_statics.stride) +#define board_max_coords(b) (board_statics.max_coords) #endif -#define real_board_size(b) (board_size(b) - 2) -#define the_real_board_size() (the_board_size() - 2) +#define board_stride(b) (board_rsize(b) + 2) /* This is a shortcut for taking different action on smaller and large boards * (e.g. picking different variable defaults). This is of course less optimal than * fine-tuning dependency function of values on board size, but that is difficult * and possibly not very rewarding if you are interested just in 9x9 and 19x19. */ -#define board_large(b_) (board_size(b_)-2 >= 15) -#define board_small(b_) (board_size(b_)-2 <= 9) +#define board_large(b_) (board_rsize(b_) >= 15) +#define board_small(b_) (board_rsize(b_) <= 9) #if BOARD_SIZE == 19 # define board_bits2(b_) 9 @@ -257,11 +256,11 @@ FB_ONLY(hash_t history_hash)[history_hash_size]; /* Board "history" - hashes enc #define last_move_previ(b, n) ((BOARD_LAST_N + b->last_move_i - (n)) % BOARD_LAST_N) #define last_moven(b, n) ((b)->last_moves[last_move_previ(b, n)]) -#define board_at(b_, c) ((b_)->b[c]) -#define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)]) +#define board_at(b_, c) ((b_)->b[c]) +#define board_atxy(b_, x, y) ((b_)->b[coord_xy(x, y)]) -#define group_at(b_, c) ((b_)->g[c]) -#define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)]) +#define group_at(b_, c) ((b_)->g[c]) +#define group_atxy(b_, x, y) ((b_)->g[coord_xy(x, y)]) /* Warning! Neighbor count is not kept up-to-date for S_NONE! */ #define neighbor_count_at(b_, coord, color) ((b_)->n[coord].colors[(enum stone) color]) @@ -271,7 +270,6 @@ FB_ONLY(hash_t history_hash)[history_hash_size]; /* Board "history" - hashes enc #define immediate_liberty_count(b_, coord) (4 - neighbor_count_at(b_, coord, S_BLACK) - neighbor_count_at(b_, coord, S_WHITE) - neighbor_count_at(b_, coord, S_OFFBOARD)) #define groupnext_at(b_, c) ((b_)->p[c]) -#define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)]) #define group_base(g_) (g_) #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0) @@ -281,18 +279,17 @@ FB_ONLY(hash_t history_hash)[history_hash_size]; /* Board "history" - hashes enc #define board_group_other_lib(b_, g_, l_) (board_group_info(b_, g_).lib[board_group_info(b_, g_).lib[0] != (l_) ? 0 : 1]) #ifdef BOARD_HASH_COMPAT -#define hash_at(coord, color) (*(&board_statics.h[0][0] + ((color) == S_BLACK ? board_statics.size2 : 0) + (coord))) +#define hash_at(coord, color) (*(&board_statics.h[0][0] + ((color) == S_BLACK ? board_statics.max_coords : 0) + (coord))) #else #define hash_at(coord, color) (board_statics.h[coord][(color) == S_BLACK]) #endif -void board_init(board_t *b, int bsize, char *fbookfile); -board_t *board_new(int bsize, char *fbookfile); +void board_init(board_t *b, int size, char *fbookfile); +board_t *board_new(int size, char *fbookfile); board_t *board_copy(board_t *board2, board_t *board1); void board_done_noalloc(board_t *board); void board_done(board_t *board); -/* size here is without the S_OFFBOARD margin. */ void board_resize(board_t *b, int size); void board_clear(board_t *board); @@ -373,11 +370,7 @@ const char *rules2str(enum rules rules); #define foreach_point(board_) \ do { \ coord_t c = 0; \ - for (; c < board_size(board_) * board_size(board_); c++) -#define foreach_point_and_pass(board_) \ - do { \ - coord_t c = pass; \ - for (; c < board_size(board_) * board_size(board_); c++) + for (; c < board_max_coords(board_); c++) #define foreach_point_end \ } while (0) @@ -403,10 +396,10 @@ const char *rules2str(enum rules rules); do { \ coord_t coord__ = coord_; \ coord_t c; \ - c = coord__ - board_size(board_); do { loop_body } while (0); \ + c = coord__ - board_stride(board_); do { loop_body } while (0); \ c = coord__ - 1; do { loop_body } while (0); \ c = coord__ + 1; do { loop_body } while (0); \ - c = coord__ + board_size(board_); do { loop_body } while (0); \ + c = coord__ + board_stride(board_); do { loop_body } while (0); \ } while (0) #define foreach_8neighbor(board_, coord_) \ diff --git a/board_play.h b/board_play.h index 873b55ba2..6a59d8fcb 100644 --- a/board_play.h +++ b/board_play.h @@ -29,7 +29,7 @@ static void board_group_find_extra_libs(board_t *board, group_t group, group_info_t *gi, coord_t avoid) { /* Add extra liberty from the board to our liberty list. */ - unsigned char watermark[board_size2(board) / 8]; + unsigned char watermark[board_max_coords(board) / 8]; memset(watermark, 0, sizeof(watermark)); #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7))) #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7)) @@ -156,11 +156,9 @@ add_to_group(board_t *board, group_t group, coord_t prevstone, coord_t coord) }); if (DEBUGL(8)) - fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n", - coord_x(prevstone), coord_y(prevstone), - coord_x(coord), coord_y(coord), - groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board), - group_base(group)); + fprintf(stderr, "add_to_group: added (%s ->) %s (-> %s) to group %s\n", + coord2sstr(prevstone), coord2sstr(coord), coord2sstr(groupnext_at(board, coord)), + coord2sstr(group_base(group))); } static void profiling_noinline diff --git a/dcnn.c b/dcnn.c index de6f4efbb..8681776e0 100644 --- a/dcnn.c +++ b/dcnn.c @@ -20,7 +20,7 @@ static void detlef54_dcnn_eval(board_t *b, enum stone color, float result[]); static bool dcnn_supported_board_size(board_t *b) { - return (real_board_size(b) >= 13); + return (board_rsize(b) >= 13); } bool @@ -35,7 +35,7 @@ void dcnn_init(board_t *b) { if (dcnn_enabled && dcnn_supported_board_size(b)) - caffe_init(real_board_size(b)); + caffe_init(board_rsize(b)); if (dcnn_required && !caffe_ready()) die("dcnn required, aborting.\n"); } @@ -58,7 +58,7 @@ detlef54_dcnn_eval(board_t *b, enum stone color, float result[]) { assert(dcnn_supported_board_size(b)); - int size = real_board_size(b); + int size = board_rsize(b); int dsize = 13 * size * size; float *data = calloc2(dsize, float); diff --git a/dcnn.h b/dcnn.h index e9ecca5f6..03846e1b6 100644 --- a/dcnn.h +++ b/dcnn.h @@ -24,7 +24,7 @@ static inline int coord2dcnn_idx(coord_t c); static inline int coord2dcnn_idx(coord_t c) { - int size = the_real_board_size(); + int size = the_board_rsize(); int x = coord_x(c) - 1; int y = coord_y(c) - 1; return (y * size + x); diff --git a/distributed/distributed.c b/distributed/distributed.c index d29fc815a..335acc447 100644 --- a/distributed/distributed.c +++ b/distributed/distributed.c @@ -222,7 +222,7 @@ select_best_move(board_t *b, large_stats_t *stats, int *played, assert(reply_count > 0); /* +2 for pass and resign */ - memset(stats-2, 0, (board_size2(b)+2) * sizeof(*stats)); + memset(stats-2, 0, (board_max_coords(b)+2) * sizeof(*stats)); coord_t best_move = pass; long best_playouts = 0; @@ -246,7 +246,7 @@ select_best_move(board_t *b, large_stats_t *stats, int *played, move_stats_t s; while (r && sscanf(++r, "%63s %d " PRIfloating, move, &s.playouts, &s.value) == 3) { coord_t c = str2coord(move); - assert (c >= resign && c < board_size2(b) && s.playouts >= 0); + assert (c >= resign && c < board_max_coords(b) && s.playouts >= 0); large_stats_add_result(&stats[c], s.value, (long)s.playouts); @@ -257,7 +257,7 @@ select_best_move(board_t *b, large_stats_t *stats, int *played, r = strchr(r, '\n'); } } - for (coord_t c = resign; c < board_size2(b); c++) + for (coord_t c = resign; c < board_max_coords(b); c++) stats[c].playouts /= reply_count; *keep_looking = keep > reply_count / 2; return best_move; @@ -319,7 +319,7 @@ distributed_genmove(engine_t *e, board_t *b, time_info_t *ti, /* Combined move stats from all slaves, only for children * of the root node, plus 2 for pass and resign. */ - large_stats_t stats_array[board_size2(b) + 2], *stats; + large_stats_t stats_array[board_max_coords(b) + 2], *stats; stats = &stats_array[2]; protocol_lock(); diff --git a/engines/josekiscan.c b/engines/josekiscan.c index ac88aca5c..5bd735fb2 100644 --- a/engines/josekiscan.c +++ b/engines/josekiscan.c @@ -30,10 +30,10 @@ josekiscan_play(engine_t *e, board_t *board, move_t *m, char *move_tags) if (!board->moves) { /* New game, reset state. */ - assert(board->size == joseki_dict->bsize); + assert(board_rsize(board) == joseki_dict->bsize); for (int i = 0; i < 16; i++) { - board_resize(j->b[i], board->size - 2); + board_resize(j->b[i], board_rsize(board)); board_clear(j->b[i]); } @@ -105,7 +105,7 @@ josekiscan_state_init(char *arg) josekiscan_t *j = calloc2(1, josekiscan_t); for (int i = 0; i < 16; i++) - j->b[i] = board_new(19+2, NULL); + j->b[i] = board_new(19, NULL); j->debug_level = 1; diff --git a/engines/montecarlo.c b/engines/montecarlo.c index 56ecb49dc..719b52c2d 100644 --- a/engines/montecarlo.c +++ b/engines/montecarlo.c @@ -64,29 +64,32 @@ typedef struct { static void board_stats_print(board_t *board, move_stat_t *moves, FILE *f) { + int size = board_rsize(board); fprintf(f, "\n "); int x, y; char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ"; - for (x = 1; x < board_size(board) - 1; x++) + for (x = 1; x <= size; x++) fprintf(f, "%c ", asdf[x - 1]); fprintf(f, "\n +-"); - for (x = 1; x < board_size(board) - 1; x++) + for (x = 1; x <= size; x++) fprintf(f, "-----"); fprintf(f, "+\n"); - for (y = board_size(board) - 2; y >= 1; y--) { + for (y = size; y >= 1; y--) { fprintf(f, "%2d | ", y); - for (x = 1; x < board_size(board) - 1; x++) - if (moves[y * board_size(board) + x].games) - fprintf(f, "%0.2f ", (floating_t) moves[y * board_size(board) + x].wins / moves[y * board_size(board) + x].games); + for (x = 1; x <= size; x++) { + coord_t c = coord_xy(x, y); + if (moves[c].games) + fprintf(f, "%0.2f ", (floating_t) moves[c].wins / moves[c].games); else fprintf(f, "---- "); + } fprintf(f, "| "); - for (x = 1; x < board_size(board) - 1; x++) - fprintf(f, "%4d ", moves[y * board_size(board) + x].games); + for (x = 1; x <= size; x++) + fprintf(f, "%4d ", moves[coord_xy(x, y)].games); fprintf(f, "|\n"); } fprintf(f, " +-"); - for (x = 1; x < board_size(board) - 1; x++) + for (x = 1; x <= size; x++) fprintf(f, "-----"); fprintf(f, "+\n"); } @@ -116,7 +119,7 @@ montecarlo_genmove(engine_t *e, board_t *b, time_info_t *ti, enum stone color, b /* We use [0] for pass. Normally, this is an inaccessible corner * of board margin. */ - move_stat_t moves[board_size2(b)]; + move_stat_t moves[board_max_coords(b)]; memset(moves, 0, sizeof(moves)); int losses = 0; @@ -198,8 +201,8 @@ montecarlo_genmove(engine_t *e, board_t *b, time_info_t *ti, enum stone color, b /* Simple heuristic: avoid opening too low. Do not * play on second or first line as first white or * first two black moves.*/ - if (coord_x(c) < 3 || coord_x(c) > board_size(b) - 4 - || coord_y(c) < 3 || coord_y(c) > board_size(b) - 4) + if (coord_x(c) < 3 || coord_x(c) > board_stride(b) - 4 + || coord_y(c) < 3 || coord_y(c) > board_stride(b) - 4) continue; } @@ -237,7 +240,7 @@ montecarlo_state_init(char *arg, board_t *b) mc->debug_level = 1; mc->gamelen = MC_GAMELEN; - joseki_load(b->size); + joseki_load(board_rsize(b)); if (arg) { char *optspec, *next = arg; diff --git a/engines/replay.c b/engines/replay.c index 7bfe637f3..a04f8aac5 100644 --- a/engines/replay.c +++ b/engines/replay.c @@ -75,14 +75,14 @@ replay_genmove(engine_t *e, board_t *b, time_info_t *ti, enum stone color, bool if (DEBUGL(3)) printf("genmove: %s to play. Sampling moves (%i runs)\n", stone2str(color), r->runs); - int played_[board_size2(b) + 1]; memset(played_, 0, sizeof(played_)); + int played_[board_max_coords(b) + 1]; memset(played_, 0, sizeof(played_)); int *played = played_ + 1; // allow storing pass int most_played = 0; m.coord = replay_sample_moves(e, b, color, played, &most_played); if (DEBUGL(3)) { /* Show moves stats */ for (int k = most_played; k > 0; k--) - for (coord_t c = pass; c < board_size2(b); c++) + for (coord_t c = pass; c < board_max_coords(b); c++) if (played[c] == k) fprintf(stderr, "%3s: %.2f%%\n", coord2sstr(c), (float)k * 100 / r->runs); fprintf(stderr, "\n"); @@ -120,12 +120,12 @@ replay_best_moves(engine_t *e, board_t *b, time_info_t *ti, enum stone color, if (DEBUGL(3)) printf("best_moves: %s to play. Sampling moves (%i runs)\n", stone2str(color), r->runs); - int played_[board_size2(b) + 1]; memset(played_, 0, sizeof(played_)); + int played_[board_max_coords(b) + 1]; memset(played_, 0, sizeof(played_)); int *played = played_ + 1; // allow storing pass int most_played = 0; replay_sample_moves(e, b, color, played, &most_played); - for (coord_t c = pass; c < board_size2(b); c++) + for (coord_t c = pass; c < board_max_coords(b); c++) best_moves_add(c, (float)played[c] / r->runs, best_c, best_r, nbest); } @@ -144,7 +144,7 @@ replay_state_init(char *arg, board_t *b) r->debug_level = 1; r->runs = 1000; r->no_suicide = 0; - joseki_load(b->size); + joseki_load(board_rsize(b)); if (arg) { char *optspec, *next = arg; diff --git a/fbook.c b/fbook.c index d70d2f7cf..bf3b593ea 100644 --- a/fbook.c +++ b/fbook.c @@ -11,21 +11,18 @@ #include "random.h" -static coord_t -coord_transform(board_t *b, coord_t coord, int i) -{ #define HASH_VMIRROR 1 #define HASH_HMIRROR 2 #define HASH_XYFLIP 4 - if (i & HASH_VMIRROR) { - coord = coord_xy(coord_x(coord), board_size(b) - 1 - coord_y(coord)); - } - if (i & HASH_HMIRROR) { - coord = coord_xy(board_size(b) - 1 - coord_x(coord), coord_y(coord)); - } - if (i & HASH_XYFLIP) { - coord = coord_xy(coord_y(coord), coord_x(coord)); - } + +static coord_t +coord_transform(board_t *b, coord_t coord, int i) +{ + int stride = board_stride(b); + int x = coord_x(coord); int y = coord_y(coord); + if (i & HASH_VMIRROR) coord = coord_xy(x, stride - 1 - y); + if (i & HASH_HMIRROR) coord = coord_xy(stride - 1 - x, y); + if (i & HASH_XYFLIP) coord = coord_xy(y, x); return coord; } @@ -64,7 +61,7 @@ static fbook_t *fbcache; fbook_t * fbook_init(char *filename, board_t *b) { - if (fbcache && fbcache->bsize == board_size(b) + if (fbcache && fbcache->bsize == board_rsize(b) && fbcache->handicap == b->handicap) return fbcache; @@ -75,7 +72,7 @@ fbook_init(char *filename, board_t *b) } fbook_t *fbook = calloc2(1, fbook_t); - fbook->bsize = board_size(b); + fbook->bsize = board_rsize(b); fbook->handicap = b->handicap; /* We do not set handicap=1 in case of too low komi on purpose; * we want to go with the no-handicap fbook for now. */ @@ -100,7 +97,7 @@ fbook_init(char *filename, board_t *b) * BSIZE COORD COORD COORD... | COORD * BSIZE/HANDI COORD COORD COORD... | COORD */ int bsize = strtol(line, &line, 10); - if (bsize != fbook->bsize - 2) + if (bsize != fbook->bsize) continue; int handi = 0; if (*line == '/') { diff --git a/gogui.c b/gogui.c index ba2d1d005..416d3c488 100644 --- a/gogui.c +++ b/gogui.c @@ -439,7 +439,7 @@ cmd_gogui_final_score(board_t *b, engine_t *e, time_info_t *ti, gtp_t *gtp) if (e->dead_group_list) e->dead_group_list(e, b, &q); int dame, seki; - int ownermap[board_size2(b)]; + int ownermap[board_max_coords(b)]; floating_t score = board_official_score_details(b, &q, &dame, &seki, ownermap, NULL); char buffer[5000]; strbuf_t strbuf; strbuf_t *buf = strbuf_init(&strbuf, buffer, sizeof(buffer)); diff --git a/joseki.c b/joseki.c index ff0c81a86..96b8b1098 100644 --- a/joseki.c +++ b/joseki.c @@ -27,7 +27,7 @@ bool using_joseki(board_t *b) { bool r = (joseki_enabled && !using_dcnn(b) && - joseki_dict && joseki_dict->bsize == board_size(b)); + joseki_dict && joseki_dict->bsize == board_rsize(b)); if (joseki_required && !r) die("joseki required but not used, aborting.\n"); return r; } @@ -220,14 +220,14 @@ static int convert_coords(int bsize, char *buf) { if (str_prefix("boardsize", buf)) - sprintf(buf, "boardsize %i", bsize-2); + sprintf(buf, "boardsize %i", bsize); if (str_prefix("play ", buf)) { /* Convert coordinates */ char *arg = buf + 7; assert(buf[6] == ' '); if (str_prefix("pass", arg)) return 0; - coord_t c = str2coord_for(arg, 19+2); - int offset = 21 - bsize; assert(offset >= 0); + coord_t c = str2coord_for(arg, 19); + int offset = 21 - (bsize+2); assert(offset >= 0); int x = (c % 21) - offset, y = (c / 21) - offset; if (x < 1 || y < 1) return -1; /* Offboard, discard rest of sequence */ @@ -255,7 +255,7 @@ joseki_load(int bsize) if (!joseki_enabled) return; if (joseki_dict && joseki_dict->bsize != bsize) joseki_done(); if (joseki_dict && joseki_dict->bsize == bsize) return; - if (joseki_dict || bsize < 13+2) return; /* no joseki below 13x13 */ + if (joseki_dict || bsize < 13) return; /* no joseki below 13x13 */ char fname[1024]; snprintf(fname, 1024, "joseki19.gtp"); @@ -278,7 +278,7 @@ joseki_load(int bsize) char buf[4096]; gtp_t gtp; gtp_init(>p); for (int lineno = 1; fgets(buf, 4096, f); lineno++) { - if (bsize != 19+2 && convert_coords(bsize, buf) < 0) + if (bsize != 19 && convert_coords(bsize, buf) < 0) skip_sequence(buf, 4096, f, &lineno); gtp.quiet = true; @@ -292,7 +292,7 @@ joseki_load(int bsize) debug_level = saved_debug_level; int variations = gtp.played_games; - if (DEBUGL(2)) fprintf(stderr, "Loaded joseki dictionary for %ix%i (%i variations).\n", bsize-2, bsize-2, variations); + if (DEBUGL(2)) fprintf(stderr, "Loaded joseki dictionary for %ix%i (%i variations).\n", bsize, bsize, variations); if (DEBUGL(3)) joseki_stats(joseki_dict); fclose(f); } diff --git a/move.c b/move.c index 83cb62a65..f220c5c1a 100644 --- a/move.c +++ b/move.c @@ -50,13 +50,14 @@ str2coord_for(char *str, int size) if (!strcasecmp(str, "resign")) return resign; char xc = tolower(str[0]); - return xc - 'a' - (xc > 'i') + 1 + atoi(str + 1) * size; + int stride = size + 2; + return xc - 'a' - (xc > 'i') + 1 + atoi(str + 1) * stride; } coord_t str2coord(char *str) { - return str2coord_for(str, the_board_size()); + return str2coord_for(str, the_board_rsize()); } /* Must match rotations in pthashes_init() */ @@ -64,7 +65,7 @@ coord_t rotate_coord(coord_t c, int rot) { assert(c != pass); - int size = the_real_board_size(); + int size = the_board_rsize(); int x = coord_x(c); int y = coord_y(c); diff --git a/move.h b/move.h index f16bc5241..f1dfafeb6 100644 --- a/move.h +++ b/move.h @@ -11,7 +11,7 @@ typedef int coord_t; // XXX board_size() instead of board_statics.size -#define coord_xy(x, y) ((x) + (y) * the_board_size()) +#define coord_xy(x, y) ((x) + (y) * the_board_stride()) #define coord_x(c) (board_statics.coord[c][0]) #define coord_y(c) (board_statics.coord[c][1]) /* TODO: Smarter way to do this? */ @@ -23,8 +23,8 @@ typedef int coord_t; #define is_pass(c) (c == pass) #define is_resign(c) (c == resign) -#define coord_is_adjecent(c1, c2) (abs(c1 - c2) == 1 || abs(c1 - c2) == the_board_size()) -#define coord_is_8adjecent(c1, c2) (abs(c1 - c2) == 1 || abs(abs(c1 - c2) - the_board_size()) < 2) +#define coord_is_adjecent(c1, c2) (abs(c1 - c2) == 1 || abs(c1 - c2) == the_board_stride()) +#define coord_is_8adjecent(c1, c2) (abs(c1 - c2) == 1 || abs(abs(c1 - c2) - the_board_stride()) < 2) char *coord2bstr(char *buf, coord_t c); /* Return coordinate string in a dynamically allocated buffer. Thread-safe. */ diff --git a/ownermap.c b/ownermap.c index 18828411d..15e623473 100644 --- a/ownermap.c +++ b/ownermap.c @@ -54,15 +54,6 @@ ownermap_fill(ownermap_t *ownermap, board_t *b) } foreach_point_end; } -void -ownermap_merge(int bsize2, ownermap_t *dst, ownermap_t *src) -{ - dst->playouts += src->playouts; - for (int i = 0; i < bsize2; i++) - for (int j = 0; j < S_MAX; j++) - dst->map[i][j] += src->map[i][j]; -} - float ownermap_estimate_point(ownermap_t *ownermap, coord_t c) { @@ -102,7 +93,7 @@ ownermap_judge_groups(board_t *b, ownermap_t *ownermap, group_judgement_t *judge { assert(ownermap->map); assert(judge->gs); - memset(judge->gs, GS_NONE, board_size2(b) * sizeof(judge->gs[0])); + memset(judge->gs, GS_NONE, board_max_coords(b) * sizeof(judge->gs[0])); foreach_point(b) { enum stone color = board_at(b, c); @@ -146,7 +137,7 @@ groups_of_status(board_t *b, group_judgement_t *judge, enum gj_state s, move_que void get_dead_groups(board_t *b, ownermap_t *ownermap, move_queue_t *dead, move_queue_t *unclear) { - enum gj_state gs_array[board_size2(b)]; + enum gj_state gs_array[board_max_coords(b)]; group_judgement_t gj = { 0.67, gs_array }; ownermap_judge_groups(b, ownermap, &gj); if (dead) { dead->moves = 0; groups_of_status(b, &gj, GS_DEAD, dead); } @@ -239,7 +230,7 @@ board_position_final(board_t *b, ownermap_t *ownermap, char **msg) floating_t score_est = ownermap_score_est(b, ownermap); int dame, seki; - int final_ownermap[board_size2(b)]; + int final_ownermap[board_max_coords(b)]; floating_t final_score = board_official_score_details(b, &dead, &dame, &seki, final_ownermap, ownermap); return board_position_final_full(b, ownermap, &dead, &unclear, score_est, diff --git a/ownermap.h b/ownermap.h index bd9748c6d..e2c41daf7 100644 --- a/ownermap.h +++ b/ownermap.h @@ -47,7 +47,6 @@ typedef struct ownermap { void ownermap_init(ownermap_t *ownermap); void board_print_ownermap(board_t *b, FILE *f, ownermap_t *ownermap); void ownermap_fill(ownermap_t *ownermap, board_t *b); -void ownermap_merge(int bsize2, ownermap_t *dst, ownermap_t *src); /* Coord ownermap status: dame / black / white / unclear */ enum point_judgement ownermap_judge_point(ownermap_t *ownermap, coord_t c, floating_t thres); @@ -82,7 +81,7 @@ bool board_position_final_full(board_t *b, ownermap_t *ownermap, /* Don't allow passing earlier than that: * 19x19: 120 15x15: 56 13x13: 33 9x9: 16 */ -#define board_earliest_pass(b) (real_board_size2(b) / (7 - 2 * real_board_size(b) / 9)) +#define board_earliest_pass(b) (board_max_coords(b) / (7 - 2 * board_rsize(b) / 9)) #endif diff --git a/pachi.c b/pachi.c index 30f79b46a..ec646f91d 100644 --- a/pachi.c +++ b/pachi.c @@ -395,7 +395,7 @@ int main(int argc, char *argv[]) if (DEBUGL(2)) fprintf(stderr, "Random seed: %d\n", seed); fifo_init(); - board_t *b = board_new(19 + 2, fbookfile); + board_t *b = board_new(19, fbookfile); if (forced_ruleset) { if (!board_set_rules(b, forced_ruleset)) die("Unknown ruleset: %s\n", forced_ruleset); if (DEBUGL(1)) fprintf(stderr, "Rules: %s\n", forced_ruleset); diff --git a/patternsp.c b/patternsp.c index b098a1f95..663ed3602 100644 --- a/patternsp.c +++ b/patternsp.c @@ -201,11 +201,12 @@ spatial2str(spatial_t *s) void spatial_print(board_t *board, spatial_t *s, FILE *f, move_t *at) { - board_t *b = board_new(board_size(board), NULL); + int size = board_rsize(board); + board_t *b = board_new(size, NULL); last_move(b).coord = at->coord; - for (int i = 0; i < real_board_size(b); i++) - for (int j = 0; j < real_board_size(b); j++) { + for (int i = 0; i < size; i++) + for (int j = 0; j < size; j++) { coord_t c = coord_xy(i+1, j+1); board_at(b, c) = (enum stone)4; // HACK ! } diff --git a/patternsp.h b/patternsp.h index 2a227f905..1ddfe081f 100644 --- a/patternsp.h +++ b/patternsp.h @@ -129,8 +129,8 @@ extern hash_t pthashes[PTH__ROTATIONS][MAX_PATTERN_AREA][S_MAX]; #define ptcoords_at(x_, y_, c_, j_) \ int x_ = coord_x((c_)) + ptcoords[j_].x; \ int y_ = coord_y((c_)) + ptcoords[j_].y; \ - if (x_ >= the_board_size()) x_ = the_board_size() - 1; else if (x_ < 0) x_ = 0; \ - if (y_ >= the_board_size()) y_ = the_board_size() - 1; else if (y_ < 0) y_ = 0; + if (x_ >= the_board_stride()) x_ = the_board_stride() - 1; else if (x_ < 0) x_ = 0; \ + if (y_ >= the_board_stride()) y_ = the_board_stride() - 1; else if (y_ < 0) y_ = 0; /* Spatial dictionary file manipulation. */ diff --git a/playout.c b/playout.c index 51ca3b491..62712c1ba 100644 --- a/playout.c +++ b/playout.c @@ -109,7 +109,7 @@ static coord_t fill_bent_four(board_t *b, enum stone color, coord_t *other, coord_t *kill) { enum stone other_color = stone_other(color); // white here - int s = real_board_size(b); + int s = board_rsize(b); coord_t corners[4] = { coord_xy(1, 1), coord_xy(1, s), coord_xy(s, 1), diff --git a/playout/moggy.c b/playout/moggy.c index 56818985b..6ccfb941a 100644 --- a/playout/moggy.c +++ b/playout/moggy.c @@ -508,8 +508,8 @@ eye_fix_check(playout_policy_t *p, board_t *b, move_t *m, enum stone to_play, mo /* Iterator for walking coordinates in a clockwise fashion * (nei8 jumps "over" the middle point, inst. of "around). */ - int size = board_size(b); - int nei8_clockwise[10] = { -size-1, 1, 1, size, size, -1, -1, -size, -size, 1 }; + int stride = board_stride(b); + int nei8_clockwise[10] = { -stride-1, 1, 1, stride, stride, -1, -1, -stride, -stride, 1 }; /* This is sort of like a cross between foreach_diag_neighbor * and foreach_8neighbor. */ @@ -1018,7 +1018,7 @@ playout_moggy_assess(playout_policy_t *p, prior_map_t *map, int games) moggy_policy_t *pp = (moggy_policy_t*)p->data; /* First, go through all endangered groups. */ - for (group_t g = 1; g < board_size2(map->b); g++) + for (group_t g = 1; g < board_max_coords(map->b); g++) if (group_at(map->b, g) == g) playout_moggy_assess_group(p, map, g, games); diff --git a/t-unit/board_regtest.c b/t-unit/board_regtest.c index f9a2e619a..b29a6d464 100644 --- a/t-unit/board_regtest.c +++ b/t-unit/board_regtest.c @@ -47,7 +47,7 @@ hash_board_statics(board_t *b) hash_init(); /* These are board statics really */ - hash_int(board_size2(b)); + hash_int(board_max_coords(b)); hash_int(board_bits2(b)); /* Just care about hashes here */ @@ -71,7 +71,7 @@ hash_board(board_t *b) /****************************************************************/ /* Common fields */ - hash_field(size); + hash_int(board_rsize(b) + 2); // for backwards compatibility //hash_field(captures); // don't hash displayed fields //hash_field(passes); hash_field(komi); @@ -166,7 +166,7 @@ hash_board(board_t *b) static void dump_board_statics(board_t *b) { - int size = real_board_size(b); + int size = board_rsize(b); /* Dump once for each board size. */ static int size_done[BOARD_MAX_SIZE + 1] = { 0, }; diff --git a/t-unit/test.c b/t-unit/test.c index 3d2658136..f546ec4b8 100644 --- a/t-unit/test.c +++ b/t-unit/test.c @@ -429,14 +429,14 @@ test_moggy_moves(board_t *b, char *arg) fprintf(stderr, "moggy moves, %s to play. Sampling moves (%i runs)...\n\n", stone2str(color), runs); - int played_[board_size2(b) + 1]; memset(played_, 0, sizeof(played_)); + int played_[board_max_coords(b) + 1]; memset(played_, 0, sizeof(played_)); int *played = played_ + 1; // allow storing pass int most_played = 0; replay_sample_moves(&e, b, color, played, &most_played); /* Show moves stats */ for (int k = most_played; k > 0; k--) - for (coord_t c = pass; c < board_size2(b); c++) + for (coord_t c = pass; c < board_max_coords(b); c++) if (played[c] == k) if (DEBUGL(2)) fprintf(stderr, "%3s: %.2f%%\n", coord2str(c), (float)k * 100 / runs); @@ -628,7 +628,7 @@ unit_test(char *filename) int total = 0, passed = 0; int total_opt = 0, passed_opt = 0; - board_t *b = board_new(19+2, NULL); + board_t *b = board_new(19, NULL); b->komi = 7.5; char buf[256]; char *line = buf; diff --git a/t-unit/test_undo.c b/t-unit/test_undo.c index 2cb20c95f..0e7c8bfed 100644 --- a/t-unit/test_undo.c +++ b/t-unit/test_undo.c @@ -15,7 +15,7 @@ static int board_quick_cmp(struct board *b1, struct board *b2) { - if (b1->size != b2->size || + if (b1->rsize != b2->rsize || b1->captures[S_BLACK] != b2->captures[S_BLACK] || b1->captures[S_WHITE] != b2->captures[S_WHITE] || b1->moves != b2->moves) { @@ -72,7 +72,7 @@ board_dump_group(board_t *b, group_t g) static void board_dump(board_t *b) { - printf("board_dump(): size: %i\n", b->size); + printf("board_dump(): size: %i\n", board_rsize(b)); board_print(b, stdout); printf("ko: %s %s last_ko: %s %s last_ko_age: %i\n", diff --git a/tactics/seki.c b/tactics/seki.c index 8b6f49342..112e609be 100644 --- a/tactics/seki.c +++ b/tactics/seki.c @@ -103,8 +103,8 @@ breaking_corner_seki(board_t *b, coord_t coord, enum stone color) /* Other lib is corner eye ? */ int x = coord_x(lib2), y = coord_y(lib2); - if ((x != 1 && x != real_board_size(b)) || - (y != 1 && y != real_board_size(b)) || + if ((x != 1 && x != board_rsize(b)) || + (y != 1 && y != board_rsize(b)) || !board_is_one_point_eye(b, lib2, color)) return false; diff --git a/tactics/seki.h b/tactics/seki.h index 66f1c3d04..44d94b858 100644 --- a/tactics/seki.h +++ b/tactics/seki.h @@ -1,8 +1,8 @@ #ifndef PACHI_TACTICS_SEKI_H #define PACHI_TACTICS_SEKI_H -#define MOGGY_MIDDLEGAME (real_board_size2(b) * 10 / 25) /* 19x19: 144 */ -#define MOGGY_ENDGAME (real_board_size2(b) * 100 / 164) /* 19x19: 220 */ +#define MOGGY_MIDDLEGAME (board_rsize(b) * board_rsize(b) * 10 / 25) /* 19x19: 144 */ +#define MOGGY_ENDGAME (board_rsize(b) * board_rsize(b) * 100 / 164) /* 19x19: 220 */ #define check_special_sekis(b, m) \ (b->moves > MOGGY_MIDDLEGAME && !immediate_liberty_count(b, m->coord)) diff --git a/tactics/util.c b/tactics/util.c index 4477938bd..94cf71086 100644 --- a/tactics/util.c +++ b/tactics/util.c @@ -17,8 +17,8 @@ board_stone_radar(board_t *b, coord_t coord, int distance) for (int i = 0; i < 4; i++) if (bounds[i] < 1) bounds[i] = 1; - else if (bounds[i] > board_size(b) - 2) - bounds[i] = board_size(b) - 2; + else if (bounds[i] > board_rsize(b)) + bounds[i] = board_rsize(b); for (int x = bounds[0]; x <= bounds[2]; x++) for (int y = bounds[1]; y <= bounds[3]; y++) { coord_t c = coord_xy(x, y); @@ -38,8 +38,8 @@ cfg_distances(board_t *b, coord_t start, int *distances, int maxdist) { /* Queue for d+1 spots; no two spots of the same group * should appear in the queue. */ -#define qinc(x) (x = ((x + 1) >= board_size2(b) ? ((x) + 1 - board_size2(b)) : (x) + 1)) - coord_t queue[board_size2(b)]; int qstart = 0, qstop = 0; +#define qinc(x) (x = ((x + 1) >= board_max_coords(b) ? ((x) + 1 - board_max_coords(b)) : (x) + 1)) + coord_t queue[board_max_coords(b)]; int qstart = 0, qstop = 0; foreach_point(b) { distances[c] = board_at(b, c) == S_OFFBOARD ? maxdist + 1 : -1; @@ -106,7 +106,7 @@ board_effective_handicap(board_t *b, int first_move_value) int board_estimated_moves_left(board_t *b) { - int total_points = (board_size(b)-2)*(board_size(b)-2); + int total_points = (board_rsize(b)) * (board_rsize(b)); int moves_left = (b->flen - total_points*EXPECTED_FINAL_EMPTY_PERCENT/100)/2; return moves_left > MIN_MOVES_LEFT ? moves_left : MIN_MOVES_LEFT; } diff --git a/tactics/util.h b/tactics/util.h index d9b441c62..fa5df0d1c 100644 --- a/tactics/util.h +++ b/tactics/util.h @@ -20,7 +20,7 @@ static int coord_gridcular_distance(coord_t c1, coord_t c2); * graph of intersections where edges between all neighbors have weight 1, * but edges between neighbors of same color have weight 0. Thus, this is * "stone chain" metric in a sense. */ -/* The output are distances from start stored in given [board_size2()] array; +/* The output are distances from start stored in given [board_max_coords()] array; * intersections further away than maxdist have all distance maxdist+1 set. */ void cfg_distances(board_t *b, coord_t start, int *distances, int maxdist); @@ -50,9 +50,10 @@ static double board_local_value(bool scan_neis, board_t *b, coord_t coord, enum static inline int coord_edge_distance(coord_t c) { + int stride = the_board_stride(); int x = coord_x(c), y = coord_y(c); - int dx = x > the_board_size() / 2 ? the_board_size() - 1 - x : x; - int dy = y > the_board_size() / 2 ? the_board_size() - 1 - y : y; + int dx = x > stride / 2 ? stride - 1 - x : x; + int dy = y > stride / 2 ? stride - 1 - y : y; return (dx < dy ? dx : dy) - 1 /* S_OFFBOARD */; } diff --git a/timeinfo.c b/timeinfo.c index 5402ab877..fc8309a64 100644 --- a/timeinfo.c +++ b/timeinfo.c @@ -315,7 +315,7 @@ time_stop_set_remaining(time_info_t *ti, board_t *b, double net_lag, time_stop_t static void time_stop_phase_adjust(board_t *b, int fuseki_end, int yose_start, time_stop_t *stop) { - int bsize = (board_size(b)-2)*(board_size(b)-2); + int bsize = (board_rsize(b))*(board_rsize(b)); fuseki_end = fuseki_end * bsize / 100; // move nb at fuseki end yose_start = yose_start * bsize / 100; // move nb at yose start assert(fuseki_end < yose_start); @@ -479,8 +479,8 @@ fuseki_moves(board_t *b) return opt_fuseki_moves; int moves = 20; - if (real_board_size(b) <= 15) moves = 10; - if (real_board_size(b) <= 13) moves = 7; + if (board_rsize(b) <= 15) moves = 10; + if (board_rsize(b) <= 13) moves = 7; if (board_small(b)) moves = 4; return moves; } diff --git a/uct/dynkomi.c b/uct/dynkomi.c index 853918d44..0d731e0c1 100644 --- a/uct/dynkomi.c +++ b/uct/dynkomi.c @@ -145,7 +145,7 @@ static int linear_moves(board_t *b, enum stone color) { if (board_small(b)) return 15; - if (real_board_size(b) == 15) return 100; + if (board_rsize(b) == 15) return 100; if (color == S_BLACK) return (board_large(b) ? 200 : 50); else return (board_large(b) ? 150 : 50); } @@ -293,8 +293,8 @@ board_game_portion(dynkomi_adaptive_t *a, board_t *b) int total_moves = b->moves + 2 * board_estimated_moves_left(b); return (floating_t) b->moves / total_moves; } else { - int brsize = board_size(b) - 2; - return 1.0 - (floating_t) b->flen / (brsize * brsize); + int size = board_rsize(b); + return 1.0 - (floating_t) b->flen / (size * size); } } diff --git a/uct/policy/ucb1amaf.c b/uct/policy/ucb1amaf.c index 359121914..452bb4708 100644 --- a/uct/policy/ucb1amaf.c +++ b/uct/policy/ucb1amaf.c @@ -260,7 +260,7 @@ ucb1amaf_update(uct_policy_t *p, tree_t *tree, tree_node_t *node, * at this coordinate, or INT_MAX if the move was not played. * The parity gives the color of this move. */ - int first_map[board_size2(final_board)+1]; + int first_map[board_max_coords(final_board)+1]; int *first_move = &first_map[1]; // +1 for pass #if 0 @@ -272,7 +272,7 @@ ucb1amaf_update(uct_policy_t *p, tree_t *tree, tree_node_t *node, #endif /* Initialize first_move */ - for (int i = pass; i < board_size2(final_board); i++) first_move[i] = INT_MAX; + for (int i = pass; i < board_max_coords(final_board); i++) first_move[i] = INT_MAX; int move; assert(map->gamelen > 0); for (move = map->gamelen - 1; move >= map->game_baselen; move--) diff --git a/uct/search.c b/uct/search.c index 3fc60639b..b3ecf1787 100644 --- a/uct/search.c +++ b/uct/search.c @@ -653,7 +653,7 @@ uct_pass_first(uct_t *u, board_t *b, enum stone color, bool pass_all_alive, coor uct_mcowner_playouts(u, b, color); get_dead_groups(b, &u->ownermap, &dead, &unclear); if (unclear.moves) return false; - int final_ownermap[board_size2(b)]; + int final_ownermap[board_max_coords(b)]; int dame, seki; board_official_score_details(b, &dead, &dame, &seki, final_ownermap, &u->ownermap); diff --git a/uct/slave.c b/uct/slave.c index 6efbdfaf1..d24daf649 100644 --- a/uct/slave.c +++ b/uct/slave.c @@ -448,7 +448,7 @@ report_stats(uct_t *u, board_t *b, coord_t c, for (tree_node_t *ni = root->children; ni; ni = ni->sibling) { if (is_pass(node_coord(ni))) continue; - assert(node_coord(ni) > 0 && node_coord(ni) < board_size2(b)); + assert(node_coord(ni) > 0 && node_coord(ni) < board_max_coords(b)); if (ni->u.playouts > max_playouts) max_playouts = ni->u.playouts; diff --git a/uct/tree.c b/uct/tree.c index 98963be89..48e1d1bb5 100644 --- a/uct/tree.c +++ b/uct/tree.c @@ -241,12 +241,12 @@ tree_dump(tree_t *tree, double thres) static char * tree_book_name(board_t *b) { + int size = board_rsize(b); static char buf[256]; - if (b->handicap > 0) { - sprintf(buf, "ucttbook-%d-%02.01f-h%d.pachitree", b->size - 2, b->komi, b->handicap); - } else { - sprintf(buf, "ucttbook-%d-%02.01f.pachitree", b->size - 2, b->komi); - } + if (b->handicap > 0) + sprintf(buf, "ucttbook-%d-%02.01f-h%d.pachitree", size, b->komi, b->handicap); + else + sprintf(buf, "ucttbook-%d-%02.01f.pachitree", size, b->komi); return buf; } @@ -578,15 +578,15 @@ void tree_expand_node(tree_t *t, tree_node_t *node, board_t *b, enum stone color, uct_t *u, int parity) { /* Get a Common Fate Graph distance map from parent node. */ - int distances[board_size2(b)]; + int distances[board_max_coords(b)]; if (!is_pass(last_move(b).coord)) cfg_distances(b, last_move(b).coord, distances, TREE_NODE_D_MAX); else // Pass - everything is too far. foreach_point(b) { distances[c] = TREE_NODE_D_MAX + 1; } foreach_point_end; /* Include pass in the prior map. */ - move_stats_t map_prior[board_size2(b) + 1]; memset(map_prior, 0, sizeof(map_prior)); - bool map_consider[board_size2(b) + 1]; memset(map_consider, 0, sizeof(map_consider)); + move_stats_t map_prior[board_max_coords(b) + 1]; memset(map_prior, 0, sizeof(map_prior)); + bool map_consider[board_max_coords(b) + 1]; memset(map_consider, 0, sizeof(map_consider)); /* Get a map of prior values to initialize the new nodes with. */ prior_map_t map = { b, color, tree_parity(t, parity), &map_prior[1], &map_consider[1], distances }; @@ -627,7 +627,7 @@ tree_expand_node(tree_t *t, tree_node_t *node, board_t *b, enum stone color, uct for (int j = b->symmetry.y1; j <= b->symmetry.y2; j++) { for (int i = b->symmetry.x1; i <= b->symmetry.x2; i++) { if (b->symmetry.d) { - int x = b->symmetry.type == SYM_DIAG_DOWN ? board_size(b) - 1 - i : i; + int x = b->symmetry.type == SYM_DIAG_DOWN ? board_stride(b) - 1 - i : i; if (x > j) { if (UDEBUGL(7)) fprintf(stderr, "drop %d,%d\n", i, j); @@ -658,8 +658,8 @@ flip_coord(board_t *b, coord_t c, { int x = coord_x(c), y = coord_y(c); if (flip_diag) { int z = x; x = y; y = z; } - if (flip_horiz) { x = board_size(b) - 1 - x; } - if (flip_vert) { y = board_size(b) - 1 - y; } + if (flip_horiz) { x = board_stride(b) - 1 - x; } + if (flip_vert) { y = board_stride(b) - 1 - y; } return coord_xy(x, y); } @@ -695,7 +695,7 @@ tree_fix_symmetry(tree_t *tree, board_t *b, coord_t c) bool flip_diag = 0; if (s->d) { bool dir = (s->type == SYM_DIAG_DOWN); - int x = dir ^ flip_horiz ^ flip_vert ? board_size(b) - 1 - cx : cx; + int x = dir ^ flip_horiz ^ flip_vert ? board_stride(b) - 1 - cx : cx; if (flip_vert ? x < cy : x > cy) { flip_diag = 1; } diff --git a/uct/uct.c b/uct/uct.c index f8bf14fe1..5f85dc39d 100644 --- a/uct/uct.c +++ b/uct/uct.c @@ -130,7 +130,7 @@ uct_pass_is_safe(uct_t *u, board_t *b, enum stone color, bool pass_all_alive, ch floating_t score_est = ownermap_score_est_color(b, &u->ownermap, color); if (check_score && score_est < 0) return false; - int final_ownermap[board_size2(b)]; + int final_ownermap[board_max_coords(b)]; int dame, seki; floating_t final_score = board_official_score_details(b, &dead, &dame, &seki, final_ownermap, &u->ownermap); if (color == S_BLACK) final_score = -final_score; @@ -1369,7 +1369,7 @@ uct_state_init(char *arg, board_t *b) } dcnn_init(b); - if (!using_dcnn(b)) joseki_load(b->size); + if (!using_dcnn(b)) joseki_load(board_rsize(b)); if (!pat_setup) patterns_init(&u->pc, NULL, false, true); log_nthreads(u); if (!u->prior) u->prior = uct_prior_init(NULL, b, u); diff --git a/uct/walk.c b/uct/walk.c index cfa63e055..61d09c624 100644 --- a/uct/walk.c +++ b/uct/walk.c @@ -353,7 +353,7 @@ scale_value(uct_t *u, board_t *b, enum stone node_color, tree_node_t *significan int vp = u->val_points; if (!vp) { - vp = board_size(b) - 1; vp *= vp; vp *= 2; + vp = board_stride(b) - 1; vp *= vp; vp *= 2; } floating_t sval = (floating_t) abs(result) / vp; @@ -494,7 +494,7 @@ uct_playout_descent(uct_t *u, board_t *b, board_t *b2, enum stone player_color, significant[node_color - 1] = n; int result; - int pass_limit = real_board_size2(b2) / 2; + int pass_limit = board_rsize(b2) * board_rsize(b2) / 2; int passes = is_pass(last_move(b).coord) && b->moves > 0; /* debug */