-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
303 lines (270 loc) · 6.88 KB
/
board.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
#include "board.h"
Board::Board()
: turn(BLACK), scoreB(0), scoreW(0), last_move(make_pair(-1, -1)), moves(0),
eB(50.0), eW(50.0) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
board[i][j] = EMPTY;
}
}
}
Board::~Board() {}
Stone Board::get_stone(int x, int y) { return board[x][y]; }
void Board::set_stone(int x, int y, Stone s) { board[x][y] = s; }
int Board::get_score(Stone s) { return (s == BLACK) ? scoreB : scoreW; }
void Board::set_score(Stone s, int score) {
(s == BLACK) ? scoreB = score : scoreW = score;
}
void Board::set_last_move(int x, int y) { last_move = make_pair(x, y); }
Coords Board::get_last_move() { return last_move; }
bool Board::is_last_move(int x, int y) {
return get_last_move() == make_pair(x, y);
}
void Board::toggle_turn() {
last = turn;
turn = (turn == BLACK) ? WHITE : BLACK;
}
Stone Board::last_turn() { return last; }
Stone Board::whose_turn() { return turn; }
string Board::get_nickname(Stone s) {
switch ((s == BLACK) ? playerB : playerW) {
case HUMAN:
return "Human";
case SOFIAI:
return "Sofiai";
case MARIAI:
return "Mariai";
default:
return "Unknown";
}
}
Player Board::get_player(Stone s) { return (s == BLACK) ? playerB : playerW; }
void Board::set_player(Player pB, Player pW) {
playerB = pB;
playerW = pW;
}
int Board::make_move(int x, int y) {
if (board[x][y] != EMPTY) {
return 1;
} else {
if (check_3_3(x, y))
return 2;
moves++;
set_stone(x, y, turn);
set_last_move(x, y);
if (BCF)
crunch(x, y);
return 0;
}
}
bool Board::in_range(int x, int y) {
return x >= 0 && x < N && y >= 0 && y < N;
}
bool Board::check_tied() { return moves == N * N - 2; }
bool Board::check_quit(int x, int y) {
if (check_tied())
return true;
if (scoreB >= WP)
return true;
if (scoreW >= WP)
return true;
if (check_nseq(x, y, 0, GOAL))
return true;
if (check_nseq(x, y, 1, GOAL))
return true;
if (check_nseq(x, y, 2, GOAL))
return true;
if (check_nseq(x, y, 3, GOAL))
return true;
return false;
}
bool Board::check_3_3(int x, int y) {
int count = 0;
if (check_nseq_open(x, y, 0, 3))
count++;
if (check_nseq_open(x, y, 1, 3))
count++;
if (check_nseq_open(x, y, 2, 3))
count++;
if (count == 0)
return false;
if (check_nseq_open(x, y, 3, 3))
count++;
return (count > 1) ? true : false;
}
bool Board::check_nseq(int x, int y, int q, int n) {
// q in Z/4Z = {0, 1, 2, 3}
// meaning the direction of two on-axes and two off-axes
auto check = [&](int dx1, int dy1, int dx2, int dy2) {
return count_seq(x, y, dx1, dy1) + count_seq(x, y, dx2, dy2) + 1 == n;
};
switch (q) {
case 0:
return check(1, 0, -1, 0);
case 1:
return check(1, 1, -1, -1);
case 2:
return check(0, 1, 0, -1);
case 3:
return check(1, -1, -1, 1);
default:
return false;
}
}
bool Board::check_nseq_open(int x, int y, int q, int n) {
auto check = [&](int dx1, int dy1, int dx2, int dy2) {
int f = count_seq(x, y, dx1, dy1, true);
if (f == -1)
return false;
int s = count_seq(x, y, dx2, dy2, true);
return (s == -1) ? false : f + 1 + s == n;
};
switch (q) {
case 0:
return check(1, 0, -1, 0);
case 1:
return check(1, 1, -1, -1);
case 2:
return check(0, 1, 0, -1);
case 3:
return check(1, -1, -1, 1);
default:
return false;
}
}
int Board::count_seq(int x, int y, int dx, int dy, bool open) {
int count = 0;
if (open) {
while (1) {
x += dx;
y += dy;
if (!in_range(x, y))
return -1;
if (board[x][y] == EMPTY)
return count;
if (board[x][y] != turn)
return -1;
count++;
}
} else {
while (1) {
x += dx;
y += dy;
if (!in_range(x, y) || board[x][y] != turn)
break;
count++;
}
return count;
}
}
void Board::crunch(int x, int y) {
auto crunch_if_any = [&](int dx, int dy) {
if (!in_range(x + 3 * dx, y + 3 * dy))
return;
if (board[x + 3 * dx][y + 3 * dy] == turn &&
board[x + 2 * dx][y + 2 * dy] == last &&
board[x + 1 * dx][y + 1 * dy] == last) {
board[x + 2 * dx][y + 2 * dy] = EMPTY;
board[x + 1 * dx][y + 1 * dy] = EMPTY;
(turn == BLACK) ? scoreB += 2 : scoreW += 2;
}
};
crunch_if_any(-1, 0);
crunch_if_any(1, 0);
crunch_if_any(0, -1);
crunch_if_any(0, 1);
crunch_if_any(1, 1);
crunch_if_any(-1, 1);
crunch_if_any(1, -1);
crunch_if_any(-1, -1);
}
void Board::read_board(string file) {
ifstream fin(file);
if (!fin.good())
return;
vector<string> data;
vector<string> grid;
string token;
while (getline(fin, token, ':')) {
data.emplace_back(token);
}
fin.close();
last_move = make_pair(stoi(data[0]), stoi(data[1]));
moves = stoi(data[2]);
turn = (stoi(data[3]) == 1) ? BLACK : WHITE;
last = (turn == BLACK) ? WHITE : BLACK;
scoreB = stoi(data[4]);
scoreW = stoi(data[5]);
eB = stoi(data[6]) / 10.;
eW = stoi(data[7]) / 10.;
grid = vector<string>(data.begin() + 8, data.end());
for (int i = 0; i < N * N; i++) {
int x = i % N;
int y = i / N;
Stone stone = (stoi(grid[i]) == 1) ? BLACK
: (stoi(grid[i]) == -1) ? WHITE
: EMPTY;
board[y][x] = stone;
}
}
void Board::write_board(string file) {
ofstream fout(file);
int iturn = (turn == BLACK) ? -1 : 1;
fout << last_move.first << ":";
fout << last_move.second << ":";
fout << moves << ":";
fout << iturn << ":";
fout << scoreB << ":";
fout << scoreW << ":";
fout << int(eB * 10) << ":";
fout << int(1000 - (eB * 10)) << ":";
for (int j = 0; j < N; j++) {
for (int i = 0; i < N; i++) {
int stone = (board[j][i] == BLACK) ? 1 : (board[j][i] == WHITE) ? -1 : 0;
if ((i == N - 1) && (j == N - 1))
fout << stone;
else
fout << stone << ":";
}
}
fout.close();
}
void Board::print_board(VecCoords candy, bool print_candy) {
if (print_candy) {
for (auto q : candy) {
int i, j;
tie(i, j) = q;
board[i][j] = CANDY;
}
}
cout << endl;
cout << setw(4) << " ";
for (int i = 0; i < N; i++) {
cout << i % 10 << " ";
}
cout << endl;
for (int i = 0; i < N; i++) {
cout << setw(3) << i % 10 << " ";
for (int j = 0; j < N; j++) {
if (board[i][j] == CANDY) {
cout << TC_GREEN << "-" << TC_RESET << " ";
} else if (is_last_move(i, j)) {
if (board[i][j] == BLACK)
cout << TC_GREEN << "x" << TC_RESET << " ";
if (board[i][j] == WHITE)
cout << TC_GREEN << "o" << TC_RESET << " ";
} else {
if (board[i][j] == BLACK) {
cout << TC_BLUE << "x" << TC_RESET << " ";
} else if (board[i][j] == WHITE) {
cout << TC_RED << "o" << TC_RESET << " ";
} else {
cout << "\u2219"
<< " ";
}
}
}
cout << endl;
}
cout << endl;
}