-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoard.cpp
More file actions
executable file
·408 lines (360 loc) · 12.2 KB
/
Copy pathChessBoard.cpp
File metadata and controls
executable file
·408 lines (360 loc) · 12.2 KB
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
#include "ChessBoard.h"
#include "RookPiece.h"
#include "BishopPiece.h"
#include "PawnPiece.h"
#include "KingPiece.h"
#include "QueenPiece.h"
#include "KnightPiece.h"
#include "ChessPiece.h"
#include "ChessMove.h"
#include "Matrix.h"
#include "Helper.h"
#include <memory>
#include <cmath>
void ChessBoard::move_piece(ChessMove move) {
// Retrieve the piece to move
std::shared_ptr<ChessPiece> cp = move.piece;
// Change the x- and y-coordinates of the piece
cp->x = move.x_to;
cp->y = move.y_to;
// Move the piece
this->state.set_element(0, move.x_to+move.y_to*8, cp);
// Make the original square empty (i.e. set pointer to NULL)
this->state.set_element(0, move.x_from+move.y_from*8, NULL);
}
void ChessBoard::rewind_move_piece(ChessMove move,
std::shared_ptr<ChessPiece> cp_removed_by_move) {
// Retrieve the piece of the move to rewind
std::shared_ptr<ChessPiece> cp_to_rewind = move.piece;
// Change the x- and y-coordinates of the piece
cp_to_rewind->x = move.x_from;
cp_to_rewind->y = move.y_from;
// Put the moved piece back into its original place
this->state.set_element(0, move.x_from+move.y_from*8, cp_to_rewind);
// Put the piece removed by the move back into place
this->state.set_element(0, move.x_to+move.y_to*8, cp_removed_by_move);
}
void ChessBoard::ai1_make_move(bool isWhite) {
/* Function to make a move according to the AI-1 strategy: random thinker
- If there are no capturing moves, this AI will perform a non capturing
at random.
- If there are several capturing moves, the AI will perform one of them at
random.
- If there is a pawn promotion, a random piece will be selected.
*/
// Get all non-capturing and capturing moves
std::vector<ChessMove> nc_moves = non_capturing_moves(isWhite);
std::vector<ChessMove> c_moves = capturing_moves(isWhite);
ChessMove move;
// If capturing moves, randomly select a move
if (c_moves.size() > 0) {
move = *select_random_element(c_moves.begin(), c_moves.end());
// Otherwise randomly select a non-capturing move
} else {
move = *select_random_element(nc_moves.begin(), nc_moves.end());
}
// Make the move
move_piece(move);
// At the end of the board, the rPawn transforms into any of Queen, Bishop,
// Knight, Bishop, King -- through random selection
if ((move.piece->type == pawn && move.piece->isWhite && move.y_to == 7) ||
(move.piece->type == pawn && !move.piece->isWhite && move.y_to == 0)) {
// Select a random promotion piece
int num = gen_rand_num(0, 4);
std::shared_ptr<ChessPiece> cp_new = create_new_piece(move.x_to,
move.y_to, move.piece->get_color(), Type(num), move.piece->board);
// Place the promotion piece on the board
this->state.set_element(0, move.y_to*8+move.x_to, cp_new);
}
switch_turn();
}
void ChessBoard::ai2_make_move(bool isWhite) {
/* Function to make a move according to the AI-2 strategy: think one step
ahead, but default to random
- If there are no capturing moves
* Check if any non-capturing move will force a capturing move for the
opponent.
* If not, play a random move.
- If there are several capturing moves
* Check if any capturing move will force a capturing move for the opponent
* If not, play a random capturing move.
- If there is a pawn promotion
* If possible, pick a piece that can not capture on its next move.
* If not possible, promote to a random piece.
*/
// Get all non-capturing and capturing moves
std::vector<ChessMove> nc_moves = non_capturing_moves(isWhite);
std::vector<ChessMove> c_moves = capturing_moves(isWhite);
bool hasMoved = false;
ChessMove move;
if (c_moves.size() > 0) {
// If capturing move, see if one could force the opponent to capture one of
// ours
for (auto const& m: c_moves) {
std::shared_ptr<ChessPiece> cp_to = this->get_piece(m.x_to, m.y_to);
move_piece(m);
if (capturing_moves(!isWhite).size() != 0) {
move = m;
hasMoved = true;
break;
} else {
rewind_move_piece(m, cp_to);
}
}
// Default to random selection
if (!hasMoved) {
move = *select_random_element(c_moves.begin(), c_moves.end());
move_piece(move);
}
// Otherwise see if a non-capturing move could force the opponent to capture
// one of ours
} else {
for (auto const& m: nc_moves) {
std::shared_ptr<ChessPiece> cp_to = this->get_piece(m.x_to, m.y_to);
move_piece(m);
if (capturing_moves(!isWhite).size() != 0) {
move = m;
hasMoved = true;
break;
} else {
rewind_move_piece(m, cp_to);
}
}
// Default to random selection
if (!hasMoved) {
move = *select_random_element(nc_moves.begin(), nc_moves.end());
move_piece(move);
}
}
// At the end of the board, the Pawn transforms into any of Queen, Bishop,
// Knight, Bishop, King -- preferrably one that can be captured on the next
// move
if ((move.piece->type == pawn && move.piece->isWhite && move.y_to == 7) ||
(move.piece->type == pawn && !move.piece->isWhite && move.y_to == 0)) {
bool hasBeenPromoted = false;
// See if particular promotion could force the opponent to capture one of
// ours
for (int i = 0; i < 5; i++) {
std::shared_ptr<ChessPiece> cp_new = create_new_piece(move.x_to,
move.y_to, move.piece->get_color(), Type(i), move.piece->board);
// Place the promotion piece on the board
if (capturing_moves(!isWhite).size() != 0) {
this->state.set_element(0, move.y_to*8+move.x_to, cp_new);
hasBeenPromoted = true;
break;
}
}
// Default to random selection
if (!hasBeenPromoted) {
int num = gen_rand_num(0, 4);
std::shared_ptr<ChessPiece> cp_new = create_new_piece(move.x_to,
move.y_to, move.piece->get_color(), Type(num), move.piece->board);
// Place the promotion piece on the board
this->state.set_element(0, move.y_to*8+move.x_to, cp_new);
}
}
switch_turn();
}
std::shared_ptr<ChessPiece> ChessBoard::create_new_piece(int x, int y,
bool isWhite, int type, ChessBoard* board) {
std::shared_ptr<ChessPiece> cp;
switch (type) {
case king:
cp = std::make_shared<KingPiece>(x, y, isWhite, king, board);
case queen:
cp = std::make_shared<QueenPiece>(x, y, isWhite, queen, board);
break;
case bishop:
cp = std::make_shared<BishopPiece>(x, y, isWhite, bishop, board);
break;
case knight:
cp = std::make_shared<KnightPiece>(x, y, isWhite, knight, board);
break;
case rook:
cp = std::make_shared<RookPiece>(x, y, isWhite, rook, board);
break;
}
return cp;
}
void ChessBoard::initialise_board(Matrix<std::shared_ptr<ChessPiece>> m) {
turn = 1;
state = m;
}
ChessBoard & operator>>(std::istream& is, ChessBoard& board) {
Matrix<std::shared_ptr<ChessPiece>> m(1, 64);
char c;
int cnt = 0;
// Read the input stream
c = is.get();
while (is) {
if (c == '\n') {
c = is.get();
} else {
int x = cnt % 8;
int y = std::floor(cnt / 8);
// Put piece corresponding to character on the board
switch (c) {
case 'K':
m.set_element(0, cnt, std::make_shared<KingPiece>(
x, y, true, king, &board));
break;
case 'k':
m.set_element(0, cnt, std::make_shared<KingPiece>(
x, y, false, king, &board));
break;
case 'Q':
m.set_element(0, cnt, std::make_shared<QueenPiece>(
x, y, true, queen, &board));
break;
case 'q':
m.set_element(0, cnt, std::make_shared<QueenPiece>(
x, y, false, queen, &board));
break;
case 'N':
m.set_element(0, cnt, std::make_shared<KnightPiece>(
x, y, true, knight, &board));
break;
case 'n':
m.set_element(0, cnt, std::make_shared<KnightPiece>(
x, y, false, knight, &board));
break;
case 'P':
m.set_element(0, cnt, std::make_shared<PawnPiece>(
x, y, true, pawn, &board));
break;
case 'p':
m.set_element(0, cnt, std::make_shared<PawnPiece>(
x, y, false, pawn, &board));
break;
case 'B':
m.set_element(0, cnt, std::make_shared<BishopPiece>(
x, y, true, bishop, &board));
break;
case 'b':
m.set_element(0, cnt, std::make_shared<BishopPiece>(
x, y, false, bishop, &board));
break;
case 'R':
m.set_element(0, cnt, std::make_shared<RookPiece>(
x, y, true, rook, &board));
break;
case 'r':
m.set_element(0, cnt, std::make_shared<RookPiece>(
x, y, false, rook, &board));
break;
default:
m.set_element(0, cnt, nullptr);
}
cnt++;
c = is.get();
}
}
if (cnt < 64) {
std::cout << "Illegal size of input stream!" <<std::endl;
exit(0);
}
// Initialize the board
board.initialise_board(m);
return board;
}
void ChessBoard::print_board() {
std::cout << "\n ";
int cnt = 0;
for (int file = 0; file <= 7; file++)
std::cout << file << " ";
std::cout << std::endl;
for (int rank=0; rank <= 7; rank++) {
std::cout << " +---+---+---+---+---+---+---+---+" << std::endl;
std::cout << rank << " ";
for (char file = 'A'; file <= 'H'; file++) {
std::cout << '|' << " ";
if (state.get_element(0, cnt) != NULL) {
std::cout << state.get_element(0, cnt)->utf_representation();
} else {
std::cout << " ";
}
cnt ++;
std::cout << " ";
}
std::cout << "|" << std::endl;
}
std::cout << " +---+---+---+---+---+---+---+---+\n" << std::endl;
}
void ChessBoard::switch_turn() {
turn = !turn;
}
bool ChessBoard::get_turn() {
return turn;
}
std::shared_ptr<ChessPiece> ChessBoard::get_piece(int x, int y) {
return state.get_element(0, x+y*8);
}
std::vector<ChessMove> ChessBoard::capturing_moves(bool isWhite) {
std::vector<ChessMove> capturingMoves;
int c = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
std::shared_ptr<ChessPiece> cp = this->get_piece(x, y);
if (cp != NULL && (cp->get_color() == isWhite)) {
std::vector<ChessMove> moves =
this->get_piece(x, y)->capturing_moves();
capturingMoves.insert(capturingMoves.end(), moves.begin(),
moves.end());
}
}
}
return capturingMoves;
}
std::vector<ChessMove> ChessBoard::non_capturing_moves(bool isWhite) {
std::vector<ChessMove> nonCapturingMoves;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
std::shared_ptr<ChessPiece> cp = this->get_piece(x, y);
if (cp != NULL && (cp->get_color() == isWhite)) {
std::vector<ChessMove> moves =
this->get_piece(x, y)->non_capturing_moves();
nonCapturingMoves.insert(nonCapturingMoves.end(), moves.begin(),
moves.end());
}
}
}
return nonCapturingMoves;
}
std::vector<ChessMove> ChessBoard::possible_moves(bool isWhite) {
std::vector<ChessMove> possible_moves = non_capturing_moves(isWhite);
std::vector<ChessMove> c_moves = capturing_moves(isWhite);
possible_moves.insert(possible_moves.end(), c_moves.begin(), c_moves.end());
return possible_moves;
}
std::shared_ptr<ChessPiece> ChessBoard::find_king(bool isWhite) {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
std::shared_ptr<ChessPiece> cp = this->get_piece(x, y);
if (cp != NULL) {
if (cp->get_color() == isWhite && cp->get_type() == king) {
return cp;
}
}
}
}
return nullptr;
}
bool ChessBoard::king_in_check(bool isWhite) {
std::shared_ptr<ChessPiece> king = find_king(isWhite);
if (king != NULL) {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
std::shared_ptr<ChessPiece> cp = this->get_piece(x, y);
if (cp != NULL && cp->get_color() != isWhite) {
if (cp->valid_move(king->x, king->y)) {
return true;
}
}
}
}
}
return false;
}
bool ChessBoard::any_valid_move(bool isWhite) {
return possible_moves(isWhite).size() > 0 ? true : false;
}