aboutsummaryrefslogtreecommitdiff
path: root/include/state.c
diff options
context:
space:
mode:
authortslil <tslil@posteo.de>2020-12-29 22:48:53 -0500
committertslil <tslil@posteo.de>2026-08-28 19:37:41 +0100
commit0a3b2d825077b5e6c5f1cf4a0dcb13fdfffc0be6 (patch)
tree08b71fbf1810806e628bbf7f8d7b121d9b7c8443 /include/state.c
parente57746530836b24b6d8d20e41239e955fa9e20d4 (diff)
Working on the big wrapper for everything
Diffstat (limited to 'include/state.c')
-rw-r--r--include/state.c106
1 files changed, 78 insertions, 28 deletions
diff --git a/include/state.c b/include/state.c
index 3708792..dfdd02b 100644
--- a/include/state.c
+++ b/include/state.c
@@ -18,7 +18,8 @@
// -------------------------------------------------------------------
// Game management
-void reset_state(const uint8_t new_board_size) {
+void
+reset_state(const uint8_t new_board_size) {
if (new_board_size == 6) {
board_size = 6;
white_flats = 30; black_flats = 30;
@@ -30,16 +31,39 @@ void reset_state(const uint8_t new_board_size) {
black_caps = 1;
turn = 0;
+ current_colour = C_BLACK;
for (uint8_t k = 0; k < board_size * board_size; k++ ) {
celldat[k] = 0;
}
}
+void
+next_turn(void) {
+ /*
+
+ StartType::CPS(c) => {
+ if ply / 2 >= (*c as usize) {
+ if ply % 2 == 0 {
+ (Player::White, TurnOrder::Normal)
+ } else {
+ (Player::Black, TurnOrder::Normal)
+ }
+ } else {
+ if ply % 2 == 0 {
+ (Player::White, TurnOrder::WhitePlacesBlack)
+ } else {
+ (Player::Black, TurnOrder::BlackPlacesWhite)
+ }
+ }
+ }
+ */
+}
+
// -------------------------------------------------------------------
// Place stone
-enum ACTION_RESULT
+enum E_RESULT
try_place(const int8_t location, const enum COLOUR colour,
const enum STONE_VARIANT stone)
{
@@ -95,7 +119,7 @@ drop_stones(const int8_t location, const uint8_t count) {
celldat[location] = dec_count & NUM_MASK;
}
-enum ACTION_RESULT
+enum E_RESULT
try_move(const int8_t location, const enum MOVE_DIRECTION direction,
const uint8_t steps, const uint8_t drops[5]) {
// Can't do this
@@ -225,34 +249,16 @@ dfs_road(uint8_t dfs_stack[board_size*board_size], uint8_t dfs_pntr,
return 0;
}
-enum WIN_RESULT
-check_win(const enum COLOUR colour) {
- // Do we do a flat count?
- if (black_flats == 0 || white_flats == 0 || board_full()) {
- int8_t total = 0;
- for (uint8_t k = 0; k < board_size * board_size; k++) {
- if (STONE_AT(k) == STONE_FLAT) {
- total += ((colours[k] & 1) == C_BLACK) ? +1 : -1 ;
- }
- }
- if (total > 0) return W_FLAT_BLACK;
- else return W_FLAT_WHITE;
- }
-
- // Road?
+enum E_RESULT
+check_road_colour(const enum COLOUR colour) {
uint8_t dfs_stack[board_size * board_size];
uint8_t dfs_pntr = 0;
// Prime the depth-first-search stack with all boundary cells of
- // colour COLOUR, we're using the two left-over bits in data_t to
- // track whether we've seen it. Reset those before anything.
+ // colour COLOUR.
- for (uint8_t k=0; k<board_size*board_size; k++) {
- celldat[k] &= USED_MASK;
- }
-
- enum WIN_RESULT res = (colour == C_BLACK) ?
- W_ROAD_BLACK : W_ROAD_WHITE;
+ const enum E_RESULT winner =
+ (colour == C_BLACK) ? W_ROAD_BLACK : W_ROAD_WHITE;
// First left-to-right
for (uint8_t y=0; y<board_size; y++) {
@@ -261,7 +267,7 @@ check_win(const enum COLOUR colour) {
celldat[THE_COORDS(0, y)] |= DFS_MASK;
}
}
- if (dfs_road(dfs_stack, dfs_pntr, colour, 0)) return res;
+ if (dfs_road(dfs_stack, dfs_pntr, colour, 0)) return winner;
// Then top-to-bottom
for (uint8_t x=1; x+1<board_size; x++) {
@@ -270,7 +276,51 @@ check_win(const enum COLOUR colour) {
celldat[THE_COORDS(x, 0)] |= DFS_MASK;
}
}
- if (dfs_road(dfs_stack, dfs_pntr, colour, 1)) return res;
+ if (dfs_road(dfs_stack, dfs_pntr, colour, 1)) return winner;
return W_NONE;
}
+
+enum E_RESULT
+check_win(void) {
+ // Do we do a flat count?
+ if (black_flats == 0 || white_flats == 0 || board_full()) {
+ int8_t total = 0;
+ for (uint8_t k = 0; k < board_size * board_size; k++) {
+ if (STONE_AT(k) == STONE_FLAT) {
+ total += ((colours[k] & 1) == C_BLACK) ? +1 : -1 ;
+ }
+ }
+ if (total > 0) {
+ return W_FLAT_BLACK;
+ } else if (total < 0) {
+ return W_FLAT_WHITE;
+ } else {
+ return W_DRAW;
+ }
+ }
+
+ // Road?
+
+ // We're using the two left-over bits in data_t to track whether
+ // we've seen it. Reset those before anything. No need to do it
+ // between checks, however, as pieces are black XOR white.
+
+ for (uint8_t k=0; k<board_size*board_size; k++) {
+ celldat[k] &= USED_MASK;
+ }
+
+ enum E_RESULT rb, rw;
+ rb = check_road_colour(C_BLACK);
+ rw = check_road_colour(C_WHITE);
+
+ if (rb == W_ROAD_BLACK) {
+ if (rw == W_ROAD_WHITE) {
+ return W_DRAGON;
+ } else {
+ return W_ROAD_BLACK;
+ }
+ } else {
+ return rw;
+ }
+}