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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/*
This file is part of ct.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with ct. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef TAK_H
#define TAK_H
#include <stdint.h>
// ===================================================================
// Types
// ===================================================================
enum ACT_RESULT {
ACT_OK,
ACT_ILLEGAL,
ACT_OVERFLOW,
ACT_INVALID_PTN,
GAME_END
};
enum PTN_RESULT { PTN_OK, PTN_INVALID };
enum TPS_RESULT { TPS_OK, TPS_INVALID };
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 };
enum WIN_TYPE {
WIN_DRAW,
WIN_FLAT_WHITE,
WIN_FLAT_BLACK,
WIN_ROAD_WHITE,
WIN_ROAD_BLACK
};
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(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(tak_state_p state, const uint8_t new_board_size);
void next_ply(tak_state_p state);
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(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(tak_state_p state);
// -------------------------------------------------------------------
// PTN related
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(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 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 ACT_RESULT do_ptn(tak_state_p state, char *ptn);
#endif
|