-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChess.cpp
642 lines (606 loc) · 19.4 KB
/
Chess.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#include <iostream>
#include <stdlib.h>
using namespace std;
class GamePiece
{
public:
GamePiece(char pieceColor) : pieceColor(pieceColor) {}
virtual ~GamePiece() {}
virtual char GetPiece() = 0;
char GetColor()
{
return pieceColor;
}
bool IsLegalMove(int srcRow, int srcCol, int destRow, int destCol, GamePiece *gameBoard[8][8])
{
GamePiece *destPiece = gameBoard[destRow][destCol];
if ((destPiece == 0) || (pieceColor != destPiece->GetColor()))
{
return AreSquaresLegal(srcRow, srcCol, destRow, destCol, gameBoard);
}
return false;
}
private:
virtual bool AreSquaresLegal(int srcRow, int srcCol, int destRow, int destCol, GamePiece *gameBoard[8][8]) = 0;
char pieceColor;
};
class PawnPiece : public GamePiece
{
public:
PawnPiece(char pieceColor) : GamePiece(pieceColor) {}
~PawnPiece() {}
private:
virtual char GetPiece()
{
return 'P';
}
bool AreSquaresLegal(int srcRow, int srcCol, int destRow, int destCol, GamePiece *gameBoard[8][8])
{
GamePiece *destPiece = gameBoard[destRow][destCol];
if (destPiece == 0)
{
// Destination square is unoccupied
if (srcCol == destCol)
{
if (GetColor() == 'W')
{
if (destRow == srcRow + 1)
{
return true;
}
}
else
{
if (destRow == srcRow - 1)
{
return true;
}
}
}
}
else
{
// Dest holds piece of opposite color
if ((srcCol == destCol + 1) || (srcCol == destCol - 1))
{
if (GetColor() == 'W')
{
if (destRow == srcRow + 1)
{
return true;
}
}
else
{
if (destRow == srcRow - 1)
{
return true;
}
}
}
}
return false;
}
};
class KnightPiece : public GamePiece
{
public:
KnightPiece(char pieceColor) : GamePiece(pieceColor) {}
~KnightPiece() {}
private:
virtual char GetPiece()
{
return 'N';
}
bool AreSquaresLegal(int srcRow, int srcCol, int destRow, int destCol, GamePiece *gameBoard[8][8])
{
GamePiece *destPiece = gameBoard[destRow][destCol];
// Knight moves in an L-shape: Check both movement conditions
bool isLShape =
((abs(srcCol - destCol) == 1) && (abs(srcRow - destRow) == 2)) ||
((abs(srcCol - destCol) == 2) && (abs(srcRow - destRow) == 1));
if (!isLShape)
{
return false;
}
// Check if the destination square is unoccupied or occupied by an opponent's piece
if (destPiece == nullptr || destPiece->GetColor() != this->GetColor())
{
return true;
}
return false;
}
};
class BishopPiece : public GamePiece
{
public:
BishopPiece(char pieceColor) : GamePiece(pieceColor) {}
~BishopPiece() {}
private:
virtual char GetPiece()
{
return 'B';
}
bool AreSquaresLegal(int srcRow, int srcCol, int destRow, int destCol, GamePiece *gameBoard[8][8])
{
// Check if the move is diagonal
if ((destCol - srcCol == destRow - srcRow) || (destCol - srcCol == srcRow - destRow))
{
// Make sure all intervening squares are empty
int rowOffset = (destRow - srcRow > 0) ? 1 : -1;
int colOffset = (destCol - srcCol > 0) ? 1 : -1;
int checkRow;
int checkCol;
for (checkRow = srcRow + rowOffset, checkCol = srcCol + colOffset;
checkRow != destRow;
checkRow = checkRow + rowOffset, checkCol = checkCol + colOffset)
{
if (gameBoard[checkRow][checkCol] != nullptr)
{
return false;
}
}
// Check if the destination square is empty or occupied by an opponent's piece
GamePiece* destPiece = gameBoard[destRow][destCol];
if (destPiece == nullptr || destPiece->GetColor() != this->GetColor())
{
return true;
}
}
// If the move is not diagonal or another condition failed, return false
return false;
}
};
class RookPiece : public GamePiece
{
public:
RookPiece(char pieceColor) : GamePiece(pieceColor) {}
~RookPiece() {}
private:
virtual char GetPiece()
{
return 'R';
}
bool AreSquaresLegal(int srcRow, int srcCol, int destRow, int destCol, GamePiece *gameBoard[8][8])
{
if (srcRow == destRow)
{
// Make sure that all intervening squares are empty
int colOffset = (destCol - srcCol > 0) ? 1 : -1;
for (int checkCol = srcCol + colOffset; checkCol != destCol; checkCol = checkCol + colOffset)
{
if (gameBoard[srcRow][checkCol] != 0)
{
return false;
}
}
return true;
}
else if (destCol == srcCol)
{
// Make sure that all intervening squares are empty
int rowOffset = (destRow - srcRow > 0) ? 1 : -1;
for (int checkRow = srcRow + rowOffset; checkRow != destRow; checkRow = checkRow + rowOffset)
{
if (gameBoard[checkRow][srcCol] != 0)
{
return false;
}
}
return true;
}
return false;
}
};
class QueenPiece : public GamePiece
{
public:
QueenPiece(char pieceColor) : GamePiece(pieceColor) {}
~QueenPiece() {}
private:
virtual char GetPiece()
{
return 'Q';
}
bool AreSquaresLegal(int srcRow, int srcCol, int destRow, int destCol, GamePiece *gameBoard[8][8])
{
if (srcRow == destRow)
{
// Make sure that all intervening squares are empty
int colOffset = (destCol - srcCol > 0) ? 1 : -1;
for (int checkCol = srcCol + colOffset; checkCol != destCol; checkCol = checkCol + colOffset)
{
if (gameBoard[srcRow][checkCol] != 0)
{
return false;
}
}
return true;
}
else if (destCol == srcCol)
{
// Make sure that all intervening squares are empty
int rowOffset = (destRow - srcRow > 0) ? 1 : -1;
for (int checkRow = srcRow + rowOffset; checkRow != destRow; checkRow = checkRow + rowOffset)
{
if (gameBoard[checkRow][srcCol] != 0)
{
return false;
}
}
return true;
}
else if ((destCol - srcCol == destRow - srcRow) || (destCol - srcCol == srcRow - destRow))
{
// Make sure that all intervening squares are empty
int rowOffset = (destRow - srcRow > 0) ? 1 : -1;
int colOffset = (destCol - srcCol > 0) ? 1 : -1;
int checkRow;
int checkCol;
for (checkRow = srcRow + rowOffset, checkCol = srcCol + colOffset;
checkRow != destRow;
checkRow = checkRow + rowOffset, checkCol = checkCol + colOffset)
{
if (gameBoard[checkRow][checkCol] != 0)
{
return false;
}
}
return true;
}
return false;
}
};
class KingPiece : public GamePiece
{
public:
KingPiece(char pieceColor) : GamePiece(pieceColor) {}
~KingPiece() {}
private:
virtual char GetPiece()
{
return 'K';
}
bool AreSquaresLegal(int srcRow, int srcCol, int destRow, int destCol, GamePiece *gameBoard[8][8])
{
int rowDelta = destRow - srcRow;
int colDelta = destCol - srcCol;
if (((rowDelta >= -1) && (rowDelta <= 1)) &&
((colDelta >= -1) && (colDelta <= 1)))
{
return true;
}
return false;
}
};
class ChessBoard
{
public:
ChessBoard()
{
for (int row = 0; row < 8; ++row)
{
for (int col = 0; col < 8; ++col)
{
mainGameBoard[row][col] = 0;
}
}
// Allocate and place black pieces
for (int col = 0; col < 8; ++col)
{
mainGameBoard[6][col] = new PawnPiece('B');
}
mainGameBoard[7][0] = new RookPiece('B');
mainGameBoard[7][1] = new KnightPiece('B');
mainGameBoard[7][2] = new BishopPiece('B');
mainGameBoard[7][3] = new KingPiece('B');
mainGameBoard[7][4] = new QueenPiece('B');
mainGameBoard[7][5] = new BishopPiece('B');
mainGameBoard[7][6] = new KnightPiece('B');
mainGameBoard[7][7] = new RookPiece('B');
// Allocate and place white pieces
for (int col = 0; col < 8; ++col)
{
mainGameBoard[1][col] = new PawnPiece('W');
}
mainGameBoard[0][0] = new RookPiece('W');
mainGameBoard[0][1] = new KnightPiece('W');
mainGameBoard[0][2] = new BishopPiece('W');
mainGameBoard[0][3] = new KingPiece('W');
mainGameBoard[0][4] = new QueenPiece('W');
mainGameBoard[0][5] = new BishopPiece('W');
mainGameBoard[0][6] = new KnightPiece('W');
mainGameBoard[0][7] = new RookPiece('W');
}
~ChessBoard()
{
for (int row = 0; row < 8; ++row)
{
for (int col = 0; col < 8; ++col)
{
delete mainGameBoard[row][col];
mainGameBoard[row][col] = 0;
}
}
}
void Print()
{
const int squareWidth = 4;
const int squareHeight = 3;
for (int row = 0; row < 8 * squareHeight; ++row)
{
int squareRow = row / squareHeight;
// Print side border with numbering
if (row % 3 == 1)
{
cout << '-' << (char)('1' + 7 - squareRow) << '-';
}
else
{
cout << "---";
}
// Print the chess board
for (int col = 0; col < 8 * squareWidth; ++col)
{
int squareCol = col / squareWidth;
if (((row % 3) == 1) && ((col % 4) == 1 || (col % 4) == 2) && mainGameBoard[7 - squareRow][squareCol] != 0)
{
if ((col % 4) == 1)
{
cout << mainGameBoard[7 - squareRow][squareCol]->GetColor();
}
else
{
cout << mainGameBoard[7 - squareRow][squareCol]->GetPiece();
}
}
else
{
if ((squareRow + squareCol) % 2 == 1)
{
cout << '*';
}
else
{
cout << ' ';
}
}
}
cout << endl;
}
// Print the bottom border with numbers
for (int row = 0; row < squareHeight; ++row)
{
if (row % 3 == 1)
{
cout << "---";
for (int col = 0; col < 8 * squareWidth; ++col)
{
int squareCol = col / squareWidth;
if ((col % 4) == 1)
{
cout << (squareCol + 1);
}
else
{
cout << '-';
}
}
cout << endl;
}
else
{
for (int col = 1; col < 9 * squareWidth; ++col)
{
cout << '-';
}
cout << endl;
}
}
}
bool IsInCheck(char pieceColor)
{
// Find the king
int kingRow;
int kingCol;
for (int row = 0; row < 8; ++row)
{
for (int col = 0; col < 8; ++col)
{
if (mainGameBoard[row][col] != 0)
{
if (mainGameBoard[row][col]->GetColor() == pieceColor)
{
if (mainGameBoard[row][col]->GetPiece() == 'K')
{
kingRow = row;
kingCol = col;
}
}
}
}
}
// Run through the opponent's pieces and see if any can take the king
for (int row = 0; row < 8; ++row)
{
for (int col = 0; col < 8; ++col)
{
if (mainGameBoard[row][col] != 0)
{
if (mainGameBoard[row][col]->GetColor() != pieceColor)
{
if (mainGameBoard[row][col]->IsLegalMove(row, col, kingRow, kingCol, mainGameBoard))
{
return true;
}
}
}
}
}
return false;
}
bool CanMove(char pieceColor)
{
// Run through all pieces
for (int row = 0; row < 8; ++row)
{
for (int col = 0; col < 8; ++col)
{
if (mainGameBoard[row][col] != 0)
{
// If it is a piece of the current player, see if it has a legal move
if (mainGameBoard[row][col]->GetColor() == pieceColor)
{
for (int moveRow = 0; moveRow < 8; ++moveRow)
{
for (int moveCol = 0; moveCol < 8; ++moveCol)
{
if (mainGameBoard[row][col]->IsLegalMove(row, col, moveRow, moveCol, mainGameBoard))
{
// Make move and check whether king is in check
GamePiece *tempPiece = mainGameBoard[moveRow][moveCol];
mainGameBoard[moveRow][moveCol] = mainGameBoard[row][col];
mainGameBoard[row][col] = 0;
bool canMove = !IsInCheck(pieceColor);
// Undo the move
mainGameBoard[row][col] = mainGameBoard[moveRow][moveCol];
mainGameBoard[moveRow][moveCol] = tempPiece;
if (canMove)
{
return true;
}
}
}
}
}
}
}
}
return false;
}
GamePiece *mainGameBoard[8][8];
};
class ChessGame
{
public:
ChessGame() : playerTurn('W') {}
~ChessGame() {}
void Start()
{
do
{
GetNextMove(gameBoard.mainGameBoard);
AlternateTurn();
} while (!IsGameOver());
gameBoard.Print();
}
void GetNextMove(GamePiece *gameBoard[8][8])
{
bool validMove = false;
do
{
system("clear");
cout << endl
<< endl
<< " Welcome to Chess Game" << endl
<< endl
<< endl;
cout << " Keys to symbols used " << endl
<< endl
<< endl;
cout << " * = white space" << endl;
cout << " Blank space = black space" << endl;
cout << " WP = White pawn & BP = Black pawn" << endl;
cout << " WN = White Knight & BN = Black Knight" << endl;
cout << " WB = White Bishop & BB = Black Bishop" << endl;
cout << " WR = White Rook & BR = Black Rook" << endl;
cout << " WQ = White Queen & BQ = Black Queen" << endl;
cout << " WK = White King & BK =Black King" << endl;
cout << "Rule for move is :" << endl;
cout << "Move by selecting row & column to another valid location using row & column" << endl
<< endl
<< endl;
this->gameBoard.Print();
// Get input and convert to coordinates
cout << playerTurn << "'s Move: ";
int startMove;
cin >> startMove;
int startRow = (startMove / 10) - 1;
int startCol = (startMove % 10) - 1;
cout << "To: ";
int endMove;
cin >> endMove;
int endRow = (endMove / 10) - 1;
int endCol = (endMove % 10) - 1;
// Check that the indices are in range
// and that the source and destination are different
if ((startRow >= 0 && startRow <= 7) &&
(startCol >= 0 && startCol <= 7) &&
(endRow >= 0 && endRow <= 7) &&
(endCol >= 0 && endCol <= 7))
{
// Additional checks in here
GamePiece *currentPiece = gameBoard[startRow][startCol];
// Check that the piece is the correct color
if ((currentPiece != 0) && (currentPiece->GetColor() == playerTurn))
{
// Check that the destination is a valid destination
if (currentPiece->IsLegalMove(startRow, startCol, endRow, endCol, gameBoard))
{
// Make the move
GamePiece *tempPiece = gameBoard[endRow][endCol];
gameBoard[endRow][endCol] = gameBoard[startRow][startCol];
gameBoard[startRow][startCol] = 0;
// Make sure that the current player is not in check
if (!this->gameBoard.IsInCheck(playerTurn))
{
delete tempPiece;
validMove = true;
}
else
{ // Undo the last move
gameBoard[startRow][startCol] = gameBoard[endRow][endCol];
gameBoard[endRow][endCol] = tempPiece;
}
}
}
}
if (!validMove)
{
cout << "Invalid Move!" << endl;
}
} while (!validMove);
}
void AlternateTurn()
{
playerTurn = (playerTurn == 'W') ? 'B' : 'W';
}
bool IsGameOver()
{
// Check that the current player can move
// If not, we have a stalemate or checkmate
bool canMove = gameBoard.CanMove(playerTurn);
if (!canMove)
{
if (gameBoard.IsInCheck(playerTurn))
{
AlternateTurn();
cout << "Checkmate, " << playerTurn << " Wins!" << endl;
}
else
{
cout << "Stalemate!" << endl;
}
}
return !canMove;
}
private:
ChessBoard gameBoard;
char playerTurn;
};
int main()
{
ChessGame game;
game.Start();
return 0;
}