summaryrefslogtreecommitdiff
path: root/include/tak.h
blob: 7d96b9a568980f52d2a2756e0c813b5779217103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdint.h>

enum E_RESULT {
  ACT_OK, ACT_ILLEGAL, ACT_OVERFLOW,
  PTN_VALID, PTN_INVALID,
  WIN_DRAW, WIN_FLAT_WHITE, WIN_FLAT_BLACK,
  WIN_ROAD_WHITE, WIN_ROAD_BLACK, WIN_DRAGON };

// -------------------------------------------------------------------
// State management

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 };

typedef uint8_t data_t;
typedef uint16_t colour_stack_t;

#define NUM_SHIFT  4
#define STONE_MASK 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)

uint8_t board_size;
data_t celldat[36];
colour_stack_t colours[36];
enum COLOUR current_colour;
uint8_t white_flats, black_flats, white_caps, black_caps, ply;

void reset_state(const uint8_t new_board_size);
void next_ply(void);

enum E_RESULT
try_place(const int8_t location, const enum COLOUR colour,
          const enum STONE_VARIANT stone);

enum E_RESULT
try_move(const int8_t location, const enum MOVE_DIRECTION direction,
         const uint8_t steps, const uint8_t drops[5]);

enum E_RESULT
check_win(void);

// -------------------------------------------------------------------
// PTN related

enum E_RESULT
parse_place(const uint8_t board_size, char *ptn,
            uint8_t *out_location, enum STONE_VARIANT *out_stone);

enum E_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]);

// -------------------------------------------------------------------
// Game driver

enum E_RESULT
do_ptn(char *ptn);