-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoard.cpp
More file actions
474 lines (400 loc) · 11.7 KB
/
Copy pathChessBoard.cpp
File metadata and controls
474 lines (400 loc) · 11.7 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "ChessBoard.h"
#include <cctype>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
ChessBoard::ChessBoard() {initialiseBoard(); }
ChessBoard::~ChessBoard() {clearBoard(); }
void ChessBoard::submitMove(const char* source, const char* dest){
string from = source;
string to = dest;
//Test whether game is still unfinished
if (game_over){
cout << "The game has finished" << endl;
return;
}
//Test if inputs were correctly specified i.e. on board, only two digits etc.
if(formatInvalid(from, to))
return;
//Test for whether a square is taken by a piece
if (isEmpty(from)){
cout << "There is no piece at position " << from << "!" << endl;
return;
}
// Test for whether player whos turn it is has moved
if (board[from]->getColour() != getNextMove()){
cout << "It is not " << stringColour(board[from]->getColour())
<< "'s turn to move!" << endl;
return;
}
//Special case: Has Castling been called and is it currently legal?
//(Castling be called by moving the king to the position of one of the rooks.
if (!isEmpty(to) && board[to]->getColour() == board[from]->getColour()){
if (board[from]->getType() == KING && board[to]->getType() == ROOK){
castling(from, to);
return;
}
}
//Getting all legal targets for the piece on the source square
//If destination square not in the vector --> illegal move
vector<string> targets = board[from]->getLegalTargets(from, *this);
if (find(targets.begin(), targets.end(), to) == targets.end()){
cout << stringColour(board[from]->getColour()) << "'s "
<< stringPiece(board[from]->getType()) << " cannot move to " << to
<<"!" << endl;
return;
}
//Make the actual move if it is legal. Capture a piece if necessary*/
cout << stringColour(board[from]->getColour()) << "'s "
<< stringPiece(board[from]->getType()) << " moves from "
<< from << " to " << to;
if (!isEmpty(to)){
cout << " taking " << stringColour(board[to]->getColour()) << "'s "
<< stringPiece(board[to]->getType());
delete board[to];
}
cout << endl;
board[to] = board[from];
board.erase(from);
//Update move count for certain pieces (needed for castling and pawn opening)
if (board[to]->getType() == KING ||
board[to]->getType() == ROOK ||
board[to]->getType() == PAWN)
board[to]->addMove();
//Switch turns and test whether the game has reached stalemate or checkmate
changeTurn();
if (!this->legalMovesLeft(getNextMove())){
switchGameState();
if (this->inCheck(getNextMove())){
cout << stringColour(getNextMove()) << " is in checkmate" << endl;
return;
}
cout << "The game has ended in a stalemate" << endl;
return;
}
//Inform a player that its king in check
if (this->inCheck(getNextMove()))
cout << stringColour(getNextMove()) << " is in check" << endl;
return;
}
/* End of function*/
void ChessBoard::resetBoard(){
next_move = WHITE;
game_over = false;
clearBoard();
initialiseBoard();
}
/* End of function*/
void ChessBoard::initialiseBoard(){
string square;
//WHITE
for(square = "A2"; square[0] <= 'H'; ++square[0])
board[square] = new Pawn(WHITE);
board["A1"] = new Rook(WHITE);
board["B1"] = new Knight(WHITE);
board["C1"] = new Bishop(WHITE);
board["D1"] = new Queen(WHITE);
board["E1"] = new King(WHITE);
board["F1"] = new Bishop(WHITE);
board["G1"] = new Knight(WHITE);
board["H1"] = new Rook(WHITE);
//BLACK
for(square = "A7"; square[0] <= 'H'; ++square[0])
board[square] = new Pawn(BLACK);
board["A8"] = new Rook(BLACK);
board["B8"] = new Knight(BLACK);
board["C8"] = new Bishop(BLACK);
board["D8"] = new Queen(BLACK);
board["E8"] = new King(BLACK);
board["F8"] = new Bishop(BLACK);
board["G8"] = new Knight(BLACK);
board["H8"] = new Rook(BLACK);
cout << "A new chess game is started!" << endl;
}
/* End of function*/
void ChessBoard::clearBoard(){
for (auto it = board.begin() ; it != board.end() ; it++)
delete it->second;
board.clear();
}
/* End of function*/
bool ChessBoard::isDiagonalFree(std::string const& from, std::string const& to){
int file_diff = from[0] - to[0], rank_diff = from[1] - to[1];
string big = to, small = from;
if (file_diff == rank_diff){
if (file_diff > 0){
big = from;
small = to;
}
small[0]++;
small[1]++;
while (small != big){
if (!this->isEmpty(small))
return false;
small[0]++;
small[1]++;
}
return true;
}
if (file_diff > 0){
big = from;
small = to;
}
small[0]++;
small[1]--;
while (small != big){
if (!this->isEmpty(small))
return false;
small[0]++;
small[1]--;
}
return true;
}
/* End of function*/
bool ChessBoard::isFileRankFree(std::string const& from, std::string const& to){
int file_diff = from[0] - to[0], rank_diff = from[1] - to[1];
string big = to, small = from;
if (file_diff){
if (file_diff > 0){
big = from;
small = to;
}
small[0]++;
while (small != big){
if (!this->isEmpty(small))
return false;
small[0]++;
}
return true;
}
if (rank_diff > 0){
big = from;
small = to;
}
small[1]++;
while (small != big){
if (!this->isEmpty(small))
return false;
small[1]++;
}
return true;
}
/* End of function*/
bool ChessBoard::moveExposesKing(std::string const& from, std::string const& to){
bool check_status;
Piece* captured = nullptr;
//Store current board information
if (!this->isEmpty(to))
captured = board[to];
//Simulate the move
board[to] = board[from];
board.erase(from);
//See whether this leads to check
check_status = this->inCheck(board[to]->getColour());
//Revert changes
board[from] = board[to];
if (captured)
board[to] = captured;
else board.erase(to);
return check_status;
}
/* End of function*/
bool ChessBoard::inCheck(Colour colour){
string king = findPiece(KING, colour);
Colour attacker = BLACK;
if (colour == BLACK)
attacker = WHITE;
for (auto it = board.begin() ; it != board.end() ; it++){
if (it->second->getColour() == attacker){
vector<string> vec = it->second->getLegalTargets(it->first, *this);
if (find(vec.begin(), vec.end(), king) != vec.end())
return true; //If Kings position legal for one opposing piece -> check
}
}
return false; //No piece can attack the king --> no check
}
/* End of function*/
bool ChessBoard::legalMovesLeft(Colour colour){
std::string target = "A1";
/* For Chess, I deliberately chose to not use named constants for file/rank,
since I found the code to be closer to chess notation and thus more legible*/
for ( ; target[0] <= 'H' ; ++target[0]){
for ( ; target[1] <= '8'; ++target[1]){
if (!isEmpty(target) && board[target]->getColour() == colour){
vector<string> targets = board[target]->getLegalTargets(target, *this);
//True once one piece has a legal move (i.e. no check) left
if (targets.size())
return true;
}
}
target[1] = '1';
}
//False otherwise
return false;
}
/* End of function*/
void ChessBoard::castling(std::string const& from, std::string const& to){
string target = from;
if (board[from]->getMoveCount() || board[to]->getMoveCount()){
cout << "Invalid: Castling only legal when either piece has not yet moved "
<< endl;
return;
}
if (inCheck(board[from]->getColour())){
cout << "Invalid: Cannot castle when in check" << endl;
return;
}
if (!isFileRankFree(from, to)){
cout << "Invalid: Cannot castle with pieces between King and Rook" << endl;
return;
}
//QUEENSIDE
if (to[0] < from[0]){
target[0]--;
if (moveExposesKing(from, target)){
cout << "Invalid: Cannot castle through check" << endl;
return;
}
target[0]--;
if (moveExposesKing(from, target)){
cout << "Invalid: Cannot castle into check" << endl;
return;
}
board[from]->addMove();
board[to]-> addMove();
board[target] = board[from];
target[0]++;
board[target] = board[to];
board.erase(from);
board.erase(to);
cout << stringColour(board[target]->getColour())
<< " has completed queenside castling "<< endl;
}
//KINGSIDE
if (to[0] > from[0]){
target[0]++;
if (moveExposesKing(from, target)){
cout << "Invalid: Cannot castle through check" << endl;
return;
}
target[0]++;
if (moveExposesKing(from, target)){
cout << "Invalid: Cannot castle into check" << endl;
return;
}
board[from]->addMove();
board[to]-> addMove();
board[target] = board[from];
target[0]--;
board[target] = board[to];
board.erase(from);
board.erase(to);
cout << stringColour(board[target]->getColour())
<< " has completed kingside castling "<< endl;
}
changeTurn();
//Also need to check for checkmate, check and stalemate afterwards
//Castling will almost never result in checkmate, but theoretically possible
if (!this->legalMovesLeft(getNextMove())){
switchGameState();
if (this->inCheck(getNextMove())){
cout << stringColour(getNextMove()) << " is in checkmate" << endl;
return;
}
cout << "The game has ended in a stalemate" << endl;
return;
}
if (this->inCheck(getNextMove()))
cout << stringColour(getNextMove()) << " is in check" << endl;
return;
}
/* End of function*/
bool ChessBoard::formatInvalid(const string& from, const string& to){
if (!correctFormat(from) || !correctFormat(to))
return true;
if (!onBoard(from)){
cout << "Source square is not on the board" << endl;
return true;
}
if (!onBoard(to)){
cout << "Destination square is not on the board" << endl;
return true;
}
if (from == to){
cout << "Invalid. Destination and Source square are equal. " << endl;
return true;
}
return false;
}
/* End of function*/
bool ChessBoard::sameColour(const string& from, const string& to){
if (board[from]->getColour() == board[to]->getColour())
return true;
return false;
}
/* End of function*/
bool ChessBoard::onBoard(const string& square){
if (square[0] < 'A' || square[0] > 'H' || square[1] < '1' || square[1] > '8')
return false;
return true;
}
/* End of function*/
bool ChessBoard::isEmpty(const string& square){
map<string, Piece*>::iterator it;
it = board.find(square);
if (it == board.end())
return true;
return false;
}
/* End of function*/
bool ChessBoard::correctFormat(const string& square){
if (!isalpha(square[0]) || !isdigit(square[1]) || square.length() != 2){
cout << "Input has the wrong format. " //
<< "File: Uppercase Letter A-H. " //
<< "Rank: Digit 1-8." << endl;
return false;
}
return true;
}
/* End of function*/
string ChessBoard::stringColour(Colour colour){
if (colour == WHITE)
return "White";
return "Black";
}
/* End of function*/
string ChessBoard::stringPiece(Type type){
switch (type){
case 1: return "King";
case 2: return "Queen";
case 3: return "Rook";
case 4: return "Bishop";
case 5: return "Knight";
case 6: return "Pawn";
default: return "Piece";
}
}
/* End of function*/
Colour ChessBoard::getNextMove(){return next_move; }
string ChessBoard::findPiece(Type type, Colour colour){
for (auto it = board.begin() ; it != board.end() ; it++){
if (it->second->getType() == type &&
it->second->getColour() == colour)
return it->first;
}
cout << "No such piece on the board" << endl;
return "";
}
/* End of function*/
void ChessBoard::changeTurn(){
if (next_move == WHITE){
next_move = BLACK;
return;
}
next_move = WHITE;
return;
}
/* End of function*/
void ChessBoard::switchGameState(){game_over = true; }
/* END OF FILE*/