diff options
| author | tslil <tslil@posteo.de> | 2020-12-30 17:16:49 -0500 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-08-28 19:37:41 +0100 |
| commit | aeaed8da4ee5e58660223bec2d28aacd0cf8276a (patch) | |
| tree | 89f12eb812a66ce807eaca910c92d6b0cae5654d | |
| parent | 0a3b2d825077b5e6c5f1cf4a0dcb13fdfffc0be6 (diff) | |
OBOEs in try_move + logic errors
- Corrected use of static
- Implemented next_turn
| -rw-r--r-- | include/state.c | 41 | ||||
| -rw-r--r-- | include/state.h | 10 | ||||
| -rw-r--r-- | include/tak.c | 6 | ||||
| -rw-r--r-- | include/tak.h | 4 | ||||
| -rw-r--r-- | src/ctaklm.c | 17 |
5 files changed, 33 insertions, 45 deletions
diff --git a/include/state.c b/include/state.c index dfdd02b..94d0773 100644 --- a/include/state.c +++ b/include/state.c @@ -40,24 +40,13 @@ reset_state(const uint8_t new_board_size) { 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) - } - } - } - */ + turn++; + if (turn == 2) { + current_colour = C_WHITE; + } else { + if (current_colour == C_BLACK) current_colour = C_WHITE; + else current_colour = C_BLACK; + } } // ------------------------------------------------------------------- @@ -104,18 +93,18 @@ try_place(const int8_t location, const enum COLOUR colour, // ------------------------------------------------------------------- // Move stack -void +static void 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; } -void +static void drop_stones(const int8_t location, const uint8_t count) { // Calling this with count = 0 is destructive colours[location] >>= count; - const uint8_t dec_count = celldat[location] + (((~count) << NUM_SHIFT)); + const uint8_t dec_count = celldat[location] - (count << NUM_SHIFT); celldat[location] = dec_count & NUM_MASK; } @@ -186,11 +175,11 @@ try_move(const int8_t location, const enum MOVE_DIRECTION direction, j -= drops[k]; push_stones(location+(k+1)*delta, drops[k], - (colours[location] >> j) & (0xFFFF >> (16 - drops[k])), + (colours[location] >> j) & (0xFFFF >> (0x10 - drops[k])), (k == steps - 1) ? STONE_AT(location) : STONE_FLAT); } - drop_stones(location, COUNT_AT(location)-total); + drop_stones(location, total); return A_OK; } @@ -198,7 +187,7 @@ try_move(const int8_t location, const enum MOVE_DIRECTION direction, // ------------------------------------------------------------------- // Win conditions -uint8_t +static uint8_t board_full(void) { for (uint8_t k; k < board_size*board_size; k++) { if (COUNT_AT(k) == 0) return 0; @@ -207,7 +196,7 @@ board_full(void) { } // direction == 0 --> left-to-right, otherwise --> top-to-bottom -uint8_t +static uint8_t dfs_road(uint8_t dfs_stack[board_size*board_size], uint8_t dfs_pntr, const enum COLOUR colour, const uint8_t direction) { while (dfs_pntr > 0) { @@ -249,7 +238,7 @@ dfs_road(uint8_t dfs_stack[board_size*board_size], uint8_t dfs_pntr, return 0; } -enum E_RESULT +static enum E_RESULT check_road_colour(const enum COLOUR colour) { uint8_t dfs_stack[board_size * board_size]; uint8_t dfs_pntr = 0; diff --git a/include/state.h b/include/state.h index 218bf71..4f0528c 100644 --- a/include/state.h +++ b/include/state.h @@ -4,11 +4,11 @@ typedef uint16_t colour_stack_t; typedef uint8_t data_t; -static uint8_t board_size; -static data_t celldat[36]; -static colour_stack_t colours[36]; -static enum COLOUR current_colour; -static uint8_t white_flats, black_flats, white_caps, black_caps, turn; +uint8_t board_size; +data_t celldat[36]; +colour_stack_t colours[36]; +enum COLOUR current_colour; +uint8_t white_flats, black_flats, white_caps, black_caps, turn; void reset_state(const uint8_t new_board_size); void next_turn(void); diff --git a/include/tak.c b/include/tak.c index 85d04d4..1c47aeb 100644 --- a/include/tak.c +++ b/include/tak.c @@ -1,7 +1,7 @@ #include "tak.h" enum E_RESULT -try_ptn(char *ptn) { +do_ptn(char *ptn) { enum E_RESULT res; uint8_t location; @@ -11,8 +11,10 @@ try_ptn(char *ptn) { enum MOVE_DIRECTION direction; res = parse_move(board_size, ptn, &location, &direction, &steps, drops); - if (res == PTN_VALID) + if (res == PTN_VALID) { + if (turn < 2) return A_ILLEGAL; res = try_move(location, direction, steps, drops); + } } else { enum STONE_VARIANT stone; res = parse_place(board_size, ptn, &location, &stone); diff --git a/include/tak.h b/include/tak.h index 1c0ef50..4b103a0 100644 --- a/include/tak.h +++ b/include/tak.h @@ -1,7 +1,5 @@ #include "state.h" #include "ptn.h" -void reset_game(const uint8_t new_board_size); - enum E_RESULT -try_ptn(char *ptn); +do_ptn(char *ptn); diff --git a/src/ctaklm.c b/src/ctaklm.c index d7fd7c1..885da7a 100644 --- a/src/ctaklm.c +++ b/src/ctaklm.c @@ -6,24 +6,23 @@ int main(int argc, char **argv) { reset_state(5); - enum ACTION_RESULT res = try_place(0, 0x0001, STONE_FLAT); - res = try_place(1, 0x0001, STONE_FLAT); - const uint8_t d[1] = {1}; - res = try_move(1, M_LEFT, 1, d); + enum E_RESULT r = try_place(0, C_BLACK, STONE_FLAT); + r = try_place(1, C_BLACK, STONE_FLAT); + uint8_t drops[5] = {1,0,0,0,0}; + r = try_move(1, M_LEFT, 1, drops); uint8_t location; enum STONE_VARIANT stone; - enum PTN_PARSE_RESULT pr = parse_place(5, "Cb3", &location, &stone); + r = parse_place(5, "Cb3", &location, &stone); - printf("Place <%d>: stone %d at (%d,%d)\n",pr,stone,location%5,location/5); + printf("Place <%d>: stone %d at (%d,%d)\n",r,stone,location%5,location/5); enum MOVE_DIRECTION direction; uint8_t steps; - uint8_t drops[5]; - pr = parse_move(5, "3c2+12", &location, &direction, &steps, drops); + r = parse_move(5, "3c2+12", &location, &direction, &steps, drops); printf("Move <%d>: from (%d,%d) step %d sequence [%d,%d,%d,%d,%d]\n", - pr,location%5,location/5, + r,location%5,location/5, steps,drops[0],drops[1],drops[2],drops[3],drops[4]); char buf[10]; |
