-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameField.cpp
402 lines (359 loc) · 8.75 KB
/
GameField.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "GameField.h"
std::string act_to_str(int act)
{
if (act == 0) return "pass";
std::string str(2, '0');
str[0] = (act - 1) % WIDTH + 'A';
str[1] = WIDTH - (act - 1) / WIDTH + '0';
return str;
}
int str_to_act(std::string str)
{
if (str == "pass" || str == "PASS") return PASS;
return 1 + str[0] - 'A' + WIDTH * (WIDTH - str[1] + '0');
}
bool str_valid(std::string str)
{
if (str == "pass" || str == "PASS") return true;
if (str.size() != 2 || str[0] < 'A' || str[0] > 'A' + WIDTH || str[1] < '1' || str[1] > '0' + WIDTH) return false;
return true;
}
std::pair<int, int> act_to_xy(int act)
{
if (act == PASS) return { -1, -1 };
return { (act - 1) / WIDTH, (act - 1) % WIDTH };
}
int xy_to_act(int x, int y)
{
if (x == -1 && y == -1) return PASS;
return x * WIDTH + y + 1;
}
bool out_field(int pos, int index)
{
std::pair<int, int> xy = act_to_xy(pos);
int nx = xy.first + dx[index], ny = xy.second + dy[index];
if (nx < 0 || nx >= WIDTH || ny < 0 || ny >= WIDTH)
{
return true;
}
return false;
}
std::vector<double> get_noise(int num)
{
std::mt19937 g;
double k = 1;
double theta = 1;
double sm = 0;
auto res = std::vector<double>(num, 0);
for (int i = 0; i < num; i++)
{
std::gamma_distribution<> d(k, theta);
res[i] = d(g);
sm += res[i];
}
for (int i = 0; i < num; i++) res[i] /= sm;
return res;
}
GameField::GameField()
{
this->gameField = empty_field;
this->pass_cnt = 0;
this->current_color = black;
}
GameField::GameField(const GameField& field)
{
this->gameField = field.gameField;
this->pass_cnt = field.pass_cnt;
this->current_color = field.current_color;
this->past_situation_map = field.past_situation_map;
}
void GameField::clear()
{
this->gameField = empty_field;
}
void GameField::destroy()
{
this->gameField = empty_field;
this->pass_cnt = 0;
this->current_color = black;
this->past_situation_map = std::map<std::pair<board_type, int>, bool>();
}
void GameField::print()
{
std::cerr << "---------game field----------" << std::endl;
for (int i = 0; i < WIDTH; i++)
{
for (int j = 0; j < WIDTH; j++)
{
int pos = xy_to_act(i, j);
if (gameField[pos] == blank) std::cerr << '.';
else if (gameField[pos] == black) std::cerr << 'X';
else std::cerr << 'O';
}
std::cerr << std::endl;
}
std::cerr << "------------------------------" << std::endl;
}
std::vector<int> GameField::slice_group(int pos)
{
std::vector<bool> visit(MAX, false);
int color = gameField[pos];
std::queue<int> q;
std::vector<int> group;
visit[pos] = true;
q.push(pos);
group.emplace_back(pos);
while (q.size())
{
int here = q.front(); q.pop();
for (int d = 0; d < directions_cnt; d++)
{
if (out_field(here, d)) continue;
int tmp = here + directions[d];
if (visit[tmp] || gameField[tmp] != color) continue;
visit[tmp] = true;
q.push(tmp);
group.emplace_back(tmp);
}
}
return group;
}
int GameField::count_liberty(std::vector<int> group)
{
std::vector<bool> visit(MAX, false);
int cnt = 0;
for (auto l : group)
{
for (int d = 0; d < directions_cnt; d++)
{
if (out_field(l, d)) continue;
int tmp = l + directions[d];
if (visit[tmp]) continue;
if (gameField[tmp] == blank)
{
visit[tmp] = true;
cnt++;
}
}
}
return cnt;
}
void GameField::take(std::vector<int> group)
{
for (auto l : group) gameField[l] = blank;
}
std::pair<bool, GameField::board_type> GameField::settle(int act, int color, bool just_try)
{
int opposer = -color;
if (act == PASS) return { true, gameField };
int pos = act;
if (gameField[pos] != blank) return { false, gameField };
GameField nxt_field(*this);
nxt_field.gameField[pos] = color;
for (int d = 0; d < directions_cnt; d++)
{
if (out_field(pos, d)) continue;
int tmp = pos + directions[d];
if (nxt_field.gameField[tmp] == opposer)
{
std::vector<int> affects = nxt_field.slice_group(tmp);
int liberty = nxt_field.count_liberty(affects);
if (liberty < 1) nxt_field.take(affects);
}
}
std::vector<int> my_group = nxt_field.slice_group(pos);
int liberty = nxt_field.count_liberty(my_group);
if (liberty < 1) return { false, gameField };
else if (just_try) {
return { true, gameField };
}
else return { true, nxt_field.gameField };
}
void GameField::fill_blank(int pos)
{
std::queue<int> q;
q.push(pos);
gameField[pos] = mark;
while (q.size())
{
int here = q.front(); q.pop();
for (int d = 0; d < directions_cnt; d++)
{
if (out_field(here, d)) continue;
int tmp = here + directions[d];
if (gameField[tmp] != blank) continue;
gameField[tmp] = mark;
q.push(tmp);
}
}
}
std::pair<bool, bool> GameField::decide_blank_whose()
{
bool near_black = false, near_white = false;
for (int l = 1; l < ALL; l++)
{
if (near_black && near_white) break;
if (gameField[l] == mark)
{
for (int d = 0; d < directions_cnt; d++)
{
if (out_field(l, d)) continue;
int tmp = l + directions[d];
if (!near_black && gameField[tmp] == black)
near_black = true;
if (!near_white && gameField[tmp] == white)
near_white = true;
}
}
}
return { near_black, near_white };
}
double GameField::count_stones()
{
GameField copy_field(*this);
while (true)
{
int fst_blank = -1;
for (int i = 1; i < ALL; i++)
{
if (copy_field.gameField[i] == blank)
{
fst_blank = i;
break;
}
}
if (fst_blank == -1) break;
copy_field.fill_blank(fst_blank);
auto near_info = copy_field.decide_blank_whose();
bool near_black = near_info.first;
bool near_white = near_info.second;
if (near_black && !near_white)
{
for (auto& l : copy_field.gameField)
{
if (l == mark) l = black;
}
}
else if (!near_black && near_white)
{
for (auto& l : copy_field.gameField)
{
if (l == mark) l = white;
}
}
else
{
for (auto& l : copy_field.gameField)
{
if (l == mark) l = draw;
}
}
}
int black_territory = 0, draw_territory = 0;
for (auto i : copy_field.gameField)
{
if (i == black) black_territory++;
else if (i == draw) draw_territory++;
}
return black_territory + draw_territory / 2.0;
}
int GameField::referee()
{
if (pass_cnt >= 2)
{
auto black_score = count_stones();
if (black_score > THRESHOLD) return black_win;
return white_win;
}
return unfinished;
}
void GameField::play(int act, int color)
{
if (act == PASS)
{
pass_cnt++;
past_situation_map[std::make_pair(gameField, -color)] = true;
}
else
{
pass_cnt = 0;
gameField = settle(act, color).second;
past_situation_map[std::make_pair(gameField, -color)] = true;
}
this->current_color = -color;
}
void GameField::play(std::string act_str, int color)
{
int act = str_to_act(act_str);
play(act, color);
}
void GameField::play(int act)
{
if (act == PASS)
{
pass_cnt++;
past_situation_map[{gameField, -this->current_color}] = true;
}
else
{
pass_cnt = 0;
gameField = settle(act, this->current_color).second;
past_situation_map[{gameField, -this->current_color}] = true;
}
this->current_color = -this->current_color;
}
void GameField::play(std::string act_str)
{
int act = str_to_act(act_str);
play(act);
}
std::vector<int> GameField::valid_moves(int color)
{
std::vector<int> valid_moves = { PASS };
for (int pos = 1; pos < ALL; pos++)
{
auto move_info = settle(pos, color);
auto good_move = move_info.first;
auto nxt_field = move_info.second;
if (good_move && past_situation_map[{nxt_field, -color}] == false)
valid_moves.emplace_back(pos);
}
return valid_moves;
}
std::vector<int> GameField::valid_moves_mask(int color)
{
std::vector<int> mask(ALL, 0);
mask[PASS] = 1;
for (int pos = 1; pos < ALL; pos++)
{
auto move_info = settle(pos, color);
auto good_move = move_info.first;
auto nxt_field = move_info.second;
if (good_move && past_situation_map[{nxt_field, -color}] == false)
mask[pos] = 1;
}
return mask;
}
std::vector<double> GameField::get_gamefield_mat()
{
std::vector<double> input_vector(3 * WIDTH * WIDTH, 0);
auto get_linear_index = [](int a, int b, int c) { return a * WIDTH * WIDTH + b * WIDTH + c; };
if (current_color == white)
for (int x = 0; x < WIDTH; x++)
for (int y = 0; y < WIDTH; y++)
input_vector[get_linear_index(2, x, y)] = 1;
for (int i = 1; i < gameField.size(); i++)
{
auto pos_pair = act_to_xy(i);
int x = pos_pair.first, y = pos_pair.second;
if (gameField[i] == black)
{
input_vector[get_linear_index(0, x, y)] = 1;
}
else if (gameField[i] == white)
{
input_vector[get_linear_index(1, x, y)] = 1;
}
}
return input_vector;
}