diff options
| author | tslil clingman <tslil@posteo.de> | 2021-01-17 23:16:09 -0500 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-08-28 19:37:41 +0100 |
| commit | b1d677fa134741527669b93a77810cf01dd34de5 (patch) | |
| tree | bb51d2e76cd13f58a79356ff0c2b1a4e037f1b14 | |
| parent | decedc910e271d2b54298441e271524c065d119e (diff) | |
Inlined placements, should be faster
| -rw-r--r-- | include/ct1986.c | 71 | ||||
| -rw-r--r-- | include/tak.c | 1 | ||||
| -rw-r--r-- | include/tak.h | 9 |
3 files changed, 56 insertions, 25 deletions
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) // 0b00010000 #define NUM_MASK (0xF<<NUM_SHIFT) // 0b11110000 #define DFS_MASK 0b00001100 #define NOT_DFS_MASK 0b11110011 diff --git a/include/tak.h b/include/tak.h index ad0b994..0a97c2d 100644 --- a/include/tak.h +++ b/include/tak.h @@ -19,10 +19,11 @@ enum WIN_TYPE { WIN_DRAW, typedef uint8_t data_t; typedef uint16_t colour_stack_t; -#define NUM_SHIFT 4 -#define STONE_MASK 0b00000011 -#define STONE_AT(l) (celldat[(l)] & STONE_MASK) -#define COUNT_AT(l) (celldat[(l)] >> NUM_SHIFT) +#define NUM_SHIFT 4 +#define NUM_INC (0x1<<NUM_SHIFT) // 0b00010000 +#define STONE_MASK 0b00000011 +#define STONE_AT(l) (celldat[(l)] & STONE_MASK) +#define COUNT_AT(l) (celldat[(l)] >> NUM_SHIFT) #define THE_COORDS(col,row) ((col)+(row)*board_size) extern enum WIN_TYPE won; |
