-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoard.cpp
341 lines (278 loc) · 9.91 KB
/
Board.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
/*
* Board.cpp
* Created by Lukas Bos on 30/11/2017.
* Board is where the game is played.
* Contains: Check, Checkmate, initial positions and allows moves to be set on the board.
*/
#include <iostream>
#include "Board.h"
#include "chesspieces/Rook.h"
#include "chesspieces/Bishop.h"
#include "chesspieces/Knight.h"
#include "chesspieces/Pawn.h"
#include "chesspieces/Queen.h"
#include "chesspieces/King.h"
using namespace sf;
Board::Board() {
// create the squares of the board
for (short i = 0; i < 8; i++) {
for (short j = 0; j < 8; j++) {
Vector2i coordinates = Vector2i(i, j);
squares[i][j] = new Square(coordinates);
}
}
}
// set all pieces to the initial position
void Board::startGame(Player *bottomPlayer, Player *topPlayer, Player *currentPlayer) {
this->bottomPlayer = bottomPlayer;
this->topPlayer = topPlayer;
this->currentPlayer = currentPlayer;
initPieces();
}
void Board::initPieces() {
for (Square *square : getSquares()) {
if (square->getChessPiece()) {
square->getChessPiece()->setLocation(nullptr);
square->removeChessPiece();
}
}
short kingXPosition = 3, queenXPosition = 4;
if (topPlayer->getColor()==PieceColor::BLACK) {
kingXPosition = 4;
queenXPosition = 3;
}
// make all pieces twice
for (int i = 0; i < 2; i++) {
PieceColor color = topPlayer->getColor();
short row = 0;
if (i==1) {
color = bottomPlayer->getColor();
row = 7;
}
squares[0][row]->setChessPiece(new Rook(this, squares[0][row], color));
squares[1][row]->setChessPiece(new Knight(this, squares[1][row], color));
squares[2][row]->setChessPiece(new Bishop(this, squares[2][row], color));
squares[queenXPosition][row]->setChessPiece(new Queen(this, squares[queenXPosition][row], color));
squares[kingXPosition][row]->setChessPiece(new King(this, squares[kingXPosition][row], color));
squares[5][row]->setChessPiece(new Bishop(this, squares[5][row], color));
squares[6][row]->setChessPiece(new Knight(this, squares[6][row], color));
squares[7][row]->setChessPiece(new Rook(this, squares[7][row], color));
}
// add pawns
for (int i = 0; i < 8; i++) {
squares[i][1]->setChessPiece(new Pawn(this, squares[i][1], topPlayer->getColor()));
squares[i][6]->setChessPiece(new Pawn(this, squares[i][6], bottomPlayer->getColor()));
}
}
void Board::doMove(Move *nextMove) {
if (nextMove->getEnPassantTakenPiece()) {
nextMove->getEnPassantTakenPiece()->getLocation()->removeChessPiece();
}
// move the ChessPiece
nextMove->getEndOfMove()->setChessPiece(nextMove->getStartOfMove()->getChessPiece());
nextMove->getEndOfMove()->getChessPiece()->setLocation(nextMove->getEndOfMove());
nextMove->getStartOfMove()->removeChessPiece();
nextMove->getEndOfMove()->getChessPiece()->increaseAmountOfSteps(1);
if (nextMove->isPromoting()) {
nextMove->getEndOfMove()->removeChessPiece();
if (currentPlayer->isHuman) {
while (nextMove->getEndOfMove()->getChessPiece()==nullptr) {
std::cout << "make a choice: q -> queen, r -> rook, b -> bishop, n -> knight" << std::endl;
char PromotionChoice = (char) std::cin.get();
Square *location = nextMove->getEndOfMove();
PieceColor color = nextMove->getInitialPiece()->getColor();
switch (PromotionChoice) {
case 'q':nextMove->getEndOfMove()->setChessPiece(new Queen(this, location, color));
break;
case 'r':nextMove->getEndOfMove()->setChessPiece(new Rook(this, location, color));
break;
case 'b':nextMove->getEndOfMove()->setChessPiece(new Bishop(this, location, color));
break;
case 'n':nextMove->getEndOfMove()->setChessPiece(new Knight(this, location, color));
break;
default:
std::cout << "error in input. make a choice: q -> queen, r -> rook, b -> bishop, n -> knight"
<< std::endl;
}
}
} else {
nextMove->getEndOfMove()->setChessPiece(new Queen(this,
nextMove->getEndOfMove(),
nextMove->getInitialPiece()->getColor()));
}
}
// if there is a castlingRook we must move it also
if (nextMove->getRookTargetSquare()) {
nextMove->getRookTargetSquare()->setChessPiece(nextMove->getCastlingRook());
nextMove->getCastlingRook()->setLocation(nextMove->getRookTargetSquare());
nextMove->getInitalRookSquare()->removeChessPiece();
nextMove->getCastlingRook()->increaseAmountOfSteps(1);
}
if (!nextMove->isSimulated()) {
// cout << nextMove->getName() << endl;
}
FENHistory.push_back(getFEN());
allMoves.push_back(nextMove);
}
bool Board::tooMuchRepetition() {
vector<string> boards = getFENHistory();
sort(boards.begin(), boards.end());
int amountOfDuplicates = 0;
for (int i = 1; i < boards.size(); i++) {
if (boards.at(i - 1)==boards.at(i)) {
amountOfDuplicates++;
if (amountOfDuplicates > 1) {
return true;
}
} else {
amountOfDuplicates = 0;
}
}
return false;
}
void Board::undoMove() {
Move *move = allMoves.back();
if (move) {
FENHistory.pop_back();
allMoves.pop_back();
// revive a piece when it was taken
if (move->getTakenPiece()) {
move->getTakenPiece()->setLocation(move->getEndOfMove());
}
move->getEndOfMove()->setChessPiece(move->getTakenPiece());
move->getInitialPiece()->setLocation(move->getStartOfMove());
move->getStartOfMove()->setChessPiece(move->getInitialPiece());
move->getInitialPiece()->decreaseAmountOfSteps(1);
// if there is a castlingRook we must undo it also
if (move->getInitalRookSquare() && move->getCastlingRook()) {
move->getCastlingRook()->setLocation(move->getInitalRookSquare());
move->getInitalRookSquare()->setChessPiece(move->getCastlingRook());
move->getCastlingRook()->decreaseAmountOfSteps(1);
move->getRookTargetSquare()->removeChessPiece();
}
if (move->getEnPassantTakenPiece()) {
move->getEnPassantTakenPiece()->getLocation()->setChessPiece(move->getEnPassantTakenPiece());
}
if (!move->isSimulated()) {
cout << "Undo: " << move->getName() << endl;
}
}
}
bool Board::isInCheck(PieceColor color) {
PieceColor enemyColor = inverse(color);
// check if the enemy has moves that set the king of the chosen color in check
for (ChessPiece *piece : getPiecesByColor(enemyColor)) {
//considerotherpieces: true, considerCheck FALSE! otherwise infinite loop.
for (Square *square : piece->getAvailableSquares(false)) {
ChessPiece *pieceToHit = square->getChessPiece();
if (pieceToHit && pieceToHit->getType()==KING && pieceToHit->getColor()==color) {
return true;
}
}
}
return false;
}
bool Board::checkMate() {
PieceColor color;
if (isInCheck(PieceColor::WHITE)) color = PieceColor::WHITE;
else if (isInCheck(PieceColor::BLACK)) color = PieceColor::BLACK;
else return false;
// look for all pieces and look if they have available moves
for (ChessPiece *piece : getPiecesByColor(color)) {
if (piece->getAvailableSquares(true).size() > 0) {
return false;
}
}
return true;
}
std::vector<ChessPiece *> Board::getPiecesByColor(PieceColor color) {
std::vector<ChessPiece *> pieces;
pieces.reserve(16);
for (Square *square : getSquares()) {
if (square->getChessPiece() && square->getChessPiece()->getColor()==color) {
pieces.push_back(square->getChessPiece());
}
}
return pieces;
}
void Board::checkGameStatus() {
if (isInCheck(PieceColor::WHITE) || isInCheck(PieceColor::BLACK)) {
if (checkMate()) {
std::string winnerName = isInCheck(PieceColor::WHITE) ? "black" : "white";
std::cout << "The winner is " << winnerName << std::endl;
std::cout << "Restart game? Press B to play with Black and press W to play with white" << std::endl;
allMoves.clear();
// just start the game again ;-) switch players
bottomPlayer->setColor(inverse(bottomPlayer->getColor()));
topPlayer->setColor(inverse(topPlayer->getColor()));
startGame(bottomPlayer, topPlayer, currentPlayer);
}
}
// std::cout << this->getFEN() << endl;
}
Square *Board::getSquare(short x, short y) {
return squares[x][y];
}
vector<Square *> Board::getSquares() {
vector<Square *> allSquares;
allSquares.reserve(64);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
allSquares.push_back(squares[i][j]);
}
}
return allSquares;
}
const vector<Move *> &Board::getAllMoves() const {
return allMoves;
}
Player *Board::getBottomPlayer() const {
return bottomPlayer;
}
void Board::setBottomPlayer(Player *bottomPlayer) {
Board::bottomPlayer = bottomPlayer;
}
Player *Board::getTopPlayer() const {
return topPlayer;
}
void Board::setTopPlayer(Player *topPlayer) {
Board::topPlayer = topPlayer;
}
Player *Board::getCurrentPlayer() const {
return currentPlayer;
}
void Board::setCurrentPlayer(Player *currentPlayer) {
Board::currentPlayer = currentPlayer;
}
vector<string> Board::getFENHistory() {
return FENHistory;
}
string Board::getFEN() {
string FEN = "";
short emptySquaresCount = 0;
for (short i = 0; i < 8; i++) {
for (short j = 0; j < 8; j++) {
Square *square = getSquare(j, i);
if (ChessPiece *piece = square->getChessPiece()) {
if (emptySquaresCount > 0) FEN += to_string(emptySquaresCount);
emptySquaresCount = 0;
// King, Queen, Rook, Bishop, kNight, Pawn
string pieceLetters = "kqrbnp";
char pieceLetter = pieceLetters[piece->getType()];
if (piece->getColor()==PieceColor::WHITE) {
FEN += (char) toupper(pieceLetter);
} else {
FEN += pieceLetter;
}
} else {
emptySquaresCount++;
}
if (j==7) {
if (emptySquaresCount > 0) FEN += to_string(emptySquaresCount);
emptySquaresCount = 0;
FEN += "/";
}
}
}
return FEN;
}