-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreversiboard.cpp
367 lines (329 loc) · 9.15 KB
/
reversiboard.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
#include "reversiboard.h"
#include <algorithm>
#include <cassert>
#include <stdexcept>
#include <iterator>
#include <sstream>
#include "textcanvas.h"
ribi::reversi::Board::Board(const int size)
: m_board(size,std::vector<Square>(size,Square::empty))
{
assert(size > 0);
assert(size == this->GetSize());
assert(m_board.size() == m_board[0].size() );
const int x = size / 2 - 1;
const int y = size / 2 - 1;
Set(x ,y ,Square::player1);
Set(x+1,y ,Square::player2);
Set(x ,y+1,Square::player2);
Set(x+1,y+1,Square::player1);
}
/*
ribi::reversi::Board::Board(const Board& other)
: m_board(other.m_board)
{
}
ribi::reversi::Board& ribi::reversi::Board::operator=(const Board& other)
{
m_board = other.m_board;
return *this;
}
*/
bool ribi::reversi::Board::CanDoMove(const int x, const int y, const Player player) const noexcept
{
assert(x >= 0);
assert(y >= 0);
assert(x < this->GetSize());
assert(y < this->GetSize());
if (Get(x,y) != Square::empty) return false;
for (const std::pair<int,int> d: CreateDeltas())
{
int cur_x = x + d.first;
int cur_y = y + d.second;
//Must be a valid direction
if (cur_x < 0 || cur_y < 0 || cur_x >= GetSize() || cur_y >= GetSize()) continue;
//Adjacent square must be of opponent
if (Get(cur_x,cur_y) != PlayerToSquare(GetOtherPlayer(player))) continue;
assert(Get(cur_x,cur_y) != Square::empty);
//Follow the trail until
// - player is found -> Can do move
// - empty is found -> Cannot do move
// - other player is found -> Search on
while (1)
{
cur_x += d.first;
cur_y += d.second;
//Must be a valid direction
if (cur_x < 0 || cur_y < 0 || cur_x >= GetSize() || cur_y >= GetSize()) break;
if (Get(cur_x,cur_y) == Square::empty) break;
if (Get(cur_x,cur_y) == PlayerToSquare(player))
{
return true;
}
}
}
return false;
}
int ribi::reversi::Board::Count(const Square square) const noexcept
{
const int size = GetSize();
int sum = 0;
for (int y=0; y!=size; ++y)
{
for (int x=0; x!=size; ++x)
{
if (Get(x,y) == square) ++sum;
}
}
return sum;
}
const std::vector<std::pair<int,int>> ribi::reversi::Board::CreateDeltas() noexcept
{
std::vector<std::pair<int,int>> v;
for (int dx = -1; dx != 2; ++dx)
{
for (int dy = -1; dy != 2; ++dy)
{
if (dx != 0 || dy != 0) v.push_back( std::make_pair(dx,dy));
}
}
assert(v.size() == 8);
return v;
}
void ribi::reversi::Board::DoMove(const int x, const int y, const Player player) noexcept
{
assert(CanDoMove(x,y,player));
#ifndef NDEBUG
const Board before(*this);
#endif
//Collect the deltas tomodify the color
std::vector<std::pair<int,int>> v;
for (const std::pair<int,int> d: CreateDeltas())
{
int cur_x = x + d.first;
int cur_y = y + d.second;
//Must be a valid direction
if (cur_x < 0 || cur_y < 0 || cur_x >= GetSize() || cur_y >= GetSize()) continue;
//Adjacent square must be of opponent
if (Get(cur_x,cur_y) != PlayerToSquare(GetOtherPlayer(player))) continue;
assert(Get(cur_x,cur_y) != Square::empty);
//Follow the trail until
// - player is found -> Can do move
// - empty is found -> Cannot do move
// - other player is found -> Search on
while (1)
{
cur_x += d.first;
cur_y += d.second;
//Must be a valid direction
if (cur_x < 0 || cur_y < 0 || cur_x >= GetSize() || cur_y >= GetSize()) break;
if (Get(cur_x,cur_y) == Square::empty) break;
if (Get(cur_x,cur_y) == PlayerToSquare(player))
{
v.push_back(d); //Found delta
break; //Next delta
}
}
}
assert(!v.empty());
for (const std::pair<int,int> d: v)
{
int cur_x = x + d.first;
int cur_y = y + d.second;
//Adjacent square must be of opponent
assert(Get(cur_x,cur_y) == PlayerToSquare(GetOtherPlayer(player)));
Set(cur_x,cur_y,PlayerToSquare(player));
//Follow the trail until
// - player is found -> Can do move
while (1)
{
cur_x += d.first;
cur_y += d.second;
//Must be a valid direction
assert(!(cur_x < 0 || cur_y < 0 || cur_x >= GetSize() || cur_y >= GetSize()));
if (Get(cur_x,cur_y) == PlayerToSquare(player)) break;
assert(Get(cur_x,cur_y) == PlayerToSquare(GetOtherPlayer(player)));
Set(cur_x,cur_y,PlayerToSquare(player));
}
}
Set(x,y,PlayerToSquare(player));
}
ribi::reversi::Square ribi::reversi::Board::Get(const int x, const int y) const noexcept
{
#ifndef NDEBUG
const int sz = GetSize();
assert(x >= 0);
assert(y >= 0);
assert(x < sz);
assert(y < sz);
#endif
return m_board[y][x];
}
std::string ribi::reversi::Board::GetVersion() noexcept
{
return "2.1";
}
std::vector<std::string> ribi::reversi::Board::GetVersionHistory() noexcept
{
return {
"2007-09-24: version 1.0: initial version developed under C++ Builder, called Reversi",
"2010-09-24: version 1.1: initial port to Qt Creator",
"2013-12-19: version 2.0: split interface in reversi::Board and reversi::Widget",
"2014-02-14: version 2.1: use of enum classes, added ToTextCanvas member function"
};
}
ribi::reversi::Player ribi::reversi::GetOtherPlayer(const Player player) noexcept
{
switch (player)
{
case Player::player1: return Player::player2;
case Player::player2: return Player::player1;
default: assert(!"Should not get here");
}
assert(!"Should not get here");
return Player::player1;
}
const std::vector<std::pair<int,int>> ribi::reversi::Board::GetValidMoves(const Player player) const noexcept
{
const int size = GetSize();
std::vector< std::pair<int,int> > v;
for (int y=0; y!=size; ++y)
{
for (int x=0; x!=size; ++x)
{
if (CanDoMove(x,y,player))
{
v.push_back( std::make_pair(x,y) );
}
}
}
return v;
}
int ribi::reversi::Board::GetSize() const noexcept
{
return m_board.size();
}
ribi::reversi::Square ribi::reversi::Board::PlayerToSquare(const Player player) const noexcept
{
switch (player)
{
case Player::player1: return Square::player1;
case Player::player2: return Square::player2;
default:
assert(!"Should not get here");
return Square::player1;
}
}
void ribi::reversi::Board::Set(const int x, const int y, const Square state) noexcept
{
assert(x>=0 && x < GetSize());
assert(y>=0 && y < GetSize());
m_board[y][x] = state;
assert(Get(x,y)==state);
}
const boost::shared_ptr<ribi::TextCanvas> ribi::reversi::Board::ToTextCanvas() const noexcept
{
const int n_rows = static_cast<int>(m_board.size());
if (n_rows == 0)
{
return nullptr;
}
const int n_cols = static_cast<int>(m_board[0].size());
boost::shared_ptr<TextCanvas> canvas {
new TextCanvas(n_cols,n_rows)
};
for(int row=0; row!=n_rows; ++row)
{
assert(m_board[row].size() == m_board[0].size());
for (int col=0; col!=n_cols; ++col)
{
const Square square = m_board[row][col];
char c = ' ';
switch (square)
{
case Square::empty : c = '.'; break;
case Square::player1: c = 'O'; break;
case Square::player2: c = 'X'; break;
default: assert(!"Should not get here");
}
canvas->PutChar(col,row,c);
}
}
return canvas;
}
bool ribi::reversi::operator==(const ribi::reversi::Board& lhs, const ribi::reversi::Board& rhs)
{
return lhs.GetBoard() == rhs.GetBoard();
}
bool ribi::reversi::operator!=(const ribi::reversi::Board& lhs, const ribi::reversi::Board& rhs)
{
return !(lhs == rhs);
}
std::ostream& ribi::reversi::operator<<(std::ostream& os, const ribi::reversi::Board& r)
{
for(const std::vector<Square>& line: r.m_board)
{
std::transform(line.begin(),line.end(),
std::ostream_iterator<std::string>(os,""),
[](const Square square)
{
switch (square)
{
case Square::empty : return ".";
case Square::player1: return "1";
case Square::player2: return "2";
default: assert(!"Should not get here");
}
assert(!"Should not get here");
throw std::logic_error("operator<<(std::ostream& os, const Board& r): Unknown square type");
}
);
os << '\n';
}
return os;
}
std::istream& ribi::reversi::operator>>(std::istream& is, ribi::reversi::Board& r)
{
std::vector<std::string> v;
{
//Read first line
{
std::string s;
is >> s;
assert(is);
v.push_back(s);
}
//Read next lines
assert(!v.empty());
const int size = static_cast<int>(v[0].size());
for (int i=1; i!=size; ++i)
{
std::string s;
is >> s;
assert(is);
assert(s.size() == v[0].size());
v.push_back(s);
}
assert(size == static_cast<int>(v.size()));
}
r = Board(static_cast<int>(v.size()));
{
const int size = static_cast<int>(v.size());
for (int y=0; y!=size; ++y)
{
const std::string& line = v[y];
for (int x=0; x!=size; ++x)
{
const char c = line[x];
switch (c)
{
case '1': r.Set(x,y,Square::player1); break;
case '2': r.Set(x,y,Square::player2); break;
case '.': r.Set(x,y,Square::empty); break;
default: assert(!"Should not get here");
}
}
}
}
return is;
}