From 4a8e687b14b50e3a2a4a3632f6553872f4c301d5 Mon Sep 17 00:00:00 2001 From: tslil Date: Tue, 29 Dec 2020 18:55:06 -0500 Subject: PTN code looks correct, minor tweaks and header guards --- .clang_complete | 1 + include/enums.h | 3 +++ include/ptn.c | 56 +++++++++++++++++++++++++++++++++++--------------------- include/ptn.h | 6 +++--- include/state.c | 8 +++++--- include/state.h | 7 +++---- src/ctaklm.c | 24 ++++++++++++++++++------ 7 files changed, 68 insertions(+), 37 deletions(-) create mode 100644 .clang_complete diff --git a/.clang_complete b/.clang_complete new file mode 100644 index 0000000..3e2e760 --- /dev/null +++ b/.clang_complete @@ -0,0 +1 @@ +-Iinclude/ diff --git a/include/enums.h b/include/enums.h index 1df2a4b..3f1e172 100644 --- a/include/enums.h +++ b/include/enums.h @@ -1,5 +1,8 @@ +#ifndef ENUMS_H_INCLUDE +#define ENUMS_H_INCLUDE enum COLOUR { C_WHITE, C_BLACK }; enum STONE_VARIANT { STONE_FLAT, STONE_STANDING, STONE_CAPSTONE }; enum MOVE_DIRECTION { M_UP, M_DOWN, M_LEFT, M_RIGHT }; enum ACTION_RESULT { A_OK, A_ILLEGAL, A_OVERFLOW }; enum WIN_RESULT { W_NONE, W_ROAD_WHITE, W_ROAD_BLACK, W_FLAT_WHITE, W_FLAT_BLACK }; +#endif diff --git a/include/ptn.c b/include/ptn.c index 58648b9..1a333d7 100644 --- a/include/ptn.c +++ b/include/ptn.c @@ -5,26 +5,28 @@ enum PTN_PARSE_RESULT parse_place(const uint8_t board_size, char *ptn, - uint8_t *location, enum STONE_VARIANT *stone) { + uint8_t *out_location, enum STONE_VARIANT *out_stone) { + + if (board_size < 5 || board_size > 6) return PTN_INVALID; ASSERT_NONEMPTY; - *stone = STONE_FLAT; + *out_stone = STONE_FLAT; switch (*ptn) { - case 'C' : { ptn++; *stone = STONE_CAPSTONE; break; }; - case 'S' : { ptn++; *stone = STONE_STANDING; break; }; + case 'C' : { ptn++; *out_stone = STONE_CAPSTONE; break; }; + case 'S' : { ptn++; *out_stone = STONE_STANDING; break; }; case 'F' : { ptn++; break; }; } ASSERT_MORE; if ( (*ptn < 'a') || (*ptn > '`' + board_size) ) return PTN_INVALID; - *location = *ptn - 'a'; + *out_location = *ptn - 'a'; ptn++; ASSERT_MORE; if ( (*ptn < '1') || (*ptn > board_size + '0') ) return PTN_INVALID; - *location += board_size * (*ptn - '1'); + *out_location += board_size * (*ptn - '1'); if (*(++ptn) > 0) return PTN_INVALID; @@ -33,8 +35,10 @@ parse_place(const uint8_t board_size, char *ptn, enum PTN_PARSE_RESULT parse_move(const uint8_t board_size, char *ptn, - uint8_t *location, enum MOVE_DIRECTION *direction, - uint8_t *steps, uint8_t 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; ASSERT_NONEMPTY; @@ -46,37 +50,47 @@ parse_move(const uint8_t board_size, char *ptn, } if ( (*ptn < 'a') || (*ptn > '`' + board_size) ) return PTN_INVALID; - *location = *ptn - 'a'; + *out_location = *ptn - 'a'; ptn++; ASSERT_MORE; if ( (*ptn < '1') || (*ptn > board_size + '0') ) return PTN_INVALID; - *location += board_size * (*ptn - '1'); + *out_location += board_size * (*ptn - '1'); ptn++; ASSERT_MORE; switch (*ptn) { - case '+': { *direction = M_UP; break; } - case '-': { *direction = M_DOWN; break; } - case '<': { *direction = M_LEFT; break; } - case '>': { *direction = M_RIGHT; break; } + case '+': { *out_direction = M_UP; break; } + case '-': { *out_direction = M_DOWN; break; } + case '<': { *out_direction = M_LEFT; break; } + case '>': { *out_direction = M_RIGHT; break; } default: return PTN_INVALID; } - ptn++; ASSERT_MORE; + ptn++; + // Handle the case '' as + // '11' for convenience + if (*ptn == 0) { + if (picked_up == 0) { + *out_steps = 1; + out_drops[0] = 1; + } else { + return PTN_INVALID; + } + } - *steps = 0; + *out_steps = 0; uint8_t total = 0; while (*ptn) { if ( (*ptn < '1') || (*ptn > '0' + board_size) ) { return PTN_INVALID; } - // TODO: Correct? - if ( (*steps >= board_size) && *ptn) return PTN_INVALID; - drops[*steps] = *ptn - '0'; - total += drops[*steps]; - *steps += 1; + if ( (*out_steps + 1 >= board_size) && *ptn) return PTN_INVALID; + + out_drops[*out_steps] = *ptn - '0'; + total += out_drops[*out_steps]; + *out_steps += 1; ptn++; } diff --git a/include/ptn.h b/include/ptn.h index 715a7d0..e780a70 100644 --- a/include/ptn.h +++ b/include/ptn.h @@ -5,9 +5,9 @@ enum PTN_PARSE_RESULT { PTN_VALID, PTN_INVALID }; enum PTN_PARSE_RESULT parse_place(const uint8_t board_size, char *ptn, - uint8_t *location, enum STONE_VARIANT *stone); + uint8_t *out_location, enum STONE_VARIANT *out_stone); enum PTN_PARSE_RESULT parse_move(const uint8_t board_size, char *ptn, - uint8_t *location, enum MOVE_DIRECTION *direction, - uint8_t *steps, uint8_t drops[5]); + uint8_t *out_location, enum MOVE_DIRECTION *out_direction, + uint8_t *out_steps, uint8_t out_drops[5]); diff --git a/include/state.c b/include/state.c index 1505881..3708792 100644 --- a/include/state.c +++ b/include/state.c @@ -18,14 +18,16 @@ // ------------------------------------------------------------------- // Game management -void reset_game() { - if (board_size == 6) { +void reset_state(const uint8_t new_board_size) { + if (new_board_size == 6) { + board_size = 6; white_flats = 30; black_flats = 30; } else { board_size = 5; white_flats = 21; black_flats = 21; } - white_caps = 1; black_caps = 1; + white_caps = 1; + black_caps = 1; turn = 0; diff --git a/include/state.h b/include/state.h index 428a998..17d705a 100644 --- a/include/state.h +++ b/include/state.h @@ -1,16 +1,15 @@ #include #include "enums.h" -static uint8_t board_size = 5; - typedef uint16_t colour_stack_t; typedef uint8_t data_t; -static colour_stack_t colours[36]; +static uint8_t board_size; static data_t celldat[36]; +static colour_stack_t colours[36]; static uint8_t white_flats, black_flats, white_caps, black_caps, turn; -void reset_game(void); +void reset_state(const uint8_t new_board_size); enum ACTION_RESULT try_place(const int8_t location, const enum COLOUR colour, diff --git a/src/ctaklm.c b/src/ctaklm.c index 3585bec..f6ccfe8 100644 --- a/src/ctaklm.c +++ b/src/ctaklm.c @@ -1,16 +1,28 @@ -#include #include #include +#include int main(int argc, char **argv) { - reset_game(); + reset_state(5); enum ACTION_RESULT res = try_place(0, 0x0001, STONE_FLAT); res = try_place(1, 0x0001, STONE_FLAT); - const uint8_t drops[1] = {1}; - res = try_move(1, M_LEFT, 1, drops); + const uint8_t d[1] = {1}; + res = try_move(1, M_LEFT, 1, d); - printf("Result: %d\n",res); - printf("%o %o \n",colours[0],celldat[0]); + uint8_t location; + enum STONE_VARIANT stone; + enum PTN_PARSE_RESULT pr = parse_place(5, "Cb3", &location, &stone); + + printf("Place <%d>: stone %d at (%d,%d)\n",pr,stone,location%5,location/5); + + enum MOVE_DIRECTION direction; + uint8_t steps; + uint8_t drops[5]; + pr = parse_move(5, "c2+", &location, &direction, &steps, drops); + + printf("Move <%d>: from (%d,%d) step %d sequence [%d,%d,%d,%d,%d]\n", + pr,location%5,location/5, + steps,drops[0],drops[1],drops[2],drops[3],drops[4]); } -- cgit v1.3.1