diff options
| author | tslil clingman <tslil@posteo.de> | 2021-01-20 13:23:58 -0500 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-08-28 19:37:41 +0100 |
| commit | c81898d6a760e2057f0933c9da6160702a4d0f61 (patch) | |
| tree | f08bc685da10ec7f9201f983e1b5528f388f7f88 /include/minimax_cnn1986.c | |
| parent | c1b4600ee67c651a79f682600fa7f2676ffa916e (diff) | |
In-lined as much as a i dare
Diffstat (limited to 'include/minimax_cnn1986.c')
| -rw-r--r-- | include/minimax_cnn1986.c | 96 |
1 files changed, 61 insertions, 35 deletions
diff --git a/include/minimax_cnn1986.c b/include/minimax_cnn1986.c index ce950ba..d13195d 100644 --- a/include/minimax_cnn1986.c +++ b/include/minimax_cnn1986.c @@ -6,7 +6,6 @@ const float infty = 3.0; char ct1986_ptn[9]; -void (*ct1986_display_progress)(const uint8_t); // =================================================================== // Implementation of a small convolutional neural network @@ -177,11 +176,12 @@ static enum WIN_TYPE w; } \ } +// UP DOWN LEFT RIGHT +static const int8_t deltas[4] = { +5, -5, -1, +1}; + float 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 black = (ply & 1), material = (black) ? black_count : white_count, flat = material & 127, @@ -200,9 +200,33 @@ ct1986_minimax(const uint8_t cur_depth, const uint8_t max_depth, // Only try moves after CPS if (count && ((colours[loc] & 1) == current_colour) && ply>2) { // There are stones, can we move them in a given direction? - // I'm not a huge fan of looping through enums, but it's - // better than manually unrolling this. Sufficiently smart - // compilers? + + // Pre-compute end-stops + uint8_t end_stops[4][2]; // (end, not_crush) + // UP DOWN LEFT RIGHT + end_stops[0][0] = (4-row > count) ? count : 4-row; + end_stops[1][0] = (row > count) ? count : row; + end_stops[2][0] = (col > count) ? count : col; + end_stops[3][0] = (4-col > count) ? count : 4-col; + const uint8_t cap_top = STONE_AT(loc) == STONE_CAPSTONE; + for (uint8_t d = 0; d < 4; d++){ + end_stops[d][1] = 1; + const uint8_t stop = end_stops[d][0]; + end_stops[d][0] = 0; + for (uint8_t k = 1; k <= stop; k++) { + const uint8_t stone = STONE_AT(loc+k*deltas[d]); + if (stone == STONE_STANDING) { + if (cap_top) { + end_stops[d][1] = 0; + end_stops[d][0]++; + } + break; + } else if (stone == STONE_CAPSTONE) { + break; + } + end_stops[d][0]++; + } + } uint16_t colours_backup[5]; uint8_t celldat_backup[5], drops[5]; // we only use 4, the @@ -213,6 +237,10 @@ ct1986_minimax(const uint8_t cur_depth, const uint8_t max_depth, colours_backup[y] = colours[THE_COORDS(col, y)]; celldat_backup[y] = celldat[THE_COORDS(col, y)]; } + + // I'm not a huge fan of looping through enums, but it's + // better than manually unrolling this. Sufficiently smart + // compilers? for (enum MOVE_DIRECTION dir = M_UP; dir <= M_RIGHT; dir++) { // Back-up the column once we start looking horizontally if (dir == M_LEFT) { @@ -222,33 +250,16 @@ ct1986_minimax(const uint8_t cur_depth, const uint8_t max_depth, } } /* - * We don't do anything terribly efficient or smart here, - * just try all the ordered partitions of num ∈ {1,…,count} - * that will fit on the board in the current direction. - * Recall that count = max(stack height, carry limit). + * We don't do anything terribly efficient here just try + * all the ordered partitions of num ∈ {1 … end_stop}, and + * skip the partition if it calls for multiple stones at + * the end with a crush. */ - uint8_t upper; - switch (dir) { - case M_UP: { - upper = (4-row > count) ? count : 4-row; - break; - } - case M_DOWN: { - upper = (row > count) ? count : row; - break; - } - case M_LEFT: { - upper = (col > count) ? count : col; - break; - } - case M_RIGHT: { - upper = (4-col > count) ? count : 4-col; - break; - } - } uint8_t gaps, t, idx, mask; for (uint8_t num = 1; num <= count; num++) { - for (uint8_t steps = 1; steps <= upper && steps <= num; steps++) { + for (uint8_t steps = 1; + steps <= end_stops[dir][0] && steps <= num; + steps++) { gaps = 0b00000111 >> (4-steps); do { // Translate to a drop sequence @@ -262,9 +273,24 @@ ct1986_minimax(const uint8_t cur_depth, const uint8_t max_depth, } mask <<= 1; } - // Try it, and manually check for win if it's valid - r = try_move(loc, dir, steps, drops); - if (r == ACT_OK) { + // TODO: Work out what this should be before partition + + // Ensure legal move if we have to crush + if (end_stops[dir][1] || drops[steps-1] <= 1) { + // Try it, and manually check for win if it's valid + uint8_t j = num; + for (uint8_t k = 0; k < steps; k++) { + // Dear future me, i'm sorry + j -= drops[k]; + colours[loc+(k+1)*deltas[dir]] = (colours[loc+(k+1)*deltas[dir]] << drops[k]) + | ((colours[loc] >> j) & (0xFFFF >> (0x10 - drops[k]))); + celldat[loc+(k+1)*deltas[dir]] = (k == steps - 1) ? STONE_AT(loc) : STONE_FLAT + | ((celldat[loc+(k+1)*deltas[dir]] + ((drops[k] << NUM_SHIFT))) & NUM_MASK); + } + // Then we drop them from the source + colours[loc] >>= num; + const uint8_t dec_count = celldat[loc] - (num << NUM_SHIFT); + celldat[loc] = dec_count & NUM_MASK; // First check for wins, if we're at the bottom // evaluate, otherwise recurse WIN_EVALUATE_OR_RECURSE({ @@ -284,9 +310,9 @@ ct1986_minimax(const uint8_t cur_depth, const uint8_t max_depth, celldat[THE_COORDS(x, row)] = celldat_backup[x]; } } + // Prune + if (alpha >= beta) return optimal; } - // Prune - if (alpha >= beta) return optimal; /* * With thanks to * https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation |
