From e57746530836b24b6d8d20e41239e955fa9e20d4 Mon Sep 17 00:00:00 2001 From: tslil Date: Tue, 29 Dec 2020 19:33:00 -0500 Subject: Output PTN as well --- include/ptn.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- include/ptn.h | 10 ++++++++++ src/ctaklm.c | 10 +++++++++- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/include/ptn.c b/include/ptn.c index 1a333d7..66b4ad8 100644 --- a/include/ptn.c +++ b/include/ptn.c @@ -42,7 +42,7 @@ parse_move(const uint8_t board_size, char *ptn, ASSERT_NONEMPTY; - uint8_t picked_up = 0; + uint8_t picked_up = 1; if ( (*ptn >= '1') && (*ptn <= '0' + board_size)) { picked_up = *ptn - '0'; @@ -68,10 +68,10 @@ parse_move(const uint8_t board_size, char *ptn, } ptn++; - // Handle the case '' as + // Handle the case '[1]' as // '11' for convenience if (*ptn == 0) { - if (picked_up == 0) { + if (picked_up == 1) { *out_steps = 1; out_drops[0] = 1; } else { @@ -94,7 +94,49 @@ parse_move(const uint8_t board_size, char *ptn, ptn++; } - if ( (picked_up > 0) && (total != picked_up) ) return PTN_INVALID; + if ( total != picked_up ) return PTN_INVALID; return PTN_VALID; } + +void +generate_place(const uint8_t board_size, const uint8_t in_location, + 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; } + case STONE_CAPSTONE: { *out_ptn = 'C'; out_ptn++; break; } + } + *out_ptn = 'a' + (in_location % board_size); out_ptn++; + *out_ptn = '1' + (in_location / board_size); out_ptn++; + *out_ptn = 0; +} + +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]) { + uint8_t total = 0; + for (uint8_t k = 0; k 1) { + *out_ptn = '0' + total; out_ptn++; + } + + *out_ptn = 'a' + (in_location % board_size); out_ptn++; + *out_ptn = '1' + (in_location / board_size); out_ptn++; + + switch (in_direction) { + case M_UP: { *out_ptn = '+'; break; } + case M_DOWN: { *out_ptn = '-'; break; } + case M_LEFT: { *out_ptn = '<'; break; } + case M_RIGHT: { *out_ptn = '>'; break; } + }; out_ptn++; + + + for (uint8_t k = 0; (total > 1) && (k < in_steps); k++) { + *out_ptn = '0' + in_drops[k]; out_ptn++; + } + + *out_ptn = 0; +} diff --git a/include/ptn.h b/include/ptn.h index e780a70..0baed01 100644 --- a/include/ptn.h +++ b/include/ptn.h @@ -11,3 +11,13 @@ enum PTN_PARSE_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]); + +void +generate_place(const uint8_t board_size, const uint8_t in_location, + const enum STONE_VARIANT in_stone, char out_ptn[4]); + +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]); diff --git a/src/ctaklm.c b/src/ctaklm.c index f6ccfe8..d7fd7c1 100644 --- a/src/ctaklm.c +++ b/src/ctaklm.c @@ -20,9 +20,17 @@ int main(int argc, char **argv) { enum MOVE_DIRECTION direction; uint8_t steps; uint8_t drops[5]; - pr = parse_move(5, "c2+", &location, &direction, &steps, drops); + pr = 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, steps,drops[0],drops[1],drops[2],drops[3],drops[4]); + + char buf[10]; + generate_move(5, 2+5*1, M_UP, 1, drops, buf); + printf("Generated move: %s\n",buf); + + generate_place(5,1+5*2, STONE_CAPSTONE, buf); + printf("Generated place: %s\n",buf); + } -- cgit v1.2.3