-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess Program.cpp
More file actions
1219 lines (1148 loc) · 29.9 KB
/
Chess Program.cpp
File metadata and controls
1219 lines (1148 loc) · 29.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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
const int NUM_ROWS = 8;
const int NUM_COLS = 8;
string player1 = "";
string player2 = "";
// **************** CLASS: CELL *******************
class Cell {
char piece;
char color;
public:
Cell();
void place(char color, char piece);
string getPiece();
};
Cell::Cell() {
piece = ' ';
color = ' ';
}
void Cell::place(char newColor, char newPiece) {
assert((newColor == 'W') || (newColor == 'B') || (newColor == ' '));
color = newColor;
assert((newPiece == 'R') || (newPiece == 'K') ||
(newPiece == 'B') || (newPiece == 'Q') ||
(newPiece == 'K') || (newPiece == 'N') ||
(newPiece == 'P') || (newPiece == ' '));
piece = newPiece;
}
string Cell::getPiece() {
string result = "";
result = result.append(1, color);
result = result.append(1, piece);
return result;
}
// **************** CLASS: BOARD *******************
class Board {
Cell board[NUM_ROWS][NUM_COLS]; // <-- Not a good idea in the long run
char boardRowHeader[1][9];
void displayLine();
string take(int row, int col);
void place(int row, int col, char color, char piece);
bool movePieceOneStep(string piece, string direction, int &row, int &col);
bool cellEmpty(int toRow, int toCol, string piece);
bool moveRook(int &row, int &col, string direction, int steps);
bool movePawn(int &row, int &col, string direction, int steps);
bool moveBishop(int &row, int &col, string direction, int steps);
bool moveQueen(int &row, int &col, string direction, int steps);
bool moveKing(int &row, int &col, string direction, int steps);
bool moveKnight(int &row, int &col, string direction, int steps);
bool kingOnBoard(char color);
bool canTake(int row, int col, char color, string enemyPiece);
int captureCounter = 0;
public:
Board();
Board(string command, int row, int col, char color, char piece);
Board(string);
void displayBoard();
bool turn(int row, int col, string direction, int steps);
string look(int toRow, int toCol);
void buffer(string);
bool checkForCheck(char);
bool makeMove(char color);
};
Board::Board() {
boardRowHeader[0][0] = ' ';
boardRowHeader[0][1] = '0';
boardRowHeader[0][2] = '1';
boardRowHeader[0][3] = '2';
boardRowHeader[0][4] = '3';
boardRowHeader[0][5] = '4';
boardRowHeader[0][6] = '5';
boardRowHeader[0][7] = '6';
boardRowHeader[0][8] = '7';
board[0][0].place('B', 'R');
board[0][1].place('B', 'N');
board[0][2].place('B', 'B');
board[0][3].place('B', 'Q');
board[0][4].place('B', 'K');
board[0][5].place('B', 'B');
board[0][6].place('B', 'N');
board[0][7].place('B', 'R');
for (int c = 0; c < NUM_COLS; c++) {
board[1][c].place('B', 'P');
}
board[NUM_ROWS - 1][0].place('W', 'R');
board[NUM_ROWS - 1][1].place('W', 'N');
board[NUM_ROWS - 1][2].place('W', 'B');
board[NUM_ROWS - 1][4].place('W', 'K');
board[NUM_ROWS - 1][3].place('W', 'Q');
board[NUM_ROWS - 1][5].place('W', 'B');
board[NUM_ROWS - 1][6].place('W', 'N');
board[NUM_ROWS - 1][7].place('W', 'R');
for (int c = 0; c < NUM_COLS; c++) {
board[NUM_ROWS - 2][c].place('W', 'P');
}
}
Board::Board(string command, int row, int col, char color, char piece) {
if (command == "ONEPIECE") {
board[row][col].place(color, piece);
return;
}
else if (command == "NOPAWNS") {
boardRowHeader[0][0] = ' ';
boardRowHeader[0][1] = '0';
boardRowHeader[0][2] = '1';
boardRowHeader[0][3] = '2';
boardRowHeader[0][4] = '3';
boardRowHeader[0][5] = '4';
boardRowHeader[0][6] = '5';
boardRowHeader[0][7] = '6';
boardRowHeader[0][8] = '7';
board[0][0].place('B', 'R');
board[0][1].place('B', 'N');
board[0][2].place('B', 'B');
board[0][3].place('B', 'Q');
board[0][4].place('B', 'K');
board[0][5].place('B', 'B');
board[0][6].place('B', 'N');
board[0][7].place('B', 'R');
board[NUM_ROWS - 1][0].place('W', 'R');
board[NUM_ROWS - 1][1].place('W', 'N');
board[NUM_ROWS - 1][2].place('W', 'B');
board[NUM_ROWS - 1][4].place('W', 'K');
board[NUM_ROWS - 1][3].place('W', 'Q');
board[NUM_ROWS - 1][5].place('W', 'B');
board[NUM_ROWS - 1][6].place('W', 'N');
board[NUM_ROWS - 1][7].place('W', 'R');
}
else {
cout << "Invalid Command.";
assert(false);
}
}
Board::Board(string command) {
if (command == "NOPAWNS") {
boardRowHeader[0][0] = ' ';
boardRowHeader[0][1] = '0';
boardRowHeader[0][2] = '1';
boardRowHeader[0][3] = '2';
boardRowHeader[0][4] = '3';
boardRowHeader[0][5] = '4';
boardRowHeader[0][6] = '5';
boardRowHeader[0][7] = '6';
boardRowHeader[0][8] = '7';
board[0][0].place('B', 'R');
board[0][1].place('B', 'N');
board[0][2].place('B', 'B');
board[0][3].place('B', 'Q');
board[0][4].place('B', 'K');
board[0][5].place('B', 'B');
board[0][6].place('B', 'N');
board[0][7].place('B', 'R');
board[NUM_ROWS - 1][0].place('W', 'R');
board[NUM_ROWS - 1][1].place('W', 'N');
board[NUM_ROWS - 1][2].place('W', 'B');
board[NUM_ROWS - 1][4].place('W', 'K');
board[NUM_ROWS - 1][3].place('W', 'Q');
board[NUM_ROWS - 1][5].place('W', 'B');
board[NUM_ROWS - 1][6].place('W', 'N');
board[NUM_ROWS - 1][7].place('W', 'R');
}
}
void Board::buffer(string command) {
static Cell boardCopy[NUM_ROWS][NUM_COLS];
static Cell boardCopy2[NUM_ROWS][NUM_COLS];
if (command == "copy") {
// make a copy of the board
for (int rx = 0; rx < NUM_ROWS; rx++) {
for (int cx = 0; cx < NUM_COLS; cx++) {
boardCopy[rx][cx] = board[rx][cx];
}
}
}
else if (command == "paste") {
// put the copy back into the board
for (int rx = 0; rx < NUM_ROWS; rx++) {
for (int cx = 0; cx < NUM_COLS; cx++) {
board[rx][cx] = boardCopy[rx][cx];
}
}
}
else if (command == "copy2") {
// put the copy back into the board
for (int rx = 0; rx < NUM_ROWS; rx++) {
for (int cx = 0; cx < NUM_COLS; cx++) {
boardCopy2[rx][cx] = board[rx][cx];
}
}
}
else if (command == "paste2") {
// put the copy back into the board
for (int rx = 0; rx < NUM_ROWS; rx++) {
for (int cx = 0; cx < NUM_COLS; cx++) {
board[rx][cx] = boardCopy2[rx][cx];
}
}
}
else
assert(false); // only copy and paste are valid here
}
bool Board::kingOnBoard(char color) {
string foundKing;
if (color == 'W') {
for (int r = 0; r < NUM_ROWS; r++) {
for (int c = 0; c < NUM_COLS; c++) {
foundKing = look(r, c);
if (foundKing == "WK") { //
//cout << "found king at: " << r << " " << c << endl; //
return true;
} //
}
}
}
else if (color == 'B') {
for (int r = 0; r < NUM_ROWS; r++) {
for (int c = 0; c < NUM_COLS; c++) {
foundKing = look(r, c);
if (foundKing == "BK")
return true;
}
}
}
return false;
}
bool Board::canTake(int row, int col, char color, string enemyPiece) {
buffer("copy");
//cout << "Piece: " << enemyPiece << " (" << row << ", " << col << ")\n"; //TRACE
if (enemyPiece.at(1) == 'R') {
for (int steps = 1; steps < 8; steps++) {
if (turn(row, col, "N", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "S", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "E", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "W", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
}
}
else if (enemyPiece.at(1) == 'Q') {
for (int steps = 1; steps < 8; steps++) {
if (turn(row, col, "N", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
//displayBoard(); //QUEENTRACE
buffer("paste");
}
if (turn(row, col, "S", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "E", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "W", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "NE", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
//displayBoard(); //QUEENTRACE
buffer("paste");
}
if (turn(row, col, "NW", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "SE", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
//displayBoard(); //QUEENTRACE
buffer("paste");
}
if (turn(row, col, "SW", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
}
}
else if (enemyPiece == "BP") {
if (turn(row, col, "SE", 1)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "SW", 1)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
}
else if (enemyPiece == "WP") {
if (turn(row, col, "NE", 1)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "NW", 1)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
}
else if (enemyPiece.at(1) == 'B') {
for (int steps = 1; steps < 8; steps++) {
if (turn(row, col, "NE", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "NW", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "SE", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "SW", steps)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
}
}
else if (enemyPiece.at(1) == 'N') {
if (turn(row, col, "NNE", 1)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "NNW", 1)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "SSE", 1)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
buffer("paste");
}
if (turn(row, col, "SSW", 1)) {
if (!(kingOnBoard(color))) {
buffer("paste");
return true;
}
}
}
buffer("paste");
return false;
}
bool Board::checkForCheck(char color) {
string enemyPiece;
if (color == 'W') {
for (int r = 0; r < NUM_ROWS; r++) {
for (int c = 0; c < NUM_COLS; c++) {
enemyPiece = look(r, c);
if (enemyPiece.at(0) == 'B') {
if ((canTake(r, c, color, enemyPiece))) {
cout << "White is in check! You can not move into or stay in check!" << endl;
return true;
}
}
}
}
}
if (color == 'B') {
for (int r = 0; r < NUM_ROWS; r++) {
for (int c = 0; c < NUM_COLS; c++) {
enemyPiece = look(r, c);
if (enemyPiece.at(0) == 'W') {
if ((canTake(r, c, color, enemyPiece))) {
cout << "Black is in check! You can not move into or stay in check!" << endl;
return true;
}
}
}
}
}
return false;
}
void Board::displayLine() {
cout << endl;
for (int x = 0; x <= NUM_COLS; x++) {
cout << " | ";
}
cout << endl;
for (int x = 0; x <= NUM_COLS; x++) {
cout << "----| ";
}
cout << endl;
}
void Board::displayBoard() {
cout << endl << "CURRENT BOARD:" << endl << endl;
//row header
for (int r = 0; r < 1; r++) {
for (int c = 0; c <= NUM_COLS; c++) {
cout << " " << boardRowHeader[r][c] << " | ";
}
displayLine();
}
for (int r = 0; r < NUM_ROWS; r++) {
if (r == 0) {
cout << 'A' << " |"; //column header
}
else if (r == 1) {
cout << 'B' << " |";
}
else if (r == 2) {
cout << 'C' << " |";
}
else if (r == 3) {
cout << 'D' << " |";
}
else if (r == 4) {
cout << 'E' << " |";
}
else if (r == 5) {
cout << 'F' << " |";
}
else if (r == 6) {
cout << 'G' << " |";
}
else if (r == 7) {
cout << 'H' << " |";
}
for (int c = 0; c < NUM_COLS; c++) {
cout << " " << board[r][c].getPiece() << " |";
}
displayLine();
}
cout << endl << endl;
}
string Board::take(int row, int col) {
if (row <= NUM_ROWS - 1 && col <= NUM_COLS) {
string pieceToTake;
pieceToTake = board[row][col].getPiece();
board[row][col].place(' ', ' ');
return pieceToTake;
}
}
void Board::place(int row, int col, char color, char piece) {
if (row <= 7 && col <= 7) {
board[row][col].place(color, piece);
}
else {
cout << "Out of bounds!";
}
}
string Board::look(int row, int col) {
//assert((row >= 0) && (row < NUM_ROWS));
if ((row <= 0) && (row > NUM_ROWS))
return false;
//assert((col >= 0) && (col < NUM_COLS));
if ((col <= 0) && (col > NUM_COLS))
return false;
return board[row][col].getPiece();
}
bool Board::cellEmpty(int toRow, int toCol, string piece) {
//assert((toRow >= 0) && (toRow < NUM_ROWS));
if ((toRow < 0) || (toRow >= NUM_ROWS))
return false;
//assert((toCol >= 0) && (toCol < NUM_COLS));
if ((toCol < 0) || (toCol >= NUM_ROWS))
return false;
char pieceMovingColor = piece.at(0);
string pieceAtSpot = look(toRow, toCol);
if (look(toRow, toCol) == " ") {
return true;
}
else if ((pieceMovingColor != pieceAtSpot.at(0))) {
captureCounter++;
return true;
}
return false;
}
bool Board::moveRook(int &row, int &col, string direction, int steps) {
string piece = look(row, col);
//assert(piece.at(1) == 'R');
if (piece.at(1) != 'R') {
return false;
}
if (!((direction == "N") || (direction == "S") || (direction == "W") || (direction == "E"))) {
cout << "Rooks can not move " << direction << "!" << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col))
return false;
}
return true;
}
bool Board::moveBishop(int &row, int &col, string direction, int steps) {
string piece = look(row, col);
//assert(piece.at(1) == 'B');
if (piece.at(1) != 'B') {
return false;
}
if (!((direction == "NW") || (direction == "SW") || (direction == "NE") || (direction == "SE"))) {
cout << "Bishops can not move " << direction << "!" << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col))
return false;
}
return true;
}
bool Board::moveQueen(int &row, int &col, string direction, int steps) {
string piece = look(row, col);
//assert(piece.at(1) == 'Q');
if (piece.at(1) != 'Q') {
return false;
}
if (!((direction == "NW") || (direction == "SW") || (direction == "NE") || (direction == "SE") ||
(direction == "N") || (direction == "S") || (direction == "W") || (direction == "E"))) {
cout << "Queens can not move " << direction << "!" << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col))
return false;
}
return true;
}
bool Board::moveKing(int &row, int &col, string direction, int steps) {
string piece = look(row, col);
//assert(piece.at(1) == 'K');
if (piece.at(1) != 'K') {
return false;
}
if (!((direction == "NW") || (direction == "SW") || (direction == "NE") || (direction == "SE") ||
(direction == "N") || (direction == "S") || (direction == "W") || (direction == "E"))) {
cout << "Kings can not move " << direction << "!" << endl;
return false;
}
else if (steps == 1) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
else {
cout << "Kings can not move more than one space at a time!" << endl;
return false;
}
return true;
}
bool Board::moveKnight(int &row, int &col, string direction, int steps) {
string piece = look(row, col);
//assert(piece.at(1) == 'N');
if (piece.at(1) != 'N') {
return false;
}
if (!((direction == "NNW") || (direction == "NWW") || (direction == "NNE") || (direction == "SEE") ||
(direction == "NEE") || (direction == "SSE") || (direction == "SWW") || (direction == "SSW"))) {
cout << "Knights can not move " << direction << "!" << endl;
return false;
}
else if (steps == 1) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
else {
cout << "Knights can only move one space at a time (In the shape of an L)!" << endl;
return false;
}
return true;
}
bool Board::movePawn(int &row, int &col, string direction, int steps) {
string piece = look(row, col);
//assert(piece.at(1) == 'P');
if (piece.at(1) != 'P') {
return false;
}
string attackCheckString;
char attackCheckChar;
if (steps > 2) {
cout << "Pawns cannot move " << steps << " steps at a time!" << endl;
return false;
}
if (piece.at(0) == 'B') {
if (!((direction == "S") || (direction == "SW") || (direction == "SE"))) {
cout << "Black Pawns can not move " << direction << "!" << endl;
return false;
}
else if ((row == 1) && (steps <= 2)) {
if (direction == "SW") {
attackCheckString = look(row + 1, col - 1);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != 'W') {
//cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
else if (direction == "SE") {
attackCheckString = look(row + 1, col + 1);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != 'W') {
//cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
else if (direction == "S") {
attackCheckString = look(row + 1, col);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != ' ') {
cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
}
else if ((row != 1) && (steps == 1)) {
if (direction == "SW") {
attackCheckString = look(row + 1, col - 1);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != 'W') {
//cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
else if (direction == "SE") {
attackCheckString = look(row + 1, col + 1);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != 'W') {
//cout << "Pawns can only attack on a diagonal!" << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
else if (direction == "S") {
attackCheckString = look(row + 1, col);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != ' ') {
cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
}
else {
cout << "Pawns can only move 2 steps on their first move!" << endl << endl;
return false;
}
}
else if (piece.at(0) == 'W') {
if (!((direction == "N") || (direction == "NW") || (direction == "NE"))) {
cout << "White Pawns can not move " << direction << "!" << endl;
return false;
}
else if ((row == 6) && (steps <= 2)) {
if (direction == "NW") {
attackCheckString = look(row - 1, col - 1);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != 'B') {
//cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
else if (direction == "NE") {
attackCheckString = look(row - 1, col + 1);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != 'B') {
//cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
else if (direction == "N") {
attackCheckString = look(row - 1, col);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != ' ') {
cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
}
else if ((row != 6) && (steps == 1)) {
if (direction == "NW") {
attackCheckString = look(row - 1, col - 1);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != 'B') {
//cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
else if (direction == "NE") {
attackCheckString = look(row - 1, col + 1);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != 'B') {
//cout << "Pawns can only attack on a diagonal!" << endl << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
else if (direction == "N") {
attackCheckString = look(row - 1, col);
attackCheckChar = attackCheckString.at(0);
if (attackCheckChar != ' ') {
cout << "Pawns can only attack on a diagonal!" << endl;
return false;
}
for (int x = 0; x < steps; x++) {
if (!movePieceOneStep(piece, direction, row, col)) {
return false;
}
}
}
}
else {
cout << "Pawns can only move 2 steps on their first move!" << endl << endl;
return false;
}
}
}
bool Board::movePieceOneStep(string piece, string direction,
int &row, int &col) {
//assert((row >= 0) && (row < NUM_ROWS));
if ((row < 0) || (row >= NUM_ROWS)) {
return false;
}
//assert((col >= 0) && (col < NUM_COLS));
if ((col < 0) || (col >= NUM_COLS)) {
return false;
}
int toRow = row;
int toCol = col;
if (direction == "S")
toRow = row + 1;
else if (direction == "N")
toRow = row - 1;
else if (direction == "E")
toCol = col + 1;
else if (direction == "W")
toCol = col - 1;
else if (direction == "NW") {
toRow = row - 1;
toCol = col - 1;
}
else if (direction == "NE") {
toRow = row - 1;
toCol = col + 1;
}
else if (direction == "SW") {
toRow = row + 1;
toCol = col - 1;
}
else if (direction == "SE") {
toRow = row + 1;
toCol = col + 1;
}
else if (direction == "NNW") {
toRow = row - 2;
toCol = col - 1;
}
else if (direction == "NWW") {
toRow = row - 1;
toCol = col - 2;
}
else if (direction == "NNE") {
toRow = row - 2;
toCol = col + 1;
}
else if (direction == "NEE") {
toRow = row - 1;
toCol = col + 2;
}
else if (direction == "SWW") {
toRow = row + 1;
toCol = col - 2;
}
else if (direction == "SSW") {
toRow = row + 2;
toCol = col - 1;
}
else if (direction == "SSE") {
toRow = row + 2;
toCol = col + 1;
}
else if (direction == "SEE") {
toRow = row + 1;
toCol = col + 2;
}
else {
cout << "INVALID DIRECTION!" << endl;
assert(false); // force a failure
}
//assert((toRow >= 0) && (toRow < NUM_ROWS));
if ((toRow < 0) || (toRow >= NUM_ROWS)) {
return false;
}
//assert((toCol >= 0) && (toCol < NUM_COLS));
if ((toCol < 0) || (toCol >= NUM_COLS)) {
return false;
}
if (!cellEmpty(toRow, toCol, piece)) {
/*cout << "Space [" << toRow << ", " << toCol <<
"] Contains [" << look(toRow, toCol) << "]" << endl;*/
return false;
}
else if (captureCounter >= 2) {
captureCounter = 0;
//cout << "many captures";
return false;
}
else {
piece = take(row, col);
place(toRow, toCol, piece.at(0), piece.at(1));
row = toRow;
col = toCol;
return true;
}
}
bool Board::turn(int row, int col, string direction, int steps) {
captureCounter = 0;
//assert((row >= 0) && (row < NUM_ROWS));
if ((row < 0) || (row >= NUM_ROWS)) {
return false;
}
//assert((col >= 0) && (col < NUM_COLS));