-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoBoard.h
177 lines (144 loc) · 4.17 KB
/
GoBoard.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef _GO_BOARD_H_
#define _GO_BOARD_H_
#include <utility>
#include <vector>
//#include <map>
#include <unordered_map>
#include <assert.h>
#include <array>
const int BOARD_SIZE = 9;
const int PLAYER_1 = 1;
const int PLAYER_2 = -1;
const float BOARD_HEIGHT = 1.0f;
const float BOARD_MIN = 0.0f;
const float BOARD_MAX = 1.0f;
const float BOARD_GRID_SPACING = 1/.9f;
const float PIECE_OFFSET = 0.005f;
const float PIECE_X_SCALE = 0.1f;
const float PIECE_Y_SCALE = 0.03f;
const float PIECE_Z_SCALE = 0.1f;
struct coord {
int x;
int y;
coord() : x(0), y(0) {}
coord(int _x, int _y) : x(_x), y(_y) {}
bool operator== (const coord other) const { return x == other.x && y == other.y; }
bool operator< (const coord other) const {
if (x == other.x) {
return y < other.y;
} else {
return x < other.x;
}
}
};
template<>
struct std::hash<coord> {
size_t operator()(const coord &val) const {
return val.x*BOARD_SIZE + val.y;
}
};
typedef std::pair<coord, int> PlacedPieceType;
struct coordList {
int numCoords;
int moveArray[BOARD_SIZE*BOARD_SIZE*2];
coordList() : numCoords(0) {}
int getX(int i) {
return moveArray[i*2];
}
int getY(int i) {
return moveArray[i*2+1];
}
coord getMove(int i) {
return coord(moveArray[i*2], moveArray[i*2+1]);
}
void putMove(int x, int y) {
//printf("%d\n", numMoves);
assert(numCoords*2+1 < BOARD_SIZE*BOARD_SIZE*2);
moveArray[numCoords*2] = x;
moveArray[numCoords*2+1] = y;
++numCoords;
}
void empty() {
numCoords = 0;
}
void emptyTo(int length) {
assert(length <= numCoords);
numCoords = length;
}
};
class GoBoard {
public:
GoBoard();
GoBoard(const GoBoard& other);
~GoBoard();
//Accessors
int getTurn(){return turn;}
int getPiece(int x, int y) const {return pieces[x][y];}
double getControl(int x, int y) const {return control[x][y];}
double getBoardStateForPlayer(int player);
int scoreFullBoard(int player);
bool getJustPassed(){return just_passed;}
//Modifiers
bool makeMove(int player, int x, int y) {
coordList dead;
dead.empty();
bool success = placePiece(player, x, y, dead);
removePieces(dead);
return success;
}
bool placePiece(int player, int x, int y, coordList &dead);
void passTurn(){turn *= -1; just_passed = true;}
void nextTurn(){turn *= -1; just_passed = false;}
//Misc
bool legalMove(int player, int x, int y);
//similar to above, but does not play in single-point eyes
void getAllReasonableMoves(int player, coordList &coordList);
//Modifiers
bool removePiece(int x, int y);
bool removePieces(coordList &toRemove);
bool addPieces(coordList &toAdd, int player);
void printControl();
void printBoard();
std::pair<coord, int> getSpeculativePiece() const;
bool applySpeculativePiece();
void placeSpeculativePiece(int player, int x, int y);
int getPlayerScore(int player);
void printPrevBoard();
void printPrev2Board();
void printScoreBoard();
int endOfGame();
//Helper
private:
void updateControlMap(int player, int x, int y);
void eraseControlMap();
void updateBoard();
void updatePrevBoard();
void fillLine(coord start, coord dir);
void calculateScores();
bool isKo();
bool isMyPiece(int player, int x, int y) { return pieces[x][y] == player; }
int getSurroundingDeadPieces(int player, coord last_move, coordList &dead_pieces);
int checkChainForDeadPieces(coord piece, int (&visited)[BOARD_SIZE][BOARD_SIZE], int already_visited, coordList &pieces);
int stillAlive(coord piece) {
int visited[BOARD_SIZE][BOARD_SIZE];
memset(visited, 0, sizeof(visited));
coordList pieces;
return checkChainForDeadPieces(piece, visited, 0, pieces);
}
int getPlayerAtCoord(coord place);
//Representation
int pieces[BOARD_SIZE][BOARD_SIZE];
int prev_pieces[BOARD_SIZE][BOARD_SIZE];
int prev2_pieces[BOARD_SIZE][BOARD_SIZE];
int score_board[BOARD_SIZE][BOARD_SIZE];
double control[BOARD_SIZE][BOARD_SIZE];
int nextPieceID;
int player1_score;
int player2_score;
int turn;
bool just_passed;
std::unordered_map<coord, int> placed_pieces;
std::unordered_map<int, int> placed_pieces_player;
std::pair<coord, int> speculativePiece;
};
#endif