-
Notifications
You must be signed in to change notification settings - Fork 16
/
GameState.cpp
317 lines (248 loc) · 8 KB
/
GameState.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <assert.h>
#include <cctype>
#include <string>
#include <sstream>
#include "config.h"
#include "KoState.h"
#include "GameState.h"
#include "FullBoard.h"
#include "UCTSearch.h"
#include "Zobrist.h"
#include "Random.h"
#include "Utils.h"
void GameState::init_game(int size, float komi) {
KoState::init_game(size, komi);
game_history.clear();
game_history.push_back(*this);
m_timecontrol.set_boardsize(board.get_boardsize());
m_timecontrol.reset_clocks();
return;
};
void GameState::reset_game() {
KoState::reset_game();
game_history.clear();
game_history.push_back(*this);
m_timecontrol.reset_clocks();
}
bool GameState::forward_move(void) {
if (game_history.size() > m_movenum + 1) {
KoState & f = *this;
m_movenum++;
f = game_history[m_movenum];
return true;
} else {
return false;
}
}
bool GameState::undo_move(void) {
if (m_movenum > 0) {
m_movenum--;
// don't actually delete it!
//game_history.pop_back();
// this is not so nice, but it should work
KoState & f = *this;
f = game_history[m_movenum];
// This also restores hashes as they're part of state
return true;
} else {
return false;
}
}
void GameState::rewind(void) {
KoState & f = *this;
f = game_history[0];
m_movenum = 0;
}
void GameState::play_move(int vertex) {
play_move(board.get_to_move(), vertex);
}
void GameState::play_pass() {
play_move(get_to_move(), FastBoard::PASS);
}
void GameState::play_move(int color, int vertex) {
if (vertex != FastBoard::PASS && vertex != FastBoard::RESIGN) {
KoState::play_move(color, vertex);
} else {
KoState::play_pass();
if (vertex == FastBoard::RESIGN) {
m_lastmove = vertex;
m_last_was_capture = false;
}
}
// cut off any leftover moves from navigating
game_history.resize(m_movenum);
game_history.push_back(*this);
}
bool GameState::play_textmove(std::string color, std::string vertex) {
int who;
int column, row;
int boardsize = board.get_boardsize();
if (color == "w" || color == "white") {
who = FullBoard::WHITE;
} else if (color == "b" || color == "black") {
who = FullBoard::BLACK;
} else return false;
if (vertex.size() < 2) return 0;
if (!std::isalpha(vertex[0])) return 0;
if (!std::isdigit(vertex[1])) return 0;
if (vertex[0] == 'i') return 0;
if (vertex[0] >= 'A' && vertex[0] <= 'Z') {
if (vertex[0] < 'I') {
column = 25 + vertex[0] - 'A';
} else {
column = 25 + (vertex[0] - 'A')-1;
}
} else {
if (vertex[0] < 'i') {
column = vertex[0] - 'a';
} else {
column = (vertex[0] - 'a')-1;
}
}
std::string rowstring(vertex);
rowstring.erase(0, 1);
std::istringstream parsestream(rowstring);
parsestream >> row;
row--;
if (row >= boardsize) return false;
if (column >= boardsize) return false;
int move = board.get_vertex(column, row);
play_move(who, move);
return true;
}
void GameState::stop_clock(int color) {
m_timecontrol.stop(color);
}
void GameState::start_clock(int color) {
m_timecontrol.start(color);
}
void GameState::display_state() {
FastState::display_state();
m_timecontrol.display_times();
}
TimeControl& GameState::get_timecontrol() {
return m_timecontrol;
}
void GameState::set_timecontrol(int maintime, int byotime,
int byostones, int byoperiods) {
TimeControl timecontrol(board.get_boardsize(), maintime, byotime,
byostones, byoperiods);
m_timecontrol = timecontrol;
}
void GameState::set_timecontrol(TimeControl tmc) {
m_timecontrol = tmc;
}
void GameState::adjust_time(int color, int time, int stones) {
m_timecontrol.adjust_time(color, time, stones);
}
void GameState::anchor_game_history(void) {
// handicap moves don't count in game history
m_movenum = 0;
game_history.clear();
game_history.push_back(*this);
}
void GameState::trim_game_history(int lastmove) {
m_movenum = lastmove - 1;
game_history.resize(lastmove);
}
bool GameState::set_fixed_handicap(int handicap) {
if (!valid_handicap(handicap)) {
return false;
}
int board_size = board.get_boardsize();
int high = board_size >= 13 ? 3 : 2;
int mid = board_size / 2;
int low = board_size - 1 - high;
if (handicap >= 2) {
play_move(FastBoard::BLACK, board.get_vertex(low, low));
play_move(FastBoard::BLACK, board.get_vertex(high, high));
}
if (handicap >= 3) {
play_move(FastBoard::BLACK, board.get_vertex(high, low));
}
if (handicap >= 4) {
play_move(FastBoard::BLACK, board.get_vertex(low, high));
}
if (handicap >= 5 && handicap % 2 == 1) {
play_move(FastBoard::BLACK, board.get_vertex(mid, mid));
}
if (handicap >= 6) {
play_move(FastBoard::BLACK, board.get_vertex(low, mid));
play_move(FastBoard::BLACK, board.get_vertex(high, mid));
}
if (handicap >= 8) {
play_move(FastBoard::BLACK, board.get_vertex(mid, low));
play_move(FastBoard::BLACK, board.get_vertex(mid, high));
}
board.set_to_move(FastBoard::WHITE);
anchor_game_history();
set_handicap(handicap);
return true;
}
int GameState::set_fixed_handicap_2(int handicap) {
int board_size = board.get_boardsize();
int low = board_size >= 13 ? 3 : 2;
int mid = board_size / 2;
int high = board_size - 1 - low;
int interval = (high - mid) / 2;
int placed = 0;
while (interval >= 3) {
for (int i = low; i <= high; i += interval) {
for (int j = low; j <= high; j += interval) {
if (placed >= handicap) return placed;
if (board.get_square(i-1, j-1) != FastBoard::EMPTY) continue;
if (board.get_square(i-1, j) != FastBoard::EMPTY) continue;
if (board.get_square(i-1, j+1) != FastBoard::EMPTY) continue;
if (board.get_square(i, j-1) != FastBoard::EMPTY) continue;
if (board.get_square(i, j) != FastBoard::EMPTY) continue;
if (board.get_square(i, j+1) != FastBoard::EMPTY) continue;
if (board.get_square(i+1, j-1) != FastBoard::EMPTY) continue;
if (board.get_square(i+1, j) != FastBoard::EMPTY) continue;
if (board.get_square(i+1, j+1) != FastBoard::EMPTY) continue;
play_move(FastBoard::BLACK, board.get_vertex(i, j));
placed++;
}
}
interval = interval / 2;
}
return placed;
}
bool GameState::valid_handicap(int handicap) {
int board_size = board.get_boardsize();
if (handicap < 2 || handicap > 9) {
return false;
}
if (board_size % 2 == 0 && handicap > 4) {
return false;
}
if (board_size == 7 && handicap > 4) {
return false;
}
if (board_size < 7 && handicap > 0) {
return false;
}
return true;
}
void GameState::place_free_handicap(int stones) {
int limit = board.get_boardsize() * board.get_boardsize();
if (stones > limit / 2) {
stones = limit / 2;
}
int orgstones = stones;
int fixplace = std::min(9, stones);
set_fixed_handicap(fixplace);
stones -= fixplace;
stones -= set_fixed_handicap_2(stones);
for (int i = 0; i < stones; i++) {
std::unique_ptr<UCTSearch> search(new UCTSearch(*this));
int move = search->think(FastBoard::BLACK, UCTSearch::NOPASS);
play_move(FastBoard::BLACK, move);
}
if (orgstones) {
board.set_to_move(FastBoard::WHITE);
} else {
board.set_to_move(FastBoard::BLACK);
}
anchor_game_history();
set_handicap(orgstones);
}