From 40c6b1dafab4de169bac8a799e7953e85061218e Mon Sep 17 00:00:00 2001 From: tslil clingman Date: Tue, 2 Feb 2021 19:23:54 -0500 Subject: TEI interface working! --- include/tak.c | 61 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 29 deletions(-) (limited to 'include/tak.c') diff --git a/include/tak.c b/include/tak.c index c9c761c..783bea8 100644 --- a/include/tak.c +++ b/include/tak.c @@ -77,7 +77,7 @@ next_ply(void) { // Placing stones // =================================================================== -enum E_RESULT +enum ACT_RESULT try_place(const int8_t location, const enum COLOUR colour, const enum STONE_VARIANT stone) { @@ -135,7 +135,7 @@ push_stones(const int8_t location, const uint8_t count, | ((celldat[location] + ((count << NUM_SHIFT))) & NUM_MASK); } -enum E_RESULT +enum ACT_RESULT try_move(const int8_t location, const enum MOVE_DIRECTION direction, const uint8_t steps, const uint8_t drops[5]) { // Game is over? @@ -380,14 +380,17 @@ check_win(void) { // PTN place parser // =================================================================== -#define ASSERT_NONEMPTY { if (ptn == 0 || *ptn == 0) return PTN_INVALID; } -#define ASSERT_MORE { if (*ptn == 0) return PTN_INVALID; } +#define NULL 0 -enum E_RESULT -parse_place(const uint8_t board_size, char *ptn, - uint8_t *out_location, enum STONE_VARIANT *out_stone) { +#define ASSERT_NONEMPTY { \ + if (ptn == NULL || *ptn == 0) return PTN_INVALID; \ + } + +#define ASSERT_MORE { if (*ptn == 0) return PTN_INVALID; } - if (board_size < 5 || board_size > 6) return PTN_INVALID; +enum PTN_RESULT +parse_place(char *ptn, uint8_t *out_location, + enum STONE_VARIANT *out_stone) { ASSERT_NONEMPTY; @@ -410,20 +413,18 @@ parse_place(const uint8_t board_size, char *ptn, if (*(++ptn) > 0) return PTN_INVALID; - return PTN_VALID; + return PTN_OK; } // =================================================================== // PTN move parser // =================================================================== -enum E_RESULT -parse_move(const uint8_t board_size, char *ptn, - uint8_t *out_location, enum MOVE_DIRECTION *out_direction, +enum PTN_RESULT +parse_move(char *ptn, 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; uint8_t picked_up = 1; @@ -462,7 +463,7 @@ parse_move(const uint8_t board_size, char *ptn, if (*ptn == 0) { *out_steps = 1; out_drops[0] = picked_up; - return PTN_VALID; + return PTN_OK; } // Parse the drops in each subsequent square @@ -486,7 +487,7 @@ parse_move(const uint8_t board_size, char *ptn, // Mismatch between number of stones picked up and total dropped if ( total != picked_up ) return PTN_INVALID; - return PTN_VALID; + return PTN_OK; } // =================================================================== @@ -531,7 +532,6 @@ generate_move(const uint8_t in_location, 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++; } @@ -557,12 +557,13 @@ is_not_placement(char *ptn) { } } -enum E_RESULT +enum ACT_RESULT do_ptn(char *ptn) { // Game over? if (won < 0xFF) return GAME_END; - enum E_RESULT res; + enum PTN_RESULT ptn_res; + enum ACT_RESULT act_res; uint8_t location; // Placing or moving? @@ -570,24 +571,27 @@ do_ptn(char *ptn) { uint8_t steps, drops[5]; enum MOVE_DIRECTION direction; // Parse it as a move - res = parse_move(board_size, ptn, &location, - &direction, &steps, drops); + ptn_res = parse_move(ptn, &location, &direction, &steps, drops); // If valid PTN, try to do it - if (res == PTN_VALID) { + if (ptn_res == PTN_OK) { if (ply < 2) return ACT_ILLEGAL; - res = try_move(location, direction, steps, drops); + act_res = try_move(location, direction, steps, drops); + } else { + return ACT_INVALID_PTN; } } else { // It was not a move enum STONE_VARIANT stone; // Was it a valid placement? - res = parse_place(board_size, ptn, &location, &stone); + ptn_res = parse_place(ptn, &location, &stone); // If so, try it - if (res == PTN_VALID) - res = try_place(location, current_colour, stone); + if (ptn_res == PTN_OK) + act_res = try_place(location, current_colour, stone); + else + return ACT_INVALID_PTN; } // A valid ply occured - if (res == ACT_OK) { + if (act_res == ACT_OK) { // Don't bother checking that the game was won early on, could be // more conservative here :) if (ply >= board_size) { @@ -601,6 +605,5 @@ do_ptn(char *ptn) { // Only step if the game isn't over yet next_ply(); } - - return res; + return act_res; } -- cgit v1.2.3