-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBoard.cpp
263 lines (235 loc) · 6.75 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
#include "Board.hpp"
#include "Pieces/Bishop.hpp"
#include "Pieces/King.hpp"
#include "Pieces/Knight.hpp"
#include "Pieces/Pawn.hpp"
#include "Pieces/Queen.hpp"
#include "Pieces/Rook.hpp"
#include <stdexcept>
// --------------------------------------------------------------------------------------------------------------------
Board::Board()
{
for (int i = 0; i < ROWS; ++i)
{
mBoard.emplace_back(std::vector<Square>());
for (int j = 0; j < COLS; ++j)
{
mBoard[i].emplace_back(Square(i, j));
}
}
PieceType backRow[COLS] = {
PieceType::kRook,
PieceType::kKnight,
PieceType::kBishop,
PieceType::kQueen,
PieceType::kKing,
PieceType::kBishop,
PieceType::kKnight,
PieceType::kRook
};
for (int col = 0; col < COLS; ++col)
{
AddPiece(PieceColour::kBlack, backRow[col], GetSquare(0, col));
AddPiece(PieceColour::kBlack, PieceType::kPawn, GetSquare(1, col));
AddPiece(PieceColour::kWhite, PieceType::kPawn, GetSquare(6, col));
AddPiece(PieceColour::kWhite, backRow[col], GetSquare(7, col));
}
mKings[0] = GetSquare(7, 4)->GetPiece(); // white king
mKings[1] = GetSquare(0, 4)->GetPiece(); // black king
}
// --------------------------------------------------------------------------------------------------------------------
Board::~Board()
{
Empty();
}
// --------------------------------------------------------------------------------------------------------------------
void Board::Empty()
{
for (int i = 0; i < NUM_COLOURS; ++i)
{
while (mPieces[i].size() > 0)
{
if (!mPieces[i].back()->IsDead())
{
mPieces[i].back()->GetPosition()->Empty();
}
mPieces[i].pop_back();
}
}
while (mMovesPlayed.size() > 0)
{
mMovesPlayed.pop_back();
}
}
// --------------------------------------------------------------------------------------------------------------------
void Board::AddPiece(PieceColour colour, PieceType piece, Square *sq)
{
if (colour != PieceColour::kWhite && colour != PieceColour::kBlack)
{
throw std::invalid_argument("AddPiece: Invalid colour");
}
std::shared_ptr<Piece> new_piece;
switch (piece)
{
case PieceType::kPawn:
new_piece = std::make_shared<Pawn>(colour, PieceType::kPawn);
break;
case PieceType::kKnight:
new_piece = std::make_shared<Knight>(colour, PieceType::kKnight);
break;
case PieceType::kBishop:
new_piece = std::make_shared<Bishop>(colour, PieceType::kBishop);
break;
case PieceType::kRook:
new_piece = std::make_shared<Rook>(colour, PieceType::kRook);
break;
case PieceType::kQueen:
new_piece = std::make_shared<Queen>(colour, PieceType::kQueen);
break;
case PieceType::kKing:
new_piece = std::make_shared<King>(colour, PieceType::kKing);
break;
default:
throw std::invalid_argument("invalid type of piece");
}
sq->Empty();
sq->Place(new_piece);
if (colour == PieceColour::kWhite)
{
mPieces[0].push_back(new_piece);
}
else
{
mPieces[1].push_back(new_piece);
}
}
// --------------------------------------------------------------------------------------------------------------------
Move Board::GetLastMove()
{
if (mMovesPlayed.size() == 0)
{
std::logic_error("getLastMove: No move to pop found");
}
return mMovesPlayed.back();
}
// --------------------------------------------------------------------------------------------------------------------
Square *Board::GetSquare(int row, int col)
{
if (row < 0 || ROWS <= row || col < 0 || COLS <= col)
{
throw std::invalid_argument("GetSquare(int, int): Square out of range\n");
}
return &mBoard[row][col];
}
// --------------------------------------------------------------------------------------------------------------------
bool Board::InRange(int row, int col)
{
if (row < 0 || ROWS <= row || col < 0 || COLS <= col)
{
return false;
}
return true;
}
// --------------------------------------------------------------------------------------------------------------------
int Board::GetRows()
{
return ROWS;
}
// --------------------------------------------------------------------------------------------------------------------
int Board::GetCols()
{
return COLS;
}
// --------------------------------------------------------------------------------------------------------------------
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The functions below are stub functions to implement
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
// --------------------------------------------------------------------------------------------------------------------
std::vector<Move> Board::ListLegalMoves(PieceColour colour)
{
int index;
if (colour == PieceColour::kWhite)
{
index = 0;
}
else
{
index = 1;
}
std::vector<Move> legalMoves;
int len = mPieces[index].size();
for (int i = 0; i < len; ++i)
{
auto piece = mPieces[index][i];
if (piece->IsDead())
{
continue;
}
std::vector<Move> pseudo_legal_moves = piece->ListPseudoLegalMoves(this);
for (auto mv : pseudo_legal_moves)
{
DoMove(mv);
if (!IsChecked(mv.mMovingPiece->GetColour()))
{
legalMoves.push_back(mv);
}
UndoMove();
}
}
return legalMoves;
}
// --------------------------------------------------------------------------------------------------------------------
void Board::DoMove(Move &mv)
{
// implement
// update relative variables
// if the moving piece hasn't moved
if (!mv.mMovingPiece->GetHasMoved())
{
mv.mMovingPiece->SetHasMoved(true);
}
// record the move
mMovesPlayed.push_back(mv);
}
// --------------------------------------------------------------------------------------------------------------------
void Board::UndoMove()
{
if(mMovesPlayed.size() == 0){
return;
}
auto mv = mMovesPlayed[mMovesPlayed.size()-1];
mMovesPlayed.pop_back();
// implement
if (mv.mIsFirstMove)
{
mv.mMovingPiece->SetHasMoved(false);
}
}
// --------------------------------------------------------------------------------------------------------------------
bool Board::IsChecked(PieceColour colour)
{
Square *king_pos;
if (colour == PieceColour::kWhite)
{
king_pos = mKings[0]->GetPosition();
}
else
{
king_pos = mKings[1]->GetPosition();
}
// implement
return false;
}
// --------------------------------------------------------------------------------------------------------------------
bool Board::IsCheckmated(PieceColour colour)
{
return false;
}
// --------------------------------------------------------------------------------------------------------------------
bool Board::IsStalemated(PieceColour colour)
{
return false;
}
// --------------------------------------------------------------------------------------------------------------------