-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.cpp
362 lines (319 loc) · 11.3 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "board.h"
Board::Board(QObject *parent) : QObject(parent){
restart();
}
QVariantList Board::getPiecePNGList(){
QVariantList output;
for(int i=0;i<64;i++){
if(pieceArray[i]){
output.append(pieceArray[i]->getName());
} else {
output.append("blank");
}
}
return output;
}
/**
* @brief Board::isMoveLegal Checks if the chess move is legal.
* Needs to verify a list of things:
* If checked, does move change this.
* Is square free, if yes then check if movement is legal.
* If no, check if piece is friendly or hostile.
* If piece is hostile, check if take is legal.
* @param from
* @param to
* @return
*/
bool Board::isMoveLegal(QString from, QString to){
if(!isMoveStringValid(from) || !isMoveStringValid(to)){
// qDebug() << "from: " << from << " to: " << to;
return false;
}
if(isCheck){
int fromIndex = Piece::convertMoveToIndex(from);
int toIndex = Piece::convertMoveToIndex(to);
if(pieceArray[fromIndex]->isMoveLegal(to,pieceArray[toIndex],pieceArray)){
//Simulate a move
Piece* toPiece = pieceArray[toIndex];
Piece* fromPiece = pieceArray[fromIndex];
fromPiece->setCurrentPosition(to);
pieceArray[toIndex] = fromPiece;
pieceArray[fromIndex] = nullptr;
bool isChecked = getBoardCheckStatus();
// qDebug() << whichColorCanMove << " is checked. Will this move remove the check: " << !isChecked;
//Reset piece locations
pieceArray[toIndex] = toPiece;
fromPiece->setCurrentPosition(from);
pieceArray[fromIndex] = fromPiece;
//If check was removed, the move is legal.
return !isChecked;
}
return false;
} else {
//Needs to check if the move is legal, and if it puts the king in a vulnerable position
int fromIndex = Piece::convertMoveToIndex(from);
int toIndex = Piece::convertMoveToIndex(to);
if(pieceArray[fromIndex]->isMoveLegal(to,pieceArray[toIndex],pieceArray)){
Piece* toPiece = pieceArray[toIndex];
Piece* fromPiece = pieceArray[fromIndex];
fromPiece->setCurrentPosition(to);
pieceArray[toIndex] = fromPiece;
pieceArray[fromIndex] = nullptr;
bool isChecked = getBoardCheckStatus();
//qDebug() << whichColorCanMove << " has attempted a move, did the move put his own King in check: " << isChecked;
//Reset piece locations
pieceArray[toIndex] = toPiece;
fromPiece->setCurrentPosition(from);
pieceArray[fromIndex] = fromPiece;
//If check was removed, the move is legal.
return !isChecked;
}
return false;
}
/*if(isSquareFree(to)){
return isMovementLegal(from,to);
} else {
if(isHostile(from, to)){
return isTakeLegal(from,to);
} else {
//Only Exception to this rule is 'rokkade' which is NYI
return false;
}
}
return true;*/
}
void Board::movePiece(QString from, QString to){
int fromIndex = Piece::convertMoveToIndex(from);
int toIndex = Piece::convertMoveToIndex(to);
Piece* toPiece = pieceArray[toIndex];
Piece* fromPiece = pieceArray[fromIndex];
pieceArray[toIndex] = fromPiece;
pieceArray[fromIndex] = nullptr;
pieceArray[toIndex]->setCurrentPosition(to);
if(toPiece){
delete toPiece;
}
//Checks if Pawn has reached end of board and should transform to a Queen
if(fromPiece->getType()=='P'){
if(((Pawn*)fromPiece)->hasReachedEndOfBoard()){
QString queenImageName;
Color pieceColor = fromPiece->getColor();
if(pieceColor == WHITE){
queenImageName = "wQ";
} else {
queenImageName = "bQ";
}
Queen* newQueen = new Queen(to, pieceColor, queenImageName);
delete fromPiece;
pieceArray[toIndex] = newQueen;
}
}
toggleWhichColorCanMove();
isCheck = getBoardCheckStatus();
updateIsMate();
//qDebug() << "moved from: " << from << " to: " << to;
// printBoard();
}
void Board::restart(){
/*
Loop iteration (same as QML expects):
A8,B8,C8,D8,E8,F8,G8,H8,A7...
0-7
8-15
16-23
24-31
32-39
40-47
48-55
56-63
Positions 0-15 is black pieces
Positions 48-63 is white pieces
*/
pieceArray[0] = (Piece*)new Rook("A8",BLACK,"bR");
pieceArray[1] = (Piece*)new Knight("B8",BLACK,"bN");
pieceArray[2] = (Piece*)new Bishop("C8",BLACK,"bB");
pieceArray[3] = (Piece*)new Queen("D8",BLACK,"bQ");
pieceArray[4] = (Piece*)new King("E8",BLACK,"bK");
pieceArray[5] = (Piece*)new Bishop("F8",BLACK,"bB");
pieceArray[6] = (Piece*)new Knight("G8",BLACK,"bN");
pieceArray[7] = (Piece*)new Rook("H8",BLACK,"bR");
pieceArray[8] = (Piece*)new Pawn("A7",BLACK,"bP");
pieceArray[9] = (Piece*)new Pawn("B7",BLACK,"bP");
pieceArray[10] = (Piece*)new Pawn("C7",BLACK,"bP");
pieceArray[11] = (Piece*)new Pawn("D7",BLACK,"bP");
pieceArray[12] = (Piece*)new Pawn("E7",BLACK,"bP");
pieceArray[13] = (Piece*)new Pawn("F7",BLACK,"bP");
pieceArray[14] = (Piece*)new Pawn("G7",BLACK,"bP");
pieceArray[15] = (Piece*)new Pawn("H7",BLACK,"bP");
pieceArray[48] = (Piece*)new Pawn("A2",WHITE,"wP");
pieceArray[49] = (Piece*)new Pawn("B2",WHITE,"wP");
pieceArray[50] = (Piece*)new Pawn("C2",WHITE,"wP");
pieceArray[51] = (Piece*)new Pawn("D2",WHITE,"wP");
pieceArray[52] = (Piece*)new Pawn("E2",WHITE,"wP");
pieceArray[53] = (Piece*)new Pawn("F2",WHITE,"wP");
pieceArray[54] = (Piece*)new Pawn("G2",WHITE,"wP");
pieceArray[55] = (Piece*)new Pawn("H2",WHITE,"wP");
pieceArray[56] = (Piece*)new Rook("A1",WHITE,"wR");
pieceArray[57] = (Piece*)new Knight("B1",WHITE,"wN");
pieceArray[58] = (Piece*)new Bishop("C1",WHITE,"wB");
pieceArray[59] = (Piece*)new Queen("D1",WHITE,"wQ");
pieceArray[60] = (Piece*)new King("E1",WHITE,"wK");
pieceArray[61] = (Piece*)new Bishop("F1",WHITE,"wB");
pieceArray[62] = (Piece*)new Knight("G1",WHITE,"wN");
pieceArray[63] = (Piece*)new Rook("H1",WHITE,"wR");
//Trodde nullptr var default, men krasjer uten dette
for(int i=16;i<=47;i++){
pieceArray[i] = nullptr;
}
whichColorCanMove = WHITE;
}
QString Board::getWhichColorCanMove(){
if(whichColorCanMove == WHITE){
return "White's turn";
} else {
return "Black's turn";
}
}
bool Board::doesPieceBelongToPlayer(QString square){
int index = Piece::convertMoveToIndex(square);
return pieceArray[index]->getColor() == whichColorCanMove;
}
void Board::toggleWhichColorCanMove(){
if(whichColorCanMove == WHITE){
whichColorCanMove = BLACK;
} else {
whichColorCanMove = WHITE;
}
}
bool Board::getIsCheck(){
return isCheck;
}
bool Board::getIsMate(){
return isMate;
}
bool Board::isSquareFree(QString square){
int index = Piece::convertMoveToIndex(square);
return pieceArray[index];
}
bool Board::isMoveStringValid(QString move){
if(move.length() == 2){
//TODO add checks for A->H & 1->8
return true;
}
return false;
}
QString Board::getKingSquareString(Color c){
for(int i=0;i<64;i++){
if(pieceArray[i] && pieceArray[i]->getType() == 'K' && pieceArray[i]->getColor() == c){
return pieceArray[i]->getCurrentPosition();
}
}
return "";
}
/**
* @brief Board::printBoard for debugging
*/
void Board::printBoard(){
QString row = "";
for(int i=0;i<64;i++){
Piece* p = pieceArray[i];
if(i != 0 && i % 8 == 0){
qDebug() << row;
row = "";
}
if(p){
char type = p->getType();
row += type;
row += " ";
// qDebug() << "type: " << type << " index: " << i;
} else {
row += " ";
}
}
qDebug() << row;
}
bool Board::getBoardCheckStatus(){
QString vulnerableKingPosition = getKingSquareString(whichColorCanMove);
//qDebug() << "Checking if the King @ " << vulnerableKingPosition << " is checked";
Piece* vulnerableKingPiece = pieceArray[Piece::convertMoveToIndex(vulnerableKingPosition)];
Color oppositeColor;
if(whichColorCanMove == BLACK){
oppositeColor = WHITE;
} else {
oppositeColor = BLACK;
}
for(int i=0;i<64;i++){
if(pieceArray[i]){
if(pieceArray[i]->getColor() == oppositeColor){
if(pieceArray[i]->isMoveLegal(vulnerableKingPosition,vulnerableKingPiece,pieceArray)){
return true;
}
}
}
}
return false;
}
/**
* @brief Board::updateIsMate check if there exists a move which can 'un-check' the
* vulnerable King
*/
void Board::updateIsMate(){
if(isCheck){
for(int i=0;i<64;i++){
Piece* fromPiece = pieceArray[i];
//Piece exists and belongs to current player
if(fromPiece && fromPiece->getColor() == whichColorCanMove){
QString from = fromPiece->getCurrentPosition();
//Checks if tempPiece can move to any square on the board
for(int j=0;j<64;j++){
if(i == j){
continue;
}
QString to = Piece::convertIndexToMove(j);
bool canMoveToSquare = fromPiece->isMoveLegal(to,pieceArray[j],pieceArray);
//Simulate the move, see if it solved the check
if(canMoveToSquare){
Piece* toPiece = pieceArray[j];
fromPiece->setCurrentPosition(to);
pieceArray[j] = fromPiece;
pieceArray[i] = nullptr;
bool isChecked = getBoardCheckStatus();
//Reset piece locations
pieceArray[j] = toPiece;
fromPiece->setCurrentPosition(from);
pieceArray[i] = fromPiece;
//If King is no longer checked, stop the loop
if(!isChecked){
qDebug() << "It is not mate because moving from: " << from << " to: " << to << " solved the check";
return;
}
}
}
}
}
isMate = true;
} else {
isMate = false;
}
}
/**
* @brief Board::isHostile checks if the colors of the pieces are different.
* Both squares should already be verified to contain pieces.
* @param from QString
* @param to QString
* @return bool
*/
/*bool Board::isHostile(QString from, QString to){
int fromIndex = Piece::convertMoveToIndex(from);
int toIndex = Piece::convertMoveToIndex(to);
return pieceArray[fromIndex]->getColor() != pieceArray[toIndex]->getColor();
}
bool Board::isTakeLegal(QString from, QString to){
int fromIndex = Piece::convertMoveToIndex(from);
// return pieceArray[fromIndex]->is
int toIndex = Piece::convertMoveToIndex(to);
}
bool Board::isMovementLegal(QString from, QString to){
int fromIndex = Piece::convertMoveToIndex(from);
int toIndex = Piece::convertMoveToIndex(to);
}*/