From b1d677fa134741527669b93a77810cf01dd34de5 Mon Sep 17 00:00:00 2001 From: tslil clingman Date: Sun, 17 Jan 2021 23:16:09 -0500 Subject: Inlined placements, should be faster --- include/ct1986.c | 71 ++++++++++++++++++++++++++++++++++++++++---------------- include/tak.c | 1 - include/tak.h | 9 +++---- 3 files changed, 56 insertions(+), 25 deletions(-) (limited to 'include') diff --git a/include/ct1986.c b/include/ct1986.c index 5e1c0fc..9289d2f 100644 --- a/include/ct1986.c +++ b/include/ct1986.c @@ -170,8 +170,14 @@ ct1986_minimax(const uint8_t cur_depth, const uint8_t max_depth, const uint8_t min, float alpha, float beta) { enum E_RESULT r; - const uint8_t white_count_backup = white_count, - black_count_backup = black_count; + const uint8_t black = (ply & 1), + material = (black) ? black_count : white_count; + + uint8_t cap = 0, flat = material & 127, standing = 0; + if (ply > 2) { + if (material & 128) cap = 1; + if (material & 127) standing = 1; + } // 1.0 is a `certain' black win, -1.0 is a `certain' white win. float optimal = (min) ? infty : -infty; @@ -261,30 +267,55 @@ ct1986_minimax(const uint8_t cur_depth, const uint8_t max_depth, } } } else if (count == 0) { - // Empty square, try the three placements. Again, looping - // through enums, sigh. - for (enum STONE_VARIANT stone = STONE_FLAT; - stone <= STONE_CAPSTONE; stone++) { - // try_place will never check for winning, and we don't do - // that either here - r = try_place(loc, current_colour, stone); - // Legal placement, evaluate it - if (r == ACT_OK) { - // First check for wins, if we're at the bottom - // evaluate, otherwise recurse - WIN_EVALUATE_OR_RECURSE({ + // Empty square, try the three placements. + + if (flat) { + // Generate the placement + if (black) black_count--; + else white_count--; + colours[loc] = current_colour; + celldat[loc] = NUM_INC | STONE_FLAT; + WIN_EVALUATE_OR_RECURSE({ // If we did update the optimal value, store - // this move - generate_place(loc, stone, ct1986_ptn); + generate_place(loc, STONE_FLAT, ct1986_ptn); + }); + // Reset the state + celldat[loc] = 0; + if (black) black_count++; + else white_count++; + // Prune + if (alpha >= beta) return optimal; + + // Do the same for walls, can't happen without flats + if (standing) { + if (black) black_count--; + else white_count--; + colours[loc] = current_colour; + celldat[loc] = NUM_INC | STONE_STANDING; + WIN_EVALUATE_OR_RECURSE({ + generate_place(loc, STONE_STANDING, ct1986_ptn); }); - // Reset the state celldat[loc] = 0; - white_count = white_count_backup; - black_count = black_count_backup; - // Prune + if (black) black_count++; + else white_count++; if (alpha >= beta) return optimal; } } + + // and for caps + if (cap) { + if (black) black_count &= 127; + else white_count &= 127; + colours[loc] = current_colour; + celldat[loc] = NUM_INC | STONE_STANDING; + WIN_EVALUATE_OR_RECURSE({ + generate_place(loc, STONE_STANDING, ct1986_ptn); + }); + celldat[loc] = 0; + if (black) black_count |= 128; + else white_count |= 128; + if (alpha >= beta) return optimal; + } } ct1986_display_progress(cur_depth); } diff --git a/include/tak.c b/include/tak.c index ef4fc7d..3d3ba0f 100644 --- a/include/tak.c +++ b/include/tak.c @@ -15,7 +15,6 @@ uint8_t white_count, black_count, ply; // Helpers // =================================================================== -#define NUM_INC (0x1<> NUM_SHIFT) +#define NUM_SHIFT 4 +#define NUM_INC (0x1<> NUM_SHIFT) #define THE_COORDS(col,row) ((col)+(row)*board_size) extern enum WIN_TYPE won; -- cgit v1.2.3