-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameboard.cpp
266 lines (192 loc) · 6.58 KB
/
Gameboard.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
#include "Gameboard.hpp"
#include <string>
#include <algorithm>
#include <unordered_map>
#include <cctype>
#include <cassert>
GameBoard::GameBoard() {
board.fill({});
for (auto const& piece : black.pieceList)
board[piece->getPos().column][piece->getPos().row] = piece.get();
for (auto const& piece : white.pieceList)
board[piece->getPos().column][piece->getPos().row] = piece.get();
}
Bitboard GameBoard::genBitBoard() const {
Bitboard bb = 0x00;
for (auto pc = black.pieceList.begin(); pc != black.pieceList.end(); pc++)
bb |= (*pc)->getBBoard();
for (auto pc = white.pieceList.begin(); pc != white.pieceList.end(); pc++)
bb |= (*pc)->getBBoard();
return bb;
}
//GOD this is gonna be so slow
GameBoard::Team::Team(Color _color) {
Color color = _color;
teamBitBoard = color == Color::Black ?
0xFFFF000000000000 : 0x000000000000FFFF;
uint8_t yPos = (color == Color::Black ? 7 : 0);
//Since we can't list initialize unqiue pointers
std::array<Piece*, 8> tmpArr {{
new Pieces::King(color, {4, yPos}),
new Pieces::Queen(color, {3, yPos}),
new Pieces::Rook(color, {0, yPos}),
new Pieces::Rook(color, {7, yPos}),
new Pieces::Bishop(color, {2, yPos}),
new Pieces::Bishop(color, {5, yPos}),
new Pieces::Knight(color, {6, yPos}),
new Pieces::Knight(color, {1, yPos}),
}};
for (Piece* ptr : tmpArr)
pieceList.push_back(std::unique_ptr<Piece>(ptr));
yPos = (color == Color::Black ? 6 : 1);
//THIS IS GONNA BREAK!
for (uint8_t i = 0; i < 8; i++)
pieceList.push_back(std::unique_ptr<Piece>(new Pieces::Pawn(color, {i, yPos})));
}
Bitboard GameBoard::getColorBoard(Color color) const {
return color == Color::Black ? black.teamBitBoard : white.teamBitBoard;
}
//For some reason, this is getting called with
//the same start and end position somewhere...
void GameBoard::movePiece(Pos start, Pos end) {
//Invalidate future moves if a move was undone
//and a new move was made from that position
bool repeatMove = moveHistory.size() > 0 &&
(start == currMove->start && end == currMove->end);
if (!repeatMove && currMove != moveHistory.end()) {
moveHistory.erase(currMove, moveHistory.end());
}
Piece* piece = getSquare(start);
//Handle castle
bool isCastle = piece->getType() == PieceType::King &&
std::abs(start.column - end.column) > 1;
if (isCastle) {
if (repeatMove) {
std::swap(*currMove, *(currMove+1));
}
//Kingside castle if king goes to column 6
if (end.column == 6)
movePiece({7, start.row}, {5, start.row});
else
movePiece({0, start.row}, {3, start.row});
}
//Castle must move King last so that undoMove() can
//detect castling
MoveData moveData { start, end, piece->hasMoved, std::nullopt };
//Handle en passe
if (piece->getType() == PieceType::Pawn &&
std::abs(start.column - end.column) == 1 && !squareOccupied(end)) {
bool movesUp = piece->getColor() == Color::White;
Pos capturedSquare = movesUp ?
Pos{end.column, uint8_t (end.row - 1)} : Pos{end.column, uint8_t (end.row + 1)};
moveData.removedPiece.emplace(getSquare(capturedSquare));
getSquare(capturedSquare) = nullptr;
}
if (squareOccupied(end))
moveData.removedPiece.emplace(getSquare(end));
piece->hasMoved = true;
if (piece->getType() == PieceType::Pawn) {
uint8_t endRow = piece->getColor() == Color::Black ? 0 : 7;
if (end.row == endRow) {
//Is manual memory management a blunder?
Color color = piece->getColor();
delete piece;
piece = new Pieces::Queen(color, start);
piece->hasMoved = false;
piece->pawnPromoted = true;
getSquare(start) = piece;
}
}
bool futureHistory = currMove != moveHistory.end();
if (!repeatMove)
moveHistory.push_back(moveData);
currMove = (futureHistory ? currMove+1 : moveHistory.end());
piece->setPos(end);
//Mark the square from the starting piece pos empty
if (piece->getColor() == Color::Black) {
black.teamBitBoard &= ~start.asBitBoard();
black.teamBitBoard |= end.asBitBoard();
white.teamBitBoard &= ~end.asBitBoard();
} else {
white.teamBitBoard &= ~start.asBitBoard();
white.teamBitBoard |= end.asBitBoard();
black.teamBitBoard &= ~end.asBitBoard();
}
getSquare(end) = getSquare(start);
getSquare(start) = nullptr;
}
//TODO: add support for pawn promotions
void GameBoard::undoMove() {
if (currMove == moveHistory.begin())
throw std::exception();
currMove--;
MoveData lastMove = *currMove;
Piece* piece = getSquare(lastMove.end);
if (piece->getType() == PieceType::King &&
std::abs(lastMove.start.column - lastMove.end.column) > 1) {
undoMove();
lastMove = *(currMove+1);
std::swap(*currMove, *(currMove+1));
}
if (piece->pawnPromoted && piece->hasMoved == false) {
uint8_t endRow = piece->getColor() == Color::Black ? 0 : 7;
if (lastMove.end.row == endRow) {
//Is manual memory management a blunder?
Color color = piece->getColor();
delete piece;
piece = new Pieces::Pawn(color, lastMove.end);
getSquare(lastMove.end) = piece;
}
}
getSquare(lastMove.start) = piece;
getSquare(lastMove.end) = nullptr;
piece->setPos(lastMove.start);
piece->hasMoved = lastMove.hasMoved;
Team& teamData = piece->getColor() == Color::Black ? black : white;
teamData.teamBitBoard &= ~lastMove.end.asBitBoard();
teamData.teamBitBoard |= lastMove.start.asBitBoard();
//You gotta alter this for pawn promotions
if (lastMove.removedPiece.has_value()) {
Piece* capturedPiece = lastMove.removedPiece.value();
getSquare(capturedPiece->getPos()) = capturedPiece;
Team& enemyData = piece->getColor() == Color::Black ? white : black;
enemyData.teamBitBoard |= capturedPiece->getPos().asBitBoard();
}
}
void GameBoard::redoMove() {
if (currMove == moveHistory.end())
throw std::exception();
movePiece(currMove->start, currMove->end);
}
bool squareOccupied(Pos pos);
std::ostream& operator<<(std::ostream& os, GameBoard const& gameBoard) {
std::unordered_map<PieceType, char> pieceSymbolMap {
{ PieceType::King, 'k' },
{ PieceType::Queen, 'q'},
{ PieceType::Rook, 'r' },
{ PieceType::Bishop, 'b' },
{ PieceType::Knight, 'n' },
{ PieceType::Pawn, 'p' }
};
for (int row = 7; row >= 0; row--) {
os << std::to_string(row + 1) << " ";
for (int col = 0; col <= 7; col++) {
char symbol;
if (gameBoard.squareOccupied({(uint8_t) col, (uint8_t) row})) {
Piece const* piece = &gameBoard.getPiece({(uint8_t) col, (uint8_t) row});
symbol = pieceSymbolMap[piece->getType()];
if (piece->getColor() == Color::White)
symbol = std::toupper(symbol);
} else {
symbol = '0';
}
os << symbol << ' ';
}
os << '\n';
}
os << "\n ";
for (int col = 0; col <= 7; col++)
os << char ('A' + col) << ' ';
os << '\n';
return os;
}