diff options
| author | tslil <tslil@posteo.de> | 2020-12-29 18:55:06 -0500 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-08-28 19:37:41 +0100 |
| commit | 4a8e687b14b50e3a2a4a3632f6553872f4c301d5 (patch) | |
| tree | 105efe213f5d69eaa8d9a40531a4ccb72a1c793e /include | |
| parent | fd22e4cf3198c1e63cf26dd5fdce588a1e969ac1 (diff) | |
PTN code looks correct, minor tweaks and header guards
Diffstat (limited to 'include')
| -rw-r--r-- | include/enums.h | 3 | ||||
| -rw-r--r-- | include/ptn.c | 56 | ||||
| -rw-r--r-- | include/ptn.h | 6 | ||||
| -rw-r--r-- | include/state.c | 8 | ||||
| -rw-r--r-- | include/state.h | 7 |
5 files changed, 49 insertions, 31 deletions
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 '<column><row><direction>' as + // '1<column><row><direction>1' 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 <stdint.h> #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, |
