-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchess_move.cpp
453 lines (426 loc) · 12.5 KB
/
chess_move.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "chess_move.h"
#include "game.h"
#include <cassert>
#include <iostream>
#include <regex>
chess_move::chess_move(std::string s, const chess_color color)
: m_color{color},
m_is_capture{false},
m_pgn_str{s}
{
// Simple 'go to there' move, no 'from'
if (std::regex_match(s, std::regex("^[BKNQR]?x?[a-h][1-8](=[BKNQR])?(\\?\\?|\\?|\\+|#)?$")))
{
m_to = get_square(s);
m_type = get_piece_type(s);
m_is_capture = ::is_capture(s);
m_promotion_type = ::get_promotion_type(s);
}
else if (std::regex_match(s, std::regex("^(0-1)|1-0|(1/2-1/2)$")))
{
m_winner = ::get_winner(s);
}
else if (std::regex_match(s, std::regex("^O-O(-O)?$")))
{
if (s == "O-O")
{
m_castling_type = castling_type::king_side;
}
else
{
assert(s == "O-O-O");
m_castling_type = castling_type::queen_side;
}
}
}
bool is_capture(const std::string& s)
{
const std::regex e("x");
std::smatch m;
return std::regex_search(s, m, e);
}
square get_from(const game& g, const chess_move& m)
{
if (m.get_type().has_value())
{
const piece_type pt{m.get_type().value()};
switch (pt)
{
case piece_type::bishop: return get_from_for_bishop(g, m);
case piece_type::king: return get_from_for_king(g, m);
case piece_type::knight: return get_from_for_knight(g, m);
case piece_type::pawn: return get_from_for_pawn(g, m);
case piece_type::queen: return get_from_for_queen(g, m);
default:
case piece_type::rook:
assert(pt == piece_type::rook);
return get_from_for_rook(g, m);
}
}
assert(!"TODO");
}
square get_from_for_bishop(const game& g, const chess_move& m)
{
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::bishop);
const auto pieces{
find_pieces(g, piece_type::bishop, m.get_color())
};
assert(!pieces.empty());
assert(m.get_to().has_value());
const square target{m.get_to().value()};
for (const auto& piece: pieces)
{
if (are_on_same_diagonal(piece.get_current_square(), target))
{
return piece.get_current_square();
}
}
assert(!"Should not get here: there had to be a bishop on the diagonal");
}
square get_from_for_king(const game& g, const chess_move& m)
{
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::king);
const auto pieces{
find_pieces(g, piece_type::king, m.get_color())
};
assert(!pieces.empty());
assert(pieces.size() == 1); // There is only 1 king
return pieces[0].get_current_square();
}
square get_from_for_knight(const game& g, const chess_move& m)
{
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::knight);
const auto pieces{
find_pieces(g, piece_type::knight, m.get_color())
};
assert(!pieces.empty());
assert(m.get_to().has_value());
const square target{m.get_to().value()};
for (const auto& piece: pieces)
{
if (are_adjacent_for_knight(piece.get_current_square(), target))
{
return piece.get_current_square();
}
}
assert(!"Should not get here: there had to be a knight a knight's jump away");
}
square get_from_for_pawn(const game& g, const chess_move& m)
{
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::pawn);
assert(!m.is_capture());
const square one_behind{
get_behind(m.get_to().value(), m.get_color())
};
if (is_piece_at(g, one_behind))
{
assert(get_piece_at(g, one_behind).get_type() == piece_type::pawn);
return one_behind;
}
const square two_behind{get_behind(one_behind, m.get_color())};
assert(is_piece_at(g, two_behind));
assert(get_piece_at(g, two_behind).get_type() == piece_type::pawn);
return two_behind;
}
square get_from_for_queen(const game& g, const chess_move& m)
{
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::queen);
const auto pieces{
find_pieces(g, piece_type::queen, m.get_color())
};
assert(!pieces.empty());
assert(m.get_to().has_value());
const square target{m.get_to().value()};
for (const auto& piece: pieces)
{
if (are_on_same_diagonal(piece.get_current_square(), target)
|| are_on_same_rank(piece.get_current_square(), target)
|| are_on_same_file(piece.get_current_square(), target)
)
{
return piece.get_current_square();
}
}
assert(!"Should not get here: there had to be a queen on the same rank, file, or diagonal");
}
square get_from_for_rook(const game& g, const chess_move& m)
{
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::rook);
const auto pieces{
find_pieces(g, piece_type::rook, m.get_color())
};
assert(!pieces.empty());
assert(m.get_to().has_value());
const square target{m.get_to().value()};
for (const auto& piece: pieces)
{
if (are_on_same_rank(piece.get_current_square(), target)
|| are_on_same_file(piece.get_current_square(), target)
)
{
return piece.get_current_square();
}
}
assert(!"Should not get here: there had to be a rook on the same rank or file");
}
piece_type get_piece_type(const std::string& s)
{
const std::regex e("[BKNQR]");
std::smatch m;
const bool is_there{std::regex_search(s, m, e)};
if (!is_there) return piece_type::pawn;
assert(!m.str().empty());
assert(m.str().size() == 1);
return to_piece_type(m.str()[0]);
}
std::optional<piece_type> get_promotion_type(const std::string& s)
{
const std::regex e("=[BKNQR]");
std::smatch m;
const bool is_there{std::regex_search(s, m, e)};
if (!is_there) return {};
assert(!m.str().empty());
assert(m.str().size() == 2);
assert(m.str()[0] == '=');
return to_piece_type(m.str()[1]);
}
square get_square(const std::string& s)
{
const std::regex e("[a-h][1-8]");
std::smatch m;
const bool is_there{std::regex_search(s, m, e)};
assert(is_there);
return square(m.str());
}
std::vector<chess_color> get_winner(const std::string& s)
{
assert(std::regex_match(s, std::regex("^(0-1)|1-0|(1/2-1/2)$")));
if (s == "1-0") return { chess_color::white };
if (s == "0-1") return { chess_color::black };
assert(s == "1/2-1/2");
return { chess_color::white, chess_color::black };
}
bool is_capture(const chess_move& move) noexcept
{
return move.is_capture();
}
bool is_castling(const chess_move& move) noexcept
{
return move.get_castling_type().has_value();
}
bool is_draw(const chess_move& move) noexcept
{
return move.get_winner().size() == 2;
}
bool is_promotion(const chess_move& move) noexcept
{
return move.get_promotion_type().has_value();
}
bool is_simple_move(const chess_move& move) noexcept
{
return !move.is_capture()
&& !is_win(move)
&& !is_draw(move)
&& !is_promotion(move)
&& !is_castling(move)
;
}
bool is_win(const chess_move& move) noexcept
{
return move.get_winner().size() == 1;
}
void test_chess_move()
{
#ifndef NDEBUG
// Constructor
{
const std::string pgn_str{"e4"};
const chess_move m{pgn_str, chess_color::white};
assert(pgn_str == m.get_pgn_str());
}
// chess_move::get_pgn_str
{
const std::string pgn_str{"Qd4"};
const chess_move m{pgn_str, chess_color::white};
assert(pgn_str == m.get_pgn_str());
}
// Individual functions
// get_from: e2-e3
{
const game g;
const chess_move m("e3", chess_color::white);
assert(get_from(g, m) == square("e2"));
}
// get_winner
{
assert(get_winner("0-1").at(0) == chess_color::black);
assert(get_winner("1-0").at(0) == chess_color::white);
assert(get_winner("1/2-1/2").size() == 2);
}
// is_capture
// is_castling
{
assert(is_castling(chess_move("O-O", chess_color::white)));
assert(is_castling(chess_move("O-O-O", chess_color::white)));
assert(is_castling(chess_move("O-O", chess_color::black)));
assert(is_castling(chess_move("O-O-O", chess_color::black)));
assert(!is_castling(chess_move("e4", chess_color::white)));
assert(!is_castling(chess_move("1-0", chess_color::white)));
assert(!is_castling(chess_move("1/2-1/2", chess_color::white)));
}
// is_draw
{
assert(is_draw(chess_move("1/2-1/2", chess_color::white)));
assert(is_draw(chess_move("1/2-1/2", chess_color::black)));
assert(!is_draw(chess_move("1-0", chess_color::white)));
assert(!is_draw(chess_move("0-1", chess_color::white)));
assert(!is_draw(chess_move("e4", chess_color::white)));
}
// is_move
{
// e4
{
const chess_move m{"e4", chess_color::white};
assert(!is_capture(m));
assert(!is_win(m));
assert(!is_draw(m));
assert(!is_promotion(m));
assert(!is_castling(m));
assert(is_simple_move(m));
}
{
const chess_move m("Qxf7+", chess_color::white);
assert(is_capture(m));
assert(!is_win(m));
assert(!is_draw(m));
assert(!is_promotion(m));
assert(!is_castling(m));
assert(!is_simple_move(m));
}
// Some quick ones
{
assert(is_simple_move(chess_move("e4", chess_color::white)));
assert(!is_simple_move(chess_move("Qxf7", chess_color::white)));
assert(!is_simple_move(chess_move("Qxf7+", chess_color::white)));
assert(!is_simple_move(chess_move("Qxf7#", chess_color::white)));
assert(!is_simple_move(chess_move("0-1", chess_color::white)));
assert(!is_simple_move(chess_move("O-O", chess_color::white)));
assert(!is_simple_move(chess_move("e8=Q", chess_color::white)));
}
}
// is_promotion
{
assert(is_promotion(chess_move("e8=Q", chess_color::white)));
assert(!is_promotion(chess_move("e7", chess_color::white)));
}
// '1. e4 e5 2. Qh5 Nc6 3. Bc4 Nf6?? Qxf7# 1-0'
{
const chess_move m("e4", chess_color::white);
assert(m.get_to().has_value());
assert(m.get_to().value() == square("e4"));
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::pawn);
assert(!m.is_capture());
}
{
const chess_move m("e5", chess_color::black);
assert(m.get_to().has_value());
assert(m.get_to().value() == square("e5"));
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::pawn);
assert(!m.is_capture());
}
{
const chess_move m("Qh5", chess_color::white);
assert(m.get_to().has_value());
assert(m.get_to().value() == square("h5"));
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::queen);
assert(!m.is_capture());
}
{
const chess_move m("Nc6", chess_color::black);
assert(m.get_to().has_value());
assert(m.get_to().value() == square("c6"));
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::knight);
assert(!m.is_capture());
}
{
const chess_move m("Bc4", chess_color::white);
assert(m.get_to().has_value());
assert(m.get_to().value() == square("c4"));
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::bishop);
assert(!m.is_capture());
}
{
const chess_move m("Nf6??", chess_color::black);
assert(m.get_to().has_value());
assert(m.get_to().value() == square("f6"));
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::knight);
assert(!m.is_capture());
}
{
const chess_move m("Qxf7#", chess_color::white);
assert(m.get_to().has_value());
assert(m.get_to().value() == square("f7"));
assert(m.get_type().has_value());
assert(m.get_type().value() == piece_type::queen);
assert(m.is_capture());
}
{
const chess_move m("1-0", chess_color::white);
assert(!m.get_to().has_value());
assert(!m.get_type().has_value());
assert(!m.is_capture());
assert(!m.get_winner().empty());
assert(m.get_winner().at(0) == chess_color::white);
assert(!m.is_capture());
}
// operator==
{
// On string
{
const chess_move a("e2", chess_color::white);
const chess_move b("e2", chess_color::white);
const chess_move c("e3", chess_color::white);
assert(a == b);
assert(!(a == c));
}
// On color
{
const chess_move a("Qxf7#", chess_color::white);
const chess_move b("Qxf7#", chess_color::white);
const chess_move c("Qxf7#", chess_color::black);
assert(a == b);
assert(!(a == c));
}
}
// operator<<
{
const chess_move m("Qxf7#", chess_color::white);
std::stringstream s;
s << m;
assert(!s.str().empty());
}
#endif // NDEBUG
}
bool operator==(const chess_move& lhs, const chess_move& rhs) noexcept
{
return lhs.get_pgn_str() == rhs.get_pgn_str()
&& lhs.get_color() == rhs.get_color()
;
}
std::ostream& operator<<(std::ostream& os, const chess_move& m) noexcept
{
os << m.get_pgn_str();
return os;
}