-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.h
61 lines (53 loc) · 1.67 KB
/
board.h
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
#ifndef CITA_BOARD_h_
#define CITA_BOARD_h_
#include <array>
namespace cita {
constexpr short MAX_SIZE = 9;
constexpr int MAX_ARR_SIZE = (MAX_SIZE + 1) * (MAX_SIZE + 2) + 1;
constexpr int MAX_GAME_SIZE = MAX_SIZE * MAX_SIZE;
template <typename T>
using BoardArr = std::array<T, MAX_ARR_SIZE>;
template <typename T>
using BoardGameArr = std::array<T, MAX_GAME_SIZE>;
enum POINT {
POINT_EMPTY = 0b00,
POINT_BLACK = 0b01,
POINT_WHITE = 0b10,
POINT_WALL = 0b11
};
enum STATE {
STATE_ALLOW = 0b00,
STATE_FORBID_BLACK = 0b01,
STATE_FORBID_WHITE = 0b10,
STATE_FORBID = 0b11
};
using Position = short;
inline POINT getOpp(const POINT &point) {
return point == POINT_WHITE ? POINT_BLACK : POINT_WHITE;
}
inline Position getPos(int x, int y) {
return (x + 1) + (y + 1) * (MAX_SIZE + 1);
}
inline int getX(Position pos) { return pos % (MAX_SIZE + 1) - 1; }
inline int getY(Position pos) { return pos / (MAX_SIZE + 1) - 1; }
class Board {
public:
Board();
bool Place(const Position &pos, POINT stoneType);
BoardGameArr<Position> GetValidPlace(POINT stoneType, int &len) const;
int GetValidPlaceCount(POINT stoneType) const;
int GetBowlCount(POINT stoneType) const;
private:
BoardArr<POINT> board_{};
BoardArr<STATE> state_{};
BoardArr<int> liberties_{};
BoardArr<Position> chain_next_{}; // Circular chain, in order to replace the
// recursive function
BoardArr<Position> chain_head_{};
BoardArr<bool> searched_{};
void chain_affected_pos(Position pos, BoardArr<bool> &searched);
void chain_count_and_change_liberties(Position pos, BoardArr<bool> &counted);
void update_empty(Position pos);
};
} // namespace cita
#endif