From c601baf9004b7defb03caf3d5794c28fc2311f30 Mon Sep 17 00:00:00 2001 From: tslil Date: Mon, 4 Jan 2021 00:12:33 -0500 Subject: Fixed indentation and some bugs, stats programme --- include/tak.c | 133 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 68 insertions(+), 65 deletions(-) (limited to 'include') diff --git a/include/tak.c b/include/tak.c index cd9c23c..787e46a 100644 --- a/include/tak.c +++ b/include/tak.c @@ -51,7 +51,7 @@ void next_ply(void) { ply++; if (ply == 2) { - current_colour = C_WHITE; + current_colour = C_WHITE; } else { if (current_colour == C_BLACK) current_colour = C_WHITE; else current_colour = C_BLACK; @@ -64,7 +64,7 @@ next_ply(void) { enum E_RESULT try_place(const int8_t location, const enum COLOUR colour, - const enum STONE_VARIANT stone) + const enum STONE_VARIANT stone) { // Game is over? if (won < 0xFF) return GAME_END; @@ -74,30 +74,30 @@ try_place(const int8_t location, const enum COLOUR colour, } else { switch (stone) { case STONE_STANDING: - if (ply < 2) return ACT_ILLEGAL; - // behold the magic GCC comment which defeates - // -Wimplicit-fallthrough: - // fall through + if (ply < 2) return ACT_ILLEGAL; + // behold the magic GCC comment which defeates + // -Wimplicit-fallthrough: + // fall through case STONE_FLAT: { - if (colour == C_BLACK) { - if (black_count & 127) black_count--; - else return ACT_ILLEGAL; - } else { - if (white_count & 127) white_count--; - else return ACT_ILLEGAL; - } - break; + if (colour == C_BLACK) { + if (black_count & 127) black_count--; + else return ACT_ILLEGAL; + } else { + if (white_count & 127) white_count--; + else return ACT_ILLEGAL; + } + break; } case STONE_CAPSTONE: { - if (ply < 2) return ACT_ILLEGAL; - if (colour == C_BLACK) { - if (black_count & 128) black_count &= 127; - else return ACT_ILLEGAL; - } else { - if (white_count & 128) white_count &= 127; - else return ACT_ILLEGAL; - } - break; + if (ply < 2) return ACT_ILLEGAL; + if (colour == C_BLACK) { + if (black_count & 128) black_count &= 127; + else return ACT_ILLEGAL; + } else { + if (white_count & 128) white_count &= 127; + else return ACT_ILLEGAL; + } + break; } } @@ -112,21 +112,23 @@ try_place(const int8_t location, const enum COLOUR colour, // =================================================================== static void -push_stones(const int8_t location, const uint8_t count, const uint8_t new_colours, - const enum STONE_VARIANT top_stone) { +push_stones(const int8_t location, const uint8_t count, + const uint8_t new_colours, + const enum STONE_VARIANT top_stone) { colours[location] = (colours[location] << count) | new_colours; - celldat[location] = ((celldat[location] + ((count << NUM_SHIFT))) & NUM_MASK) | top_stone; + celldat[location] = top_stone + | ((celldat[location] + ((count << NUM_SHIFT))) & NUM_MASK); } enum E_RESULT try_move(const int8_t location, const enum MOVE_DIRECTION direction, - const uint8_t steps, const uint8_t drops[5]) { + const uint8_t steps, const uint8_t drops[5]) { // Game is over? if (won < 0xFF) return GAME_END; // Can't do this if (steps == 0 || steps > 5) return ACT_ILLEGAL; // Is the desired direction and count on the board? - uint8_t delta = 0; + int8_t delta = 0; switch (direction) { case M_UP: { delta = +board_size; @@ -167,11 +169,11 @@ try_move(const int8_t location, const enum MOVE_DIRECTION direction, return ACT_ILLEGAL; // Check for wall if ( (STONE_AT(location+(k+1)*delta) == STONE_STANDING) - // If not last drop, or not dropping just one, or not a cap - && ( (k+1 < steps) - || (drops[k] != 1) - || (STONE_AT(location) != STONE_CAPSTONE) ) - ) + // If not last drop, or not dropping just one, or not a cap + && ( (k+1 < steps) + || (drops[k] != 1) + || (STONE_AT(location) != STONE_CAPSTONE) ) + ) return ACT_ILLEGAL; total += drops[k]; } @@ -187,9 +189,9 @@ try_move(const int8_t location, const enum MOVE_DIRECTION direction, for (uint8_t k = 0; k < steps; k++) { j -= drops[k]; push_stones(location+(k+1)*delta, - drops[k], - (colours[location] >> j) & (0xFFFF >> (0x10 - drops[k])), - (k == steps - 1) ? STONE_AT(location) : STONE_FLAT); + drops[k], + (colours[location] >> j) & (0xFFFF >> (0x10 - drops[k])), + (k == steps - 1) ? STONE_AT(location) : STONE_FLAT); } // Then we drop them from the source colours[location] >>= total; @@ -208,49 +210,49 @@ try_move(const int8_t location, const enum MOVE_DIRECTION direction, // direction = 0 --> left-to-right, direction = 1 --> bottom-to-top static uint8_t dfs_road(uint8_t dfs_stack[NUM_SQUARES], uint8_t dfs_pntr, - const enum COLOUR colour, const uint8_t direction) { + const enum COLOUR colour, const uint8_t direction) { while (dfs_pntr > 0) { const uint8_t cur = dfs_stack[--dfs_pntr]; // Made it to the other side? if ( (direction == 1 && cur >= board_size * (board_size - 1)) - || (direction == 0 && cur % board_size == 1) ) - return 1; + || (direction == 0 && cur % board_size + 1 == board_size) ) + return 1; // Check the four neighbours of this cell, provided they exist, // are inhabited, and are of the appropriate colour // Direction: > (same row) if ( ((cur % board_size) + 1 < board_size) - && (COUNT_AT(cur + 1)) // wont ever be out of bounds - && ((colours[cur+1] & 1) == colour) - && ((celldat[cur+1] & DFS_MASK) == 0) ) { + && (COUNT_AT(cur + 1)) // wont ever be out of bounds + && ((colours[cur+1] & 1) == colour) + && ((celldat[cur+1] & DFS_MASK) == 0) ) { dfs_stack[dfs_pntr++] = cur + 1; celldat[cur+1] |= DFS_MASK; } // Direction: < (same row) if ( (cur % board_size > 0) - && (COUNT_AT(cur - 1)) // wont ever be out of bounds - && ((colours[cur-1] & 1) == colour) - && ((celldat[cur-1] & DFS_MASK) == 0) ) { + && (COUNT_AT(cur - 1)) // wont ever be out of bounds + && ((colours[cur-1] & 1) == colour) + && ((celldat[cur-1] & DFS_MASK) == 0) ) { dfs_stack[dfs_pntr++] = cur - 1; celldat[cur-1] |= DFS_MASK; } // Direction: + if ( (cur + board_size < NUM_SQUARES) - && (COUNT_AT(cur + board_size)) - && ((colours[cur+board_size] & 1) == colour) - && ((celldat[cur+board_size] & DFS_MASK) == 0) ) { + && (COUNT_AT(cur + board_size)) + && ((colours[cur+board_size] & 1) == colour) + && ((celldat[cur+board_size] & DFS_MASK) == 0) ) { dfs_stack[dfs_pntr++] = cur + board_size; celldat[cur+board_size] |= DFS_MASK; } // Direction: - if ( (cur >= board_size) - && (COUNT_AT(cur - board_size)) - && ((colours[cur-board_size] & 1) == colour) - && ((celldat[cur-board_size] & DFS_MASK) == 0) ) { + && (COUNT_AT(cur - board_size)) + && ((colours[cur-board_size] & 1) == colour) + && ((celldat[cur-board_size] & DFS_MASK) == 0) ) { dfs_stack[dfs_pntr++] = cur - board_size; celldat[cur-board_size] |= DFS_MASK; } @@ -279,7 +281,7 @@ check_road_colour(const enum COLOUR colour) { dfs_pntr = 0; for (uint8_t y=0; y 6) return PTN_INVALID; @@ -382,8 +384,8 @@ parse_place(const uint8_t board_size, char *ptn, enum E_RESULT parse_move(const uint8_t board_size, char *ptn, - uint8_t *out_location, enum MOVE_DIRECTION *out_direction, - uint8_t *out_steps, uint8_t out_drops[5]) { + uint8_t *out_location, enum MOVE_DIRECTION *out_direction, + uint8_t *out_steps, uint8_t out_drops[5]) { if (board_size < 5 || board_size > 6) return PTN_INVALID; @@ -458,7 +460,7 @@ parse_move(const uint8_t board_size, char *ptn, void generate_place(const uint8_t board_size, const uint8_t in_location, - const enum STONE_VARIANT in_stone, char out_ptn[4]) { + const enum STONE_VARIANT in_stone, char out_ptn[4]) { switch (in_stone) { case STONE_FLAT: { break; } case STONE_STANDING: { *out_ptn = 'S'; out_ptn++; break; } @@ -475,9 +477,9 @@ generate_place(const uint8_t board_size, const uint8_t in_location, void generate_move(const uint8_t board_size, const uint8_t in_location, - const enum MOVE_DIRECTION in_direction, - const uint8_t in_steps, const uint8_t in_drops[5], - char out_ptn[10]) { + const enum MOVE_DIRECTION in_direction, + const uint8_t in_steps, const uint8_t in_drops[5], + char out_ptn[10]) { uint8_t total = 0; for (uint8_t k = 0; k 1) { @@ -534,7 +536,7 @@ do_ptn(char *ptn) { enum MOVE_DIRECTION direction; // Parse it as a move res = parse_move(board_size, ptn, &location, - &direction, &steps, drops); + &direction, &steps, drops); // If valid PTN, try to do it if (res == PTN_VALID) { if (ply < 2) return ACT_ILLEGAL; @@ -551,14 +553,15 @@ do_ptn(char *ptn) { } // A valid ply occured if (res == ACT_OK) { - next_ply(); // Don't bother checking that the game was won early on, could be // more conservative here :) - if (ply > board_size) { + if (ply >= board_size) { won = check_win(); if (won < 0xFF) return GAME_END; } + // Only step if the game isn't over yet + next_ply(); } - return res; + return res; } -- cgit v1.3.1