aboutsummaryrefslogtreecommitdiff
path: root/include/tak.h
diff options
context:
space:
mode:
authortslil clingman <tslil@posteo.de>2023-01-15 21:31:00 +0100
committertslil <tslil@posteo.de>2026-08-28 19:37:41 +0100
commit0223a9bec5535fced1a7698b55fd42155d9b0446 (patch)
treee7a980454e65d88b56194eed733cabec29ef51b5 /include/tak.h
parentee216c008a188a9436fedb85c70ee5d1719733b1 (diff)
switch to explicit game state & important bug fix & clang format
Previously the code base assumed that there was a single, global game state which was the implicit target of all actions taken. Looking ahead at architectural improvements, this has now been (almost entirely) made explicit and functions take tak_state_p where necessary (and also where unnecessary). Two important fixes to actions.c were made: - Previously when generating the possible stack moves, stack height overflows (> 15) were not taken into account and this resulted in the tree search corrupting the board state. Now action search does not list all legal actions, rather the subset of these encodeable by the implementation. - The check for crushing on a stack move was incorrect (too strict), and this resulted in many legitimate moves being igonored. Finally, in other changes, weights have also been improved by training all games instead of some subset for chosen players, and clang-format was run on the codebase.
Diffstat (limited to 'include/tak.h')
-rw-r--r--include/tak.h53
1 files changed, 27 insertions, 26 deletions
diff --git a/include/tak.h b/include/tak.h
index 26f4ae6..9f458f6 100644
--- a/include/tak.h
+++ b/include/tak.h
@@ -49,60 +49,61 @@ enum WIN_TYPE {
typedef uint8_t data_t;
typedef uint16_t colour_stack_t;
+typedef struct tak_state_s {
+ data_t celldat[36];
+ colour_stack_t colours[36];
+ enum WIN_TYPE won;
+ enum COLOUR current_colour;
+ uint8_t white_count, black_count, ply, board_size;
+} * tak_state_p;
+
#define NUM_SHIFT 4
#define NUM_MASK (0xF << NUM_SHIFT) // 0b11110000
#define NUM_INC (0x1 << NUM_SHIFT) // 0b00010000
#define STONE_MASK 3 // 0b00000011
-#define STONE_AT(l) (celldat[(l)] & STONE_MASK)
-#define COUNT_AT(l) (celldat[(l)] >> NUM_SHIFT)
-#define THE_COORDS(col, row) ((col) + (row)*board_size)
-
-// ===================================================================
-// Variables
-// ===================================================================
-
-// NOTE: We only support one capstone per player and 5s or 6s games.
-
-extern enum WIN_TYPE won;
-extern uint8_t board_size;
-extern data_t celldat[36];
-extern colour_stack_t colours[36];
-extern enum COLOUR current_colour;
-extern uint8_t white_count, black_count, ply;
+#define STONE_AT(state, l) (state->celldat[(l)] & STONE_MASK)
+#define COUNT_AT(state, l) (state->celldat[(l)] >> NUM_SHIFT)
+#define THE_COORDS(board_size, col, row) ((col) + (row)*board_size)
// ===================================================================
// Methods
// ===================================================================
+tak_state_p new_tak_state(const uint8_t board_size);
+void free_tak_state(tak_state_p state);
+
// -------------------------------------------------------------------
// Game state
-void reset_state(const uint8_t new_board_size);
-void next_ply(void);
+void reset_state(tak_state_p state, const uint8_t new_board_size);
+void next_ply(tak_state_p state);
-enum ACT_RESULT try_place(const int8_t location, const enum COLOUR colour,
+enum ACT_RESULT try_place(tak_state_p state, const int8_t location,
+ const enum COLOUR colour,
const enum STONE_VARIANT stone);
-enum ACT_RESULT try_move(const int8_t location,
+enum ACT_RESULT try_move(tak_state_p state, const int8_t location,
const enum MOVE_DIRECTION direction,
const uint8_t steps, const uint8_t drops[5]);
-enum WIN_TYPE check_win(void);
+enum WIN_TYPE check_win(tak_state_p state);
// -------------------------------------------------------------------
// PTN related
-enum PTN_RESULT parse_place(char *in_ptn, uint8_t *out_location,
+enum PTN_RESULT parse_place(const uint8_t board_size, char *in_ptn,
+ uint8_t *out_location,
enum STONE_VARIANT *out_stone);
-enum PTN_RESULT parse_move(char *in_ptn, uint8_t *out_location,
+enum PTN_RESULT parse_move(const uint8_t board_size, char *in_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 in_location,
+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 in_location,
+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]);
@@ -110,5 +111,5 @@ void generate_move(const uint8_t in_location,
// -------------------------------------------------------------------
// Game driver
-enum ACT_RESULT do_ptn(char *ptn);
+enum ACT_RESULT do_ptn(tak_state_p state, char *ptn);
#endif