forked from tseip/fourinarow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathninarow_board.h
354 lines (317 loc) · 10.8 KB
/
ninarow_board.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
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
#ifndef NINAROW_BOARD_H_INCLUDED
#define NINAROW_BOARD_H_INCLUDED
#include <bitset>
#include <cstdlib>
#include <sstream>
#include <stdexcept>
#include <string>
#include "ninarow_move.h"
#include "ninarow_pattern.h"
#include "player.h"
namespace NInARow {
/**
* Represents a board of N-in-a-row. In a game of N in a row, players alternate
* placing stones on a rectangular board. When a player places N stones in a
* row, either orthogonally or diagonally, that player wins.
*
* @tparam HEIGHT The height of the board.
* @tparam WIDTH The width of the board.
* @tparam N The number of stones in a row required to win.
*/
template <std::size_t HEIGHT, std::size_t WIDTH, std::size_t N>
class Board {
public:
using PatternT = Pattern<HEIGHT, WIDTH, N>;
using PatternHasherT = PatternHasher<PatternT>;
using MoveT = Move<HEIGHT, WIDTH, N>;
/**
* Getters for board dimensions.
*
* @{
*/
static constexpr std::size_t get_board_height() { return HEIGHT; }
static constexpr std::size_t get_board_width() { return WIDTH; }
static constexpr std::size_t get_board_size() { return HEIGHT * WIDTH; }
/**
* @}
*/
/**
* @return The maximum number of moves of any possible game played on this
* board.
*/
static constexpr std::size_t get_max_num_moves() { return get_board_size(); }
static_assert(get_board_size() >= 2 * N - 1,
"N in a row boards must be large enough to accommodate a "
"possible win, at least for the first player.");
private:
/**
* Represents the pieces currently placed on the game board for both players.
* Each player has their own pattern to represent their pieces over the game
* board.
*
* Note that this representation allows for invalid game states to be
* represented, if, e.g., both players have a piece in the same location.
* We detect and throw on invalid game states as the board is being played
* out.
*/
PatternT pieces[2];
public:
/**
* Default constructor.
*/
Board() = default;
/**
* Constructs a board with the given piece pattern.
*
* @param black_pieces The black pieces on the board.
* @param white_pieces The white pieces on the board.
*/
Board(const PatternT &black_pieces, const PatternT &white_pieces) {
const auto piece_diff =
black_pieces.positions.count() - white_pieces.positions.count();
if (black_pieces.count_overlap(white_pieces) != 0 || piece_diff > 1U ||
piece_diff < 0U) {
throw std::logic_error("Given board state is illegal!");
}
pieces[0] = black_pieces;
pieces[1] = white_pieces;
}
/**
* Clears the board.
*/
void reset() {
pieces[static_cast<size_t>(Player::Player1)].positions.reset();
pieces[static_cast<size_t>(Player::Player2)].positions.reset();
}
/**
* @param player The player to check.
*
* @return True if the given player has won, false otherwise.
*/
bool player_has_won(const Player player) const {
return pieces[static_cast<size_t>(player)].contains_win();
}
/**
* @return True if the game is drawn, false otherwise.
*/
bool game_is_drawn() const {
// The game is drawn if both players' pieces cover the game board.
return (pieces[static_cast<size_t>(Player::Player1)].positions |
pieces[static_cast<size_t>(Player::Player2)].positions)
.all() &&
!player_has_won(Player::Player1) && !player_has_won(Player::Player2);
}
/**
* @return True if the game is over - either one player has one, or the game
* is drawn.
*/
bool game_has_ended() const {
return player_has_won(Player::Player1) || player_has_won(Player::Player2) ||
game_is_drawn();
}
/**
* @return The number of pieces on the board from both players.
*/
std::size_t num_pieces() const {
return (pieces[static_cast<size_t>(Player::Player1)].positions |
pieces[static_cast<size_t>(Player::Player2)].positions)
.count();
}
Player active_player() const {
// Player 1 plays first, and play proceeds in alternating turns.
return (num_pieces() % 2) == 0 ? Player::Player1 : Player::Player2;
}
std::string to_string() const { return to_string(PatternT()); }
/**
* @param p A pattern to highlight on the board.
*
* @return A string representing the board with specific board positions
* highlighted.
*/
std::string to_string(const PatternT p) const {
std::stringstream stream;
stream << "+";
for (std::size_t col = 0; col < WIDTH; ++col) stream << "-";
stream << "+" << std::endl;
for (std::size_t row = 0; row < HEIGHT; ++row) {
stream << "|";
for (std::size_t col = 0; col < WIDTH; ++col) {
const size_t position = row * WIDTH + col;
if (p.positions.test(position))
stream << "#";
else if (pieces[static_cast<size_t>(Player::Player1)].positions.test(
position))
stream << "o";
else if (pieces[static_cast<size_t>(Player::Player2)].positions.test(
position))
stream << "x";
else
stream << " ";
}
stream << "|" << std::endl;
}
stream << "+";
for (unsigned int col = 0; col < WIDTH; col++) stream << "-";
stream << "+" << std::endl;
return stream.str();
}
/**
* @param player The player whose pieces are being requested.
*
* @return The pieces of the given player.
*/
PatternT get_pieces(const Player player) const {
return pieces[static_cast<size_t>(player)];
}
/**
* @return A pattern containing all empty spaces on the board.
*/
PatternT get_spaces() const {
return ~(pieces[static_cast<size_t>(Player::Player1)].positions |
pieces[static_cast<size_t>(Player::Player2)].positions);
}
/**
* @param p The pattern to check.
*
* @return The number of positions that the given player has covered in the
* given pattern.
*/
std::size_t count_pieces(const PatternT p, const Player player) const {
return pieces[static_cast<size_t>(player)].count_overlap(p);
}
/**
* @param p The pattern to check.
*
* @return The number of positions that neither player has covered in the
* given pattern.
*/
std::size_t count_spaces(const PatternT p) const {
return PatternT(pieces[static_cast<size_t>(Player::Player1)].positions |
pieces[static_cast<size_t>(Player::Player2)].positions)
.count_spaces(p);
}
/**
* @param pattern A pattern of pieces to cover.
* @param player The player who desires to cover the given pattern.
*
* @return A pattern representing the pieces needed to be played by the
* given player in order to cover the given pattern.
*/
Board::PatternT missing_pieces(const Board::PatternT &pattern,
Player player) const {
return Board::PatternT(
pattern.positions &
(~pieces[static_cast<std::size_t>(player)].positions));
}
/**
* @param p The pattern to check.
*
* @return True if the all of the given positions are unoccupied by pieces of
* either player.
*/
bool contains_spaces(const PatternT p) const {
return PatternT(pieces[static_cast<size_t>(Player::Player1)].positions |
pieces[static_cast<size_t>(Player::Player2)].positions)
.count_overlap(p) == 0;
}
/**
* @param m The move to check for.
*
* @return True if the board contains the given move.
*/
bool contains_move(MoveT m) const {
return pieces[static_cast<size_t>(m.player)].positions.test(
m.board_position);
}
/**
* @param position The position to check for a move.
*
* @return True if a move has been played by either player at the given
* position.
*/
bool contains_spaces(std::size_t position) const {
return !(
pieces[static_cast<size_t>(Player::Player1)].positions.test(position) ||
pieces[static_cast<size_t>(Player::Player2)].positions.test(position));
}
/**
* @param p The pattern to check.
* @param player The player to check.
*
* @return True if the given player's moveset contains the given pattern.
*/
bool contains(PatternT p, Player player) const {
return pieces[static_cast<size_t>(player)].contains(p);
}
/**
* @param m The move to add to the board.
*/
void add(const MoveT m) {
if (m.player != active_player()) {
throw std::logic_error("Supplied move is not legal on the given board!");
}
if (!contains_spaces(m.board_position))
throw std::invalid_argument("Piece already exists at position " +
std::to_string(m.board_position));
pieces[static_cast<size_t>(m.player)].positions.set(m.board_position);
}
/**
* @param m The move to remove from the board.
*/
void remove(const MoveT m) {
if (m.player != get_other_player(active_player())) {
throw std::logic_error(
"Removing given move would lead to an illegal board state!");
}
if (!pieces[static_cast<size_t>(m.player)].positions.test(m.board_position))
throw std::invalid_argument(
"Piece does not exist at position " +
std::to_string(m.board_position) + " for player " +
std::to_string(static_cast<size_t>(m.player) + 1U));
pieces[static_cast<size_t>(m.player)].positions.reset(m.board_position);
}
/**
* Produces a new board with the given move added.
*
* @param m The move to add.
*
* @return The new board.
*/
Board operator+(const MoveT m) const {
Board temp(*this);
temp.add(m);
return temp;
}
/**
* Produces a new board with the given move removed.
*
* @param m The move to add.
*
* @return The new board.
*/
Board operator-(const MoveT m) const {
Board temp(*this);
temp.remove(m);
return temp;
}
/**
* @param b A different board.
*
* @return True if the given board is equivalent to this board. Note that the
* histories of the two boards may be different.
*/
bool operator==(const Board &b) const {
return ((pieces[static_cast<size_t>(Player::Player2)] ==
b.pieces[static_cast<size_t>(Player::Player2)]) &&
(pieces[static_cast<size_t>(Player::Player1)] ==
b.pieces[static_cast<size_t>(Player::Player1)]));
}
/**
* @param b A different board.
*
* @return True if the given board is not equivalent to this board.
*/
bool operator!=(const Board &b) const { return !(*this == b); }
};
} // namespace NInARow
#endif // NINAROW_BOARD_H_INCLUDED