-
Notifications
You must be signed in to change notification settings - Fork 2
/
board.cc
804 lines (705 loc) · 26.5 KB
/
board.cc
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
#include <iostream>
#include "board.h"
#include "bishop.h"
#include "queen.h"
#include "king.h"
#include "pawn.h"
#include "rook.h"
#include "knight.h"
#include "levelOne.h"
#include "levelTwo.h"
#include "levelThree.h"
#include "levelFour.h"
using namespace std;
Board::Board(): board{}, td{new TextDisplay()} {
wd = new Xwindow(500, 500);
gd = new GraphicsDisplay(*wd);
}
Board::~Board() {
delete whitePlayer;
delete blackPlayer;
delete wd;
delete td;
delete gd;
}
void Board::clearBoard() {
for (int i = 0; i < boardSize; ++i) {
for (int j = 0; j < boardSize; ++j) {
delete board[i][j];
td->notify(nullptr, i, j);
gd->notify(nullptr, i, j);
}
}
whiteKing = nullptr;
blackKing = nullptr;
whitePieces.clear();
blackPieces.clear();
board.clear(); // Clear the board.
}
void Board::init(){
board.resize(boardSize, vector<Piece *>(boardSize));
for (int r = 0; r < boardSize; ++r) {
board.resize(boardSize);
for (int c = 0; c < boardSize; ++c) {
board[r][c] = nullptr;
}
}
// Setup with default mode.
placeNewPiece(PieceType::Rook, Colour::WHITE, "a1");
placeNewPiece(PieceType::Knight, Colour::WHITE, "b1");
placeNewPiece(PieceType::Bishop, Colour::WHITE, "c1");
placeNewPiece(PieceType::Queen, Colour::WHITE, "d1");
placeNewPiece(PieceType::King, Colour::WHITE, "e1");
placeNewPiece(PieceType::Bishop, Colour::WHITE, "f1");
placeNewPiece(PieceType::Knight, Colour::WHITE, "g1");
placeNewPiece(PieceType::Rook, Colour::WHITE, "h1");
for (int i = 0; i < 8; ++i) {
placeNewPiece(PieceType::Pawn, Colour::WHITE, 6, i);
}
placeNewPiece(PieceType::Rook, Colour::BLACK, "a8");
placeNewPiece(PieceType::Knight, Colour::BLACK, "b8");
placeNewPiece(PieceType::Bishop, Colour::BLACK, "c8");
placeNewPiece(PieceType::Queen, Colour::BLACK, "d8");
placeNewPiece(PieceType::King, Colour::BLACK, "e8");
placeNewPiece(PieceType::Bishop, Colour::BLACK, "f8");
placeNewPiece(PieceType::Knight, Colour::BLACK, "g8");
placeNewPiece(PieceType::Rook, Colour::BLACK, "h8");
for (int i = 0; i < 8; ++i) {
placeNewPiece(PieceType::Pawn, Colour::BLACK, 1, i);
}
}
void Board::initEmpty() {
board.resize(boardSize, vector<Piece *>(boardSize));
for (int r = 0; r < boardSize; ++r) {
board.resize(boardSize);
for (int c = 0; c < boardSize; ++c) {
board[r][c] = nullptr;
}
}
}
int Board::getBoardSize() const {
return boardSize;
}
Piece* Board::getPiece(int row, int col) const {
return board[row][col];
}
void Board::setPiece(Piece * p, int row, int col) {
board[row][col] = p;
if (!p) {
td->notify(nullptr, row, col);
gd->notify(nullptr, row, col);
}
}
vector<int> Board::getLastMove() const {
return lastMove;
}
void Board::setLastMove(int origRow, int origCol, int newRow, int newCol) {
// Sets the last move vector with this format: {origRow, origCol, newRow, newCol}.
lastMove[0] = origRow; // Sets first value to the original row of the last move.
lastMove[1] = origCol; // Sets second value to the original column of the last move.
lastMove[2] = newRow; // Sets third value to the new row of the last move.
lastMove[3] = newCol; // Sets fourth value to the new col of the last move.
}
King * Board::getWhiteKing() const {
return whiteKing;
}
void Board::setWhiteKing(King * whiteKing) {
this->whiteKing = whiteKing;
}
King * Board::getBlackKing() const {
return blackKing;
}
void Board::setBlackKing(King * blackKing) {
this->blackKing = blackKing;
}
bool Board::setWhitePlayer(string level) {
// Set the appropriate White player type based on input where human is a Player,
// computer[1] is a LevelOne computer, computer[2] is a LevelTwo computer,
// computer[3] is a LevelThree computer, computer[4] is a LevelFour computer.
if (level == "human") {
whitePlayer = new Player(Colour::WHITE);
return false;
} else if (level == "computer[1]") {
whitePlayer = new LevelOne(Colour::WHITE);
return true;
} else if (level == "computer[2]") {
whitePlayer = new LevelTwo(Colour::WHITE);
return true;
} else if (level == "computer[3]") {
whitePlayer = new LevelThree(Colour::WHITE);
return true;
} else if (level == "computer[4]") {
whitePlayer = new LevelFour(Colour::WHITE);
return true;
} else {
throw std::invalid_argument{"Not a valid player."};
}
return true;
}
bool Board::setBlackPlayer(string level) {
// Set the appropriate Black player type based on input.
if (level == "human") {
blackPlayer = new Player(Colour::BLACK);
return false;
} else if (level == "computer[1]") {
blackPlayer = new LevelOne(Colour::BLACK);
return true;
} else if (level == "computer[2]") {
blackPlayer = new LevelTwo(Colour::BLACK);
return true;
} else if (level == "computer[3]") {
blackPlayer = new LevelThree(Colour::BLACK);
return true;
} else if (level == "computer[4]") {
blackPlayer = new LevelFour(Colour::BLACK);
return true;
} else {
throw std::invalid_argument{"Not a valid player."};
}
return true;
}
vector<Piece *> Board::getWhitePieces() const {
return whitePieces;
}
vector<Piece *> Board::getBlackPieces() const {
return blackPieces;
}
bool Board::sameColourSquare(Piece * first, Piece * second) {
// If the sum of the row and column is odd, it is a white square.
// If even, it is a white square.
int whiteSquareColour = (first->getRow() + first->getCol()) % 2;
int blackSquareColour = (second->getRow() + second->getCol()) % 2;
// If the two squares have the same colour, return true.
if (whiteSquareColour == blackSquareColour) {
return true;
}
return false;
}
PieceType Board::convertStrToPiecetype(string type) {
if (type == "K" || type == "k") {
return PieceType::King;
} else if (type == "Q" || type == "q") {
return PieceType::Queen;
} else if (type == "B" || type == "b") {
return PieceType::Bishop;
} else if (type == "N" || type == "n") {
return PieceType::Knight;
} else if (type == "R" || type == "r") {
return PieceType::Rook;
} else if (type == "P" || type == "p") {
return PieceType::Pawn;
} else {
throw std::invalid_argument{"Not a valid piece."};
}
}
int Board::convertLetterToIndex(string letter) {
if (letter == "a") {
return 0;
} else if (letter == "b") {
return 1;
} else if (letter == "c") {
return 2;
} else if (letter == "d") {
return 3;
} else if (letter == "e") {
return 4;
} else if (letter == "f") {
return 5;
} else if (letter == "g") {
return 6;
} else if (letter == "h") {
return 7;
} else {
return -1;
}
}
vector<int> Board::convertStrToCoords(string location) {
// if location is empty, that means it's computer's move
if (location.empty()) return vector<int>{-1, -1};
string strCol = location.substr(0, 1);
int col = convertLetterToIndex(strCol);
// Converting string to int.
istringstream iss{location.substr(1, 2)};
int row;
iss >> row;
row = 8 - row;
// If the location's coordinates are out of bounds, it is not a valid coordinate.
if (row < 0 || row >= 8) {
throw std::invalid_argument{"Not a valid row."};
} else if (col == -1) {
throw std::invalid_argument{"Not a valid column."};
}
return vector <int> {row, col};
}
bool Board::validBoardSetup() {
// Check the board contains exactly one white King and exactly one black King.
if (!whiteKing) {
throw std::domain_error{"There is not a white King on the board. Try again."};
} else if (!blackKing) {
throw std::domain_error{"There is not a black King on the board. Try again."};
}
// Check that no pawns are on the first or last row of the board.
for (int c = 0; c < boardSize; ++c) {
if (getPiece(0, c) && getPiece(0, c)->getState() == PieceType::Pawn) {
throw std::domain_error{"There is a Pawn on the last row of the board. Try again."};
} else if (getPiece(boardSize - 1, c) && getPiece(boardSize - 1, c)->getState() == PieceType::Pawn) {
throw std::domain_error{"There is a Pawn on the first row of the board. Try again."};
}
}
// Check that none of the Kings are in check.
if (checkIfInCheck(Colour::WHITE)) {
throw std::domain_error{"The white King is in check. Try again."};
} else if (checkIfInCheck(Colour::BLACK)) {
throw std::domain_error{"The black King is in check. Try again."};
}
return true;
}
bool Board::checkValid(int origRow, int origCol, int newRow, int newCol, bool calledByPlayer) {
// Check that new location is within range of the board.
if (newRow < 0 || newRow >= boardSize || newCol < 0 || newCol >= boardSize) return false;
// Check that we are not an empty Piece.
if (board[origRow][origCol] == nullptr) {
return false;
}
// If we have a Piece of our team on the square, we cannot move there.
if ((board[newRow][newCol]) && board[origRow][origCol]->getColour() == board[newRow][newCol]->getColour()) {
return false;
}
// Check that the move does not put the King in check.
if (!board[origRow][origCol]->checkForCheckPreMove(*this, newRow, newCol)) {
return false;
}
// Call the specific Piece's checkMovementValid function to verify that it can move there.
if (!(board[origRow][origCol]->checkMovementValid(*this, newRow, newCol, calledByPlayer))) {
return false;
}
return true;
}
bool Board::checkIfInCheck(Colour c) {
// Check if Black King is in check.
if (c == Colour::BLACK) {
vector <Piece *> attackers = blackKing->checkAttackingMe(*this, blackKing->getRow(), blackKing->getCol(), blackKing->getColour());
if (attackers.size() > 0) { // in check
blackKing->setInCheck(true);
return true;
} else {
blackKing->setInCheck(false);
return false;
}
}
// Check if White King is in check.
vector <Piece *> attackers = whiteKing->checkAttackingMe(*this, whiteKing->getRow(), whiteKing->getCol(), whiteKing->getColour());
if (attackers.size() > 0) {
whiteKing->setInCheck(true);
return true;
} else {
whiteKing->setInCheck(false);
return false;
}
}
bool Board::checkForCheckmate(Colour colour) {
// Set friendly and opponent Pieces.
King * king;
vector <Piece *> myPieces;
if (colour == Colour::WHITE) {
king = whiteKing;
myPieces = getWhitePieces();
} else {
king = blackKing;
myPieces = getBlackPieces();
}
int kingRow = king->getRow();
int kingCol = king->getCol();
// Obtain all the Pieces attacking the friendly King.
vector <Piece *> attackers = king->checkAttackingMe(*this, kingRow, kingCol, colour);
// Return false if there is at least one possible move.
if (attackers.size() == 1) {
// Get the Pieces which may capture the Piece attacking the King.
for (Piece * defender:myPieces) {
// If the defender can legally capture the attacking Piece, it is not checkmate.
if (checkValid(defender->getRow(), defender->getCol(), attackers[0]->getRow(), attackers[0]->getCol())) {
return false;
}
}
// Check if there's a Piece of own team that can block the attack. This case is only applicable if
// only one Piece is checking the King. Only Queen, Bishop, and Rook attacks can be blocked.
if ((attackers[0]->getState() == PieceType::Queen) ||
(attackers[0]->getState() == PieceType::Bishop) ||
(attackers[0]->getState() == PieceType::Rook)) {
// Loop through all the squares between the King and attacking Piece.
// Check if any Piece on your own team can move to one of these squares through checkAttackingMe of opposite Colour.
// Checks are done from attacking King to attacking Piece.
int attackerRow = attackers[0]->getRow();
int attackerCol = attackers[0]->getCol();
int r = kingRow;
int c = kingCol;
// If attacking Piece is N of King.
r = kingRow - 1;
c = kingCol;
// Loop through all the squares in between the attacking Piece and the King.
while (r > attackerRow && c == attackerCol) {
for (Piece * p:myPieces) {
// Check if any friendly Pieces can move to that square.
if (checkValid(p->getRow(), p->getCol(), r, c)) {
return false;
}
}
r--;
}
// If attacking Piece is NE of King.
r = kingRow - 1;
c = kingCol + 1;
while (r > attackerRow && c < attackerCol) {
for (Piece * p:myPieces) {
if (checkValid(p->getRow(), p->getCol(), r, c)) {
return false;
}
}
r--;
c++;
}
// If attacking Piece is E of King.
r = kingRow;
c = kingCol + 1;
while (r == attackerRow && c < attackerCol) {
for (Piece * p:myPieces) {
if (checkValid(p->getRow(), p->getCol(), r, c)) {
return false;
}
}
c++;
}
// If attacking Piece is SE of King.
r = kingRow + 1;
c = kingCol + 1;
while (r < attackerRow && c < attackerCol) {
for (Piece * p:myPieces) {
if (checkValid(p->getRow(), p->getCol(), r, c)) {
return false;
}
}
r++;
c++;
}
// If attacking Piece is S of King.
r = kingRow + 1;
c = kingCol;
while (r < attackerRow && c == attackerCol) {
for (Piece * p:myPieces) {
if (checkValid(p->getRow(), p->getCol(), r, c)) {
return false;
}
}
r++;
}
// If attacking Piece is SW of King.
r = kingRow + 1;
c = kingCol - 1;
while (r < attackerRow && c > attackerCol) {
for (Piece * p:myPieces) {
if (checkValid(p->getRow(), p->getCol(), r, c)) {
return false;
}
}
r++;
c--;
}
// If attacking Piece is W of King.
r = kingRow;
c = kingCol - 1;
while (r == attackerRow && c > attackerCol) {
for (Piece * p:myPieces) {
if (checkValid(p->getRow(), p->getCol(), r, c)) {
return false;
}
}
c--;
}
// If attacking Piece is NW of King.
r = kingRow - 1;
c = kingCol - 1;
while (r > attackerRow && c > attackerCol) {
for (Piece * p:myPieces) {
if (checkValid(p->getRow(), p->getCol(), r, c)) {
return false;
}
}
r--;
c--;
}
}
}
// Check if the King can move out of check by moving to a square beside it.
// Check N of king.
if (checkValid(kingRow, kingCol, kingRow - 1, kingCol)) {
return false;
}
// Check NE of king.
if (checkValid(kingRow, kingCol, kingRow - 1, kingCol + 1)) {
return false;
}
// Check E of king.
if (checkValid(kingRow, kingCol, kingRow, kingCol + 1)) {
return false;
}
// Check SE of king.
if (checkValid(kingRow, kingCol, kingRow + 1, kingCol + 1)) {
return false;
}
// Check S of king.
if (checkValid(kingRow, kingCol, kingRow + 1, kingCol)) {
return false;
}
// Check SW of king.
if (checkValid(kingRow, kingCol, kingRow + 1, kingCol - 1)) {
return false;
}
// Check W of king.
if (checkValid(kingRow, kingCol, kingRow, kingCol - 1)) {
return false;
}
// Check NW of king.
if (checkValid(kingRow, kingCol, kingRow - 1, kingCol - 1)) {
return false;
}
return true;
}
bool Board::checkForStalemate(bool whiteTurn) {
// Check combinations of Pieces that result in checkmate.
if (whitePieces.size() == 1 && blackPieces.size() == 1) {
return true;
}
// Check King vs Knight/Bishop and King.
if (whitePieces.size() == 2 && blackPieces.size() == 1) {
if (whitePieces[1]->getState() == PieceType::Bishop || whitePieces[0]->getState() == PieceType::Bishop) {
return true;
} else if (whitePieces[1]->getState() == PieceType::Knight || whitePieces[0]->getState() == PieceType::Knight) {
return true;
}
}
if (whitePieces.size() == 1 && blackPieces.size() == 2) {
if (blackPieces[1]->getState() == PieceType::Bishop || blackPieces[0]->getState() == PieceType::Bishop) {
return true;
} else if (blackPieces[1]->getState() == PieceType::Knight || blackPieces[0]->getState() == PieceType::Knight) {
return true;
}
}
// King and Bishop vs King and Bishop on different color square as opponent's Bishop.
if (whitePieces.size() == 2 && blackPieces.size() == 2) {
if ((whitePieces[0]->getState() == PieceType::Bishop && blackPieces[0]->getState() == PieceType::Bishop)) {
if (!sameColourSquare(whitePieces[0], blackPieces[0])) {
return true;
}
} else if ((whitePieces[0]->getState() == PieceType::Bishop && blackPieces[1]->getState() == PieceType::Bishop)) {
if (!sameColourSquare(whitePieces[0], blackPieces[1])) {
return true;
}
} else if ((whitePieces[1]->getState() == PieceType::Bishop && blackPieces[0]->getState() == PieceType::Bishop)) {
if (!sameColourSquare(whitePieces[1], blackPieces[0])) {
return true;
}
} else if ((whitePieces[1]->getState() == PieceType::Bishop && blackPieces[1]->getState() == PieceType::Bishop)) {
if (!sameColourSquare(whitePieces[1], blackPieces[1])) {
return true;
}
}
}
// If there is at least one possible move, it is not stalemate.
if (whiteTurn) {
for (Piece * p: whitePieces) {
if (p->checkPossibleMoves(*this).size() > 0) {
return false;
}
}
}
if (!whiteTurn) {
for (Piece * p: blackPieces) {
if (p->checkPossibleMoves(*this).size() > 0) {
return false;
}
}
}
return true;
}
void Board::placeNewPiece(string type, string location) {
if (type == "K") {
placeNewPiece(PieceType::King, Colour::WHITE, location);
} else if (type == "k") {
placeNewPiece(PieceType::King, Colour::BLACK, location);
} else if (type == "Q") {
placeNewPiece(PieceType::Queen, Colour::WHITE, location);
} else if (type == "q") {
placeNewPiece(PieceType::Queen, Colour::BLACK, location);
} else if (type == "B") {
placeNewPiece(PieceType::Bishop, Colour::WHITE, location);
} else if (type == "b") {
placeNewPiece(PieceType::Bishop, Colour::BLACK, location);
} else if (type == "N") {
placeNewPiece(PieceType::Knight, Colour::WHITE, location);
} else if (type == "n") {
placeNewPiece(PieceType::Knight, Colour::BLACK, location);
} else if (type == "R") {
placeNewPiece(PieceType::Rook, Colour::WHITE, location);
} else if (type == "r") {
placeNewPiece(PieceType::Rook, Colour::BLACK, location);
} else if (type == "P") {
placeNewPiece(PieceType::Pawn, Colour::WHITE, location);
} else if (type == "p") {
placeNewPiece(PieceType::Pawn, Colour::BLACK, location);
}
}
void Board::placeNewPiece(PieceType type, Colour colour, int row, int col) {
Piece * p;
// Create a new Piece of specified type of Colour.
if (type == PieceType::King) {
King * k = new King(colour);
p = k;
if (colour == Colour::BLACK) {
setBlackKing(k);
} else {
setWhiteKing(k);
}
} else if (type == PieceType::Queen) {
p = new Queen(colour);
} else if (type == PieceType::Bishop) {
p = new Bishop(colour);
} else if (type == PieceType::Knight) {
p = new Knight(colour);
} else if (type == PieceType::Rook) {
p = new Rook(colour);
} else if (type == PieceType::Pawn) {
p = new Pawn(colour);
}
// Attach the Piece to the displays as an Observer.
p->attach(td);
p->attach(gd);
// Replace whatever was currently at (row, col) in the board with the new Piece.
delete board[row][col];
board[row][col] = p;
p->setCoords(row, col);
p->notifyObservers();
// Add the new Piece to the vector of Pieces.
if (colour == Colour::WHITE) {
whitePieces.emplace_back(p);
} else {
blackPieces.emplace_back(p);
}
}
void Board::placeNewPiece(PieceType type, Colour colour, string location) {
// Convert the location to a vector of coordinates.
vector <int> coords = convertStrToCoords(location);
int row = coords[0];
int col = coords[1];
// Place the Piece with the location as integers.
placeNewPiece(type, colour, row, col);
}
void Board::placeNewPiece(string pieceType, Colour colour, string location) {
PieceType type;
// Create a new Piece based on the PieceType.
if (pieceType == "K" || pieceType == "k") {
type = PieceType::King;
} else if (pieceType == "Q" || pieceType == "q") {
type = PieceType::Queen;
} else if (pieceType == "B" || pieceType == "b") {
type = PieceType::Bishop;
} else if (pieceType == "N" || pieceType == "n") {
type = PieceType::Knight;
} else if (pieceType == "R" || pieceType == "r") {
type = PieceType::Rook;
}
// Place the Piece given the PieceType, colour, and location.
placeNewPiece(type, colour, location);
}
void Board::placePieceTemporarily(int origRow, int origCol, int newRow, int newCol) {
if (board[origRow][origCol]) {
board[origRow][origCol]->setCoords(newRow, newCol);
}
// Remove what was originally in board[row][col]
board[newRow][newCol] = nullptr;
// Reassign it to new Piece.
board[newRow][newCol] = board[origRow][origCol];
// Assign original Piece to empty.
board[origRow][origCol] = nullptr;
}
void Board::removePiece(string location) {
vector <int> posToRemove = convertStrToCoords(location);
int row = posToRemove[0];
int col = posToRemove[1];
removePiece(row, col);
}
void Board::removePiece(int row, int col) {
// If there is a piece at row, col, remove it from whitePieces or blackPieces vector.
if (getPiece(row, col)) {
if (getPiece(row, col)->getColour() == Colour::WHITE) {
for (int i = 0; i < whitePieces.size(); ++i) {
// Find the White Piece to be removed.
if (whitePieces[i] == board[row][col]) {
// Use an iterator to find the Piece in the vector and erase it.
for (auto it = whitePieces.begin(); it != whitePieces.end(); ++it) {
if (*it == whitePieces[i]) {
whitePieces.erase(it);
break;
}
}
}
}
} else {
for (int i = 0; i < blackPieces.size(); ++i) {
// Find the Black Piece to be removed.
if (blackPieces[i] == board[row][col]) {
// Use an iterator to find the Piece in the vector and erase it.
for (auto it = blackPieces.begin(); it != blackPieces.end(); ++it) {
if (*it == blackPieces[i]) {
blackPieces.erase(it);
break;
}
}
}
}
}
}
// Delete the Piece from the board, and notify the displays.
delete board[row][col];
board[row][col] = nullptr;
td->notify(nullptr, row, col);
gd->notify(nullptr, row, col);
}
bool Board::boardMakeMove(bool whiteTurn, string origLocation, string newLocation) {
// Convert the string locations to integer vectors as (row, col).
vector <int> origCoords = convertStrToCoords(origLocation);
vector <int> newCoords = convertStrToCoords(newLocation);
// If the move is valid, make it and return true.
if (whiteTurn) {
if (whitePlayer->makeMove(*this, whiteTurn, origCoords[0], origCoords[1], newCoords[0], newCoords[1])) {
return true;
}
} else {
if (blackPlayer->makeMove(*this, whiteTurn, origCoords[0], origCoords[1], newCoords[0], newCoords[1])) {
return true;
}
}
return false;
}
void Board::promotePiece(bool whiteTurn, string origLocation, string newLocation, string newPieceType) {
// Remove the Pawn at the original location.
removePiece(origLocation);
// Place a new Piece based on the Piece we are promoting to on the board.
if (whiteTurn && (newPieceType == "Q" || newPieceType == "B" || newPieceType == "N" || newPieceType == "R")) {
placeNewPiece(newPieceType, Colour::WHITE, newLocation);
} else if (!whiteTurn && (newPieceType == "q" || newPieceType == "b" || newPieceType == "n" || newPieceType == "r")) {
placeNewPiece(newPieceType, Colour::BLACK, newLocation);
}
}
void Board::promotePiece(bool whiteTurn, int row, int col, PieceType pieceType) {
removePiece(row, col);
if (whiteTurn) {
// Creates a new Piece with PieceType type, Colour colour, int row, int col.
placeNewPiece(pieceType, Colour::WHITE, row, col);
} else {
placeNewPiece(pieceType, Colour::BLACK, row, col);
}
}
ostream &operator<<(std::ostream &out, const Board &b) {
out << *(b.td);
return out;
}