-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
746 lines (625 loc) · 18.9 KB
/
Copy pathgame.cpp
File metadata and controls
746 lines (625 loc) · 18.9 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
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#include <iostream>
#include <string>
using namespace std;
enum Color { WHITE, BLACK };
enum Piece { PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING };
class BoardSquare
{
private:
int rank;
int fileNumber;
char file;
public:
bool isEmpty;
BoardSquare();
void setRank(int r) { rank = r; }
void setFileNumber(int); //To Set File Number and Derive the File-Character
int getRank() { return rank; }
int getFileNumber() { return fileNumber; }
char getFile() { return file; }
};
class ChessPiece : public BoardSquare
{
protected:
Piece id;
Color color;
string name;
public:
bool pieceMoved; //To Check if the Piece is Once Moved (For Pawn, King, Rook)
bool captured; //To Check if the Piece is Captured
ChessPiece();
void setID(Piece); //To Set ID and Derive the Piece-Name
void setColor(Color c) { color = c; }
Piece getID() { return id; }
Color getColor() { return color; }
string getName() { return name; }
virtual bool Move(); //To Move the ChessPiece
};
class Pawn : public ChessPiece
{
public:
Pawn() { id = PAWN; name = "Pawn"; }
bool Move();
}
pawn[16];
class Knight : public ChessPiece
{
public:
Knight() { id = KNIGHT; name = "Knight"; }
bool Move();
}
knight[4];
class Bishop : public ChessPiece
{
public:
Bishop() { id = BISHOP; name = "Bishop"; }
bool Move();
}
bishop[4];
class Rook : public ChessPiece
{
public:
Rook() { id = ROOK; name = "Rook"; }
bool Move();
}
rook[4];
class Queen : public ChessPiece
{
public:
Queen() { id = QUEEN; name = "Queen"; }
bool Move();
}
queen[2];
class King : public ChessPiece
{
public:
King() { id = KING; name = "King"; }
bool Move();
}
king[2];
BoardSquare square[8][8]; //ChessBoard Squares
ChessPiece emptyPiece; //An Absence of Chesspiece
void initBoard(); //Initialize the ChessBoard
void PrintBoard(Color); //Print the ChessBoard
char DisplayPiece(int,int); //Display the ChessPiece
ChessPiece* getPiece(int r, int f); //To get the Piece present on a certain BoardSquare
void Game(); //The Main Game-Sequence
bool turn(); //Player's Turn
bool over(); //To Check if the Game is Over
bool QuitGame(); //The Quit Game Option
void PlayAgain(); //The PlayAgain Option
Color player = WHITE; //Default First-Turn
bool gameOver; //Check If the Game is Over
char playAgain = 'Y'; //The Default PlayAgain Option
int sourceRank,sourceFile, destinedRank,destinedFile; //The Player Inputs, for Source & Destined Rank/File
ChessPiece* ownPiece; //The Player's Input Source-Piece
BoardSquare *sourceSquare,*destinedSquare; //The Player-Input's Source & Destined Squares
int rankedSteps,filedSteps; //The Difference between Squares's Ranks/Files
int main()
{
cout << "\n Welcome to the Haseebullah's Chess Game! \n"
<< "You are Advised to Consult the Readme File, before Playing the Game. \n"
<< endl;
cout << "On your Turn, Enter the Index (File & Rank) of Your Piece "
<< "and the Index (File & Rank) of the Destined-Square. " << endl;
do {
initBoard();
Game();
PlayAgain();
}
while(playAgain == 'Y');
cout << endl << endl;
cout << "Thanks for Playing. Hope You Enjoyed the Game! \n";
cout << "It would be very Kind of you, if you give us an Honest Feedback. \n\n";
cout << "You can Find me in GitHub https://github.com/Haseebullah9012. \n";
getchar();
return 0;
}
BoardSquare::BoardSquare()
{
isEmpty = true; //All Squares are Empty atFirst
}
ChessPiece::ChessPiece()
{
pieceMoved = false;
captured = false;
}
void BoardSquare::setFileNumber(int f)
{
fileNumber = f;
file = fileNumber-1 + 'a';
}
void ChessPiece::setID(Piece p)
{
id = p;
switch (id)
{
case PAWN: name = "Pawn"; break;
case KNIGHT: name = "Knight"; break;
case BISHOP: name = "Bishop"; break;
case ROOK: name = "Rook"; break;
case QUEEN: name = "Queen"; break;
case KING: name = "King"; break;
}
}
void initBoard()
{
//Chess-Pieces Color & Board-Setup
for(int i=0; i<16; i++)
{
if(i<8) {
pawn[i].setColor(WHITE);
pawn[i].setRank(2);
pawn[i].setFileNumber(i+1);
}
else {
pawn[i].setColor(BLACK);
pawn[i].setRank(7);
pawn[i].setFileNumber(i-8+1);
}
if(i<4)
{
if(i<2) {
knight[i].setColor(WHITE);
bishop[i].setColor(WHITE);
rook[i].setColor(WHITE);
knight[i].setRank(1);
bishop[i].setRank(1);
rook[i].setRank(1);
}
else {
knight[i].setColor(BLACK);
bishop[i].setColor(BLACK);
rook[i].setColor(BLACK);
knight[i].setRank(8);
bishop[i].setRank(8);
rook[i].setRank(8);
}
if(i%2==0) {
rook[i].setFileNumber(1);
knight[i].setFileNumber(2);
bishop[i].setFileNumber(3);
}
else {
bishop[i].setFileNumber(6);
knight[i].setFileNumber(7);
rook[i].setFileNumber(8);
}
}
if(i<2)
{
if(i==0) {
queen[i].setColor(WHITE);
king[i].setColor(WHITE);
queen[i].setRank(1);
king[i].setRank(1);
}
else {
queen[i].setColor(BLACK);
king[i].setColor(BLACK);
queen[i].setRank(8);
king[i].setRank(8);
}
queen[i].setFileNumber(4);
king[i].setFileNumber(5);
}
}
//The Absence of any Piece on the BoadSquare
emptyPiece.setRank(0);
emptyPiece.setFileNumber(0);
//Chess-Pieces Squares are Now Occupied
for(int rw=0,rb=6; rw<2 && rb<8; rw++,rb++) {
for(int f=0; f<8; f++) {
square[rw][f].isEmpty = false;
square[rb][f].isEmpty = false;
}
}
}
void Game()
{
while(true)
{
PrintBoard(player);
if(player == WHITE)
cout << "White's Turn: ";
else
cout << "Black's Turn: ";
//While the Player's Input is Invalid, Continue Inputing the Move
while(!turn())
cout << "Move Again: ";
if(gameOver)
break;
//Next Turn
if(player == WHITE)
player = BLACK;
else
player = WHITE;
}
cout << endl << endl;
}
void PrintBoard(Color player)
{
cout << endl << endl;
if(player == WHITE)
{
cout << " a b c d e f g h \n";
for(int r=7; r>=0; r--) {
cout << " --- --- --- --- --- --- --- --- \n";
cout << " " << r+1 << " ";
for(int f=0; f<8; f++) {
cout << " | " << DisplayPiece(r,f); /* Print Square Here */
}
cout << " | " << " " << r+1 << " \n";
}
cout << " --- --- --- --- --- --- --- --- \n";
cout << " a b c d e f g h \n";
}
else if(player == BLACK)
{
cout << " h g f e d c b a \n";
for(int r=0; r<8; r++) {
cout << " --- --- --- --- --- --- --- --- \n";
cout << " " << r+1 << " ";
for(int f=7; f>=0; f--) {
cout << " | " << DisplayPiece(r,f); /* Print Square Here */
}
cout << " | " << " " << r+1 << " \n";
}
cout << " --- --- --- --- --- --- --- --- \n";
cout << " h g f e d c b a \n";
}
cout << "\n^!_!^\n";
}
char DisplayPiece(int r, int f)
{
ChessPiece* piece = getPiece(r+1,f+1);
string name = piece->getName();
if(name == "Knight")
name = "Night"; //K-Silent (Knight represented by N)
if(!square[r][f].isEmpty) {
if(piece->getColor() == WHITE)
return toupper(name[0]);
else if(piece->getColor() == BLACK)
return tolower(name[0]);
}
else {
return ' '; //Empty Square
}
return '-'; //Non-Executable
}
ChessPiece* getPiece(int r, int f)
{
for(int i=0; i<16; i++)
{
if(pawn[i].getRank()==r && pawn[i].getFileNumber()==f)
return &pawn[i];
if(i<4)
{
if(knight[i].getRank()==r && knight[i].getFileNumber()==f)
return &knight[i];
else if(bishop[i].getRank()==r && bishop[i].getFileNumber()==f)
return &bishop[i];
else if(rook[i].getRank()==r && rook[i].getFileNumber()==f)
return &rook[i];
}
if(i<2)
{
if(queen[i].getRank()==r && queen[i].getFileNumber()==f)
return &queen[i];
else if(king[i].getRank()==r && king[i].getFileNumber()==f)
return &king[i];
}
}
return &emptyPiece;
}
bool turn()
{
char sourceFile_char,destinedFile_char;
cin >> sourceFile_char >> sourceRank >> destinedFile_char >> destinedRank;
if(sourceFile_char=='-' && sourceRank==1 || destinedFile_char=='-' && destinedRank==1)
return QuitGame();
if(cin.fail()) {
cin.clear(); cin.ignore(255, '\n');
cout << "Invalid Input! ";
return false;
}
else if(sourceFile_char<'a'||sourceFile_char>'h' || destinedFile_char<'a'||destinedFile_char>'h') {
cout << "Invalid File Character! ";
return false;
}
else if(sourceRank<1||sourceRank>8 || destinedRank<1||destinedRank>8) {
cout << "Invalid Rank Number! ";
return false;
}
//Set File & Rank for Array-Index
sourceFile = (sourceFile_char - 'a')+1;
destinedFile = (destinedFile_char - 'a')+1;
sourceSquare = &square[sourceRank-1][sourceFile-1];
destinedSquare = &square[destinedRank-1][destinedFile-1];
if(sourceSquare->isEmpty) {
cout << "There's No Piece! ";
return false;
}
ownPiece = getPiece(sourceRank,sourceFile);
if(ownPiece->getColor() != player) {
cout << "That's Not Your Piece! ";
return false;
}
else if(!destinedSquare->isEmpty) {
ChessPiece* targetPiece = getPiece(destinedRank,destinedFile);
if(targetPiece->getColor() == player) {
cout << "Invalid Move! Cannot Land on Own Piece. ";
return false;
}
}
return ownPiece->ChessPiece::Move();
}
bool ChessPiece::Move()
{
rankedSteps = destinedRank-sourceRank;
filedSteps = destinedFile-sourceFile;
bool valid = this->Move();
if(valid) {
if(!destinedSquare->isEmpty) {
ChessPiece* targetPiece = getPiece(destinedRank,destinedFile);
targetPiece->setRank(0);
targetPiece->setFileNumber(0);
targetPiece->captured = true;
//King Captured
if(targetPiece->getName() == "King") {
cout << "King Captured. ";
if(player == WHITE)
cout << "White Won! ";
else
cout << "Black Won! ";
cout << endl;
gameOver = true;
}
}
ownPiece->setRank(destinedRank);
ownPiece->setFileNumber(destinedFile);
sourceSquare->isEmpty = true;
destinedSquare->isEmpty = false;
}
else
cout << "Invalid Move! " << name << " Cannot Move Like This. ";
if(ownPiece->getName()=="Pawn" && (ownPiece->getRank() == 8 || ownPiece->getRank() == 1)) {
char p;
cout << "Pawn Moved to the Last-Rank. ";
do {
cout << "Promote Pawn (N:Knight, B:Bishop, R:Rook, Q:Queen): ";
cin >> p;
cin.ignore(255, '\n');
p = toupper(p);
if(p=='N')
ownPiece->setID(KNIGHT);
else if(p=='B')
ownPiece->setID(BISHOP);
else if(p=='R')
ownPiece->setID(ROOK);
else if(p=='Q')
ownPiece->setID(QUEEN);
else
p = '-';
}
while(p=='-');
}
return valid;
}
bool Pawn::Move()
{
bool valid;
if(player==BLACK)
rankedSteps = -rankedSteps;
bool stepStraight = (filedSteps==0 && rankedSteps==1) && destinedSquare->isEmpty;
bool stepDiagonal = (abs(filedSteps)==1 && rankedSteps==1) && (!destinedSquare->isEmpty);
bool doubleStep = (filedSteps==0 && rankedSteps==2) && !ownPiece->pieceMoved;
if(stepStraight || stepDiagonal)
valid = true;
else if(doubleStep)
{
valid = true;
int r=1;
if(player==BLACK)
r=-1;
if(!square[sourceRank+r-1][sourceFile-1].isEmpty)
valid = false;
}
else
valid = false;
if(valid && !ownPiece->pieceMoved)
ownPiece->pieceMoved = true;
return valid;
}
bool Knight::Move()
{
bool valid;
bool rankedMovement = abs(rankedSteps)==2 && abs(filedSteps)==1;
bool filedMovement = abs(filedSteps)==2 && abs(rankedSteps)==1;
if(rankedMovement || filedMovement)
valid = true;
else
valid = false;
return valid;
}
bool Bishop::Move()
{
bool valid;
bool diagonalMovement = abs(rankedSteps)==abs(filedSteps);
if(diagonalMovement)
{
valid = true;
int stepR = rankedSteps/abs(rankedSteps);
int stepF = filedSteps/abs(filedSteps);
for(int r=stepR,f=stepF; abs(r)<abs(rankedSteps); r+=stepR,f+=stepF) {
if(!square[sourceRank+r-1][sourceFile+f-1].isEmpty) {
valid = false;
break;
}
}
}
else
return false;
return valid;
}
bool Rook::Move()
{
bool valid;
bool rankedMovement = destinedFile==sourceFile;
bool filedMovement = destinedRank==sourceRank;
if(rankedMovement)
{
valid = true;
int step = rankedSteps/abs(rankedSteps);
for(int r=step; abs(r)<abs(rankedSteps); r+=step) {
if(!square[sourceRank+r-1][sourceFile-1].isEmpty) {
valid = false;
break;
}
}
}
else if(filedMovement)
{
valid = true;
int step = filedSteps/abs(filedSteps);
for(int f=step; abs(f)<abs(filedSteps); f+=step) {
if(!square[sourceRank-1][sourceFile+f-1].isEmpty) {
valid = false;
break;
}
}
}
else
return false;
if(valid && !ownPiece->pieceMoved)
ownPiece->pieceMoved = true;
return valid;
}
bool Queen::Move()
{
bool valid;
bool diagonalMovement = abs(rankedSteps)==abs(filedSteps);
bool rankedMovement = destinedFile==sourceFile;
bool filedMovement = destinedRank==sourceRank;
if(rankedMovement)
{
valid = true;
int step = rankedSteps/abs(rankedSteps);
for(int r=step; abs(r)<abs(rankedSteps); r+=step) {
if(!square[sourceRank+r-1][sourceFile-1].isEmpty) {
valid = false;
break;
}
}
}
else if(filedMovement)
{
valid = true;
int step = filedSteps/abs(filedSteps);
for(int f=step; abs(f)<abs(filedSteps); f+=step) {
if(!square[sourceRank-1][sourceFile+f-1].isEmpty) {
valid = false;
break;
}
}
}
else if(diagonalMovement)
{
valid = true;
int stepR = rankedSteps/abs(rankedSteps);
int stepF = filedSteps/abs(filedSteps);
for(int r=stepR,f=stepF; abs(r)<abs(rankedSteps); r+=stepR,f+=stepF) {
if(!square[sourceRank+r-1][sourceFile+f-1].isEmpty) {
valid = false;
break;
}
}
}
else
valid = false;
return valid;
}
bool King::Move()
{
bool valid;
bool stepMovement = abs(rankedSteps)==1 || abs(filedSteps)==1;
bool castleMovement = (rankedSteps==0 && abs(filedSteps)==2) && !ownPiece->pieceMoved;
if(stepMovement)
valid = true;
else if(castleMovement) {
valid = true;
int f;
if(filedSteps==2)
f=1; //KingSide
else
f=-1; //QueenSide
if(!square[sourceRank-1][sourceFile+f-1].isEmpty)
valid = false; //King's Neighbor Squares
else {
//Rook's CastleMovement (Rook's actually Moved Here)
if(filedSteps==2) {
if(player==WHITE && !rook[1].pieceMoved)
rook[1].setFileNumber(6); //KingSide WhiteRook
else if(player==BLACK && !rook[3].pieceMoved)
rook[3].setFileNumber(6); //KingSide BlackRook
else
valid = false; //Rook has Moved Earlier
if(valid) {
square[sourceRank-1][7].isEmpty = true;
square[sourceRank-1][5].isEmpty = false;
}
}
else {
f=-3;
if(!square[sourceRank-1][sourceFile+f-1].isEmpty)
valid = false; //Queenside Rook's Neighbor Squares
else {
if(player==WHITE && !rook[0].pieceMoved)
rook[0].setFileNumber(4); //QueenSide WhiteRook
else if(player==BLACK && !rook[2].pieceMoved)
rook[2].setFileNumber(4); //QueenSide BlackRook
else
valid = false;
if(valid) {
square[sourceRank-1][0].isEmpty = true;
square[sourceRank-1][3].isEmpty = false;
}
}
}
}
}
else
valid = false;
if(valid && !ownPiece->pieceMoved)
ownPiece->pieceMoved = true;
return valid;
}
bool QuitGame()
{
char quit;
cout << "Do You Really Want to Quit the Game (Y/N): ";
cin >> quit;
cin.ignore(255, '\n');
quit = toupper(quit);
if(quit == 'Y') {
cout << " Game Withdraw! \n\n";
gameOver = true;
return true;
}
else {
cout << "Game Continue! ";
return false;
}
}
void PlayAgain()
{
cout << "Do You Want to Play Again (Y/N): ";
cin >> playAgain;
cin.ignore(255, '\n');
playAgain = toupper(playAgain);
if(playAgain != 'Y' && playAgain != 'N') {
cout << " Oops! Its not a legal Response. \n\n";
cout << "Again, ";
PlayAgain();
}
}