-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexGraph.cpp
305 lines (270 loc) · 6.71 KB
/
hexGraph.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
#include <vector>
#include <queue>
#include <iostream>
#include "hexGraph.h"
#include "board.h"
static inline int coord2index(int row, int col, int side)
{
return row * side + col;
}
bool HexGraph ::whiteWon(const Board &board)
{
/*
* The board is represented as a graph where the tiles are vertices and
* there is an edge between a tile and its neighbours.
* 4 virtual nodes are included on the NORTH, SOUTH, EAST, and WEST
* of the board. NORTH and SOUTH are always WHITE, while EAST and WEST
* are always BLACK.
* A path connecting NORTH->SOUTH => white win
* A path connecting EAST->WEST => black win
*/
int ntiles = board.ntiles();
int neighbours_size = std::max(6, board.side());
std::vector<int> neighbours(neighbours_size);
std::vector<bool> visited(ntiles + 4, false);
std::queue<int> q;
int north = ntiles;
int south = ntiles + 1;
visited[north] = true;
q.push(north);
while (!q.empty())
{
int s = q.front();
q.pop();
//std::vector<int> neighbours = neighbourNodes(s, board.side());
neighbourNodesFast(s, board.side(), neighbours);
for (auto n : neighbours)
{
if (n < 0)
continue;
bool connected;
if (n < ntiles)
connected = (board[n] == TileColour::WHITE);
else
connected = (n == north) || (n == south);
if (connected)
{
if (!visited[n])
{
visited[n] = true;
q.push(n);
}
}
}
}
// if south node is visited then white wins
return visited[south];
}
bool HexGraph ::blackWon(const Board &board)
{
/*
* The board is represented as a graph where the tiles are vertices and
* there is an edge between a tile and its neighbours.
* 4 virtual nodes are included on the NORTH, SOUTH, EAST, and WEST
* of the board. NORTH and SOUTH are always WHITE, while EAST and WEST
* are always BLACK.
* A path connecting NORTH->SOUTH => white win
* A path connecting WEST->EAST => black win
*/
int ntiles = board.ntiles();
int neighbours_size = std::max(6, board.side());
std::vector<int> neighbours(neighbours_size);
std::vector<bool> visited(ntiles + 4, false);
std::queue<int> q;
int west = ntiles + 2;
int east = ntiles + 3;
visited[west] = true;
q.push(west);
while (!q.empty())
{
int s = q.front();
q.pop();
//std::vector<int> neighbours = neighbourNodes(s, board.side());
neighbourNodesFast(s, board.side(), neighbours);
for (auto n : neighbours)
{
if (n < 0)
continue;
bool connected;
if (n < ntiles)
connected = (board[n] == TileColour::BLACK);
else
connected = (n == west) || (n == east);
if (connected)
{
if (!visited[n])
{
visited[n] = true;
q.push(n);
}
}
}
}
// if east node is visited then black wins
return visited[east];
}
TileColour HexGraph ::fullBoardWinner(const Board &board)
{
// for special case of a full board only need to evaluate if one of the players won
// since they can only have opposite values
// evaluate if whiteWon since it has better memory access pattern than blackWon
bool white_won = whiteWon(board);
if (white_won)
return TileColour::WHITE;
else
return TileColour::BLACK;
}
void HexGraph::neighbourNodesFast(int t, int side, std::vector<int> &neighbours)
{
int ntiles = side * side;
// init neighbours vector to null value
for (auto &neighbour : neighbours)
neighbour = -1;
int nidx = 0;
// virtual nodes
if (t == ntiles)
{
//north
for (int tile = 0; tile < side; ++tile)
neighbours[nidx++] = tile;
}
else if (t == ntiles + 1)
{
//south
for (int tile = ntiles - side - 1; tile < ntiles; ++tile)
neighbours[nidx++] = tile;
}
else if (t == ntiles + 2)
{
// west
for (int tile = 0; tile < side * side; tile += side)
neighbours[nidx++] = tile;
}
else if (t == ntiles + 3)
{
// east
for (int tile = side - 1; tile < side * side; tile += side)
neighbours[nidx++] = tile;
}
else
{
// real nodes
// 2d coordinates
int row = t / side;
int col = t % side;
if (row == 0)
{
neighbours[nidx++] = ntiles;
neighbours[nidx++] = coord2index(row + 1, col, side);
}
else if (row == (side - 1))
{
neighbours[nidx++] = ntiles + 1;
neighbours[nidx++] = coord2index(row - 1, col, side);
}
else
{
neighbours[nidx++] = coord2index(row + 1, col, side);
neighbours[nidx++] = coord2index(row - 1, col, side);
}
if (col == 0)
{
neighbours[nidx++] = ntiles + 2;
neighbours[nidx++] = coord2index(row, col + 1, side);
if (row > 0)
neighbours[nidx++] = coord2index(row - 1, col + 1, side);
}
else if (col == (side - 1))
{
neighbours[nidx++] = ntiles + 3;
neighbours[nidx++] = coord2index(row, col - 1, side);
if (row < (side - 1))
neighbours[nidx++] = coord2index(row + 1, col - 1, side);
}
else
{
neighbours[nidx++] = coord2index(row, col + 1, side);
neighbours[nidx++] = coord2index(row, col - 1, side);
if (row > 0)
neighbours[nidx++] = coord2index(row - 1, col + 1, side);
if (row < (side - 1))
neighbours[nidx++] = coord2index(row + 1, col - 1, side);
}
}
}
std::vector<int> HexGraph ::neighbourNodes(int t, int side)
{
int ntiles = side * side;
std::vector<int> neighbours;
// virtual nodes
if (t == ntiles)
{
//north
for (int i = 0; i < side; ++i)
neighbours.push_back(i);
}
else if (t == ntiles + 1)
{
//south
for (int i = ntiles - side - 1; i < ntiles; ++i)
neighbours.push_back(i);
}
else if (t == ntiles + 2)
{
// west
for (int i = 0; i < side * side; i += side)
neighbours.push_back(i);
}
else if (t == ntiles + 3)
{
// east
for (int i = side - 1; i < side * side; i += side)
neighbours.push_back(i);
}
else
{
// real nodes
// 2d coordinates
int row = t / side;
int col = t % side;
if (row == 0)
{
neighbours.push_back(ntiles);
neighbours.push_back(coord2index(row + 1, col, side));
}
else if (row == (side - 1))
{
neighbours.push_back(ntiles + 1);
neighbours.push_back(coord2index(row - 1, col, side));
}
else
{
neighbours.push_back(coord2index(row + 1, col, side));
neighbours.push_back(coord2index(row - 1, col, side));
}
if (col == 0)
{
neighbours.push_back(ntiles + 2);
neighbours.push_back(coord2index(row, col + 1, side));
if (row > 0)
neighbours.push_back(coord2index(row - 1, col + 1, side));
}
else if (col == (side - 1))
{
neighbours.push_back(ntiles + 3);
neighbours.push_back(coord2index(row, col - 1, side));
if (row < (side - 1))
neighbours.push_back(coord2index(row + 1, col - 1, side));
}
else
{
neighbours.push_back(coord2index(row, col + 1, side));
neighbours.push_back(coord2index(row, col - 1, side));
if (row > 0)
neighbours.push_back(coord2index(row - 1, col + 1, side));
if (row < (side - 1))
neighbours.push_back(coord2index(row + 1, col - 1, side));
}
}
return neighbours;
}