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 --- include/ptn.c | 56 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 21 deletions(-) (limited to 'include/ptn.c') 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++; } -- cgit v1.3.1