-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieces.cpp
More file actions
512 lines (431 loc) · 13 KB
/
Pieces.cpp
File metadata and controls
512 lines (431 loc) · 13 KB
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
/**
Chess
Pieces.cpp
Purpose: Implementation file for all the pieces
@author Sami Khan
@version 1.13 10/12/19
*/
#include "Pieces.h"
//Constructor for singular (only one for each colour) pieces.
Piece::Piece(bool colourPar)
{
colour = colourPar;
alignment = 'u';
numberOfMoves = 0;
}
//Constructor for dual (two for each colour) pieces.
Piece::Piece(bool colourPar, char alignmentPar)
{
colour = colourPar;
alignment = alignmentPar;
numberOfMoves = 0;
}
//Returns the current position of the piece.
std::vector<int> Piece::getPosition()
{
return currentPosition;
}
//Returns the type of a piece.
std::string Piece::getType()
{
return type;
}
//Returns the colour of a piece.
bool Piece::getColour()
{
return colour;
}
// Function that returns if a piece can move to a given location on the board.
bool Piece::canMoveTo(std::map<std::vector<int>, Piece*> pieceAt, std::vector<int> destinationSquare)
{
std::vector<int> vectorNeededToMove = destinationSquare - currentPosition;
std::vector<int> vectorStep = simplify(vectorNeededToMove);
std::vector<int> testPosition = currentPosition;
int divisor = std::max(abs(vectorNeededToMove[0]), abs(vectorNeededToMove[1]));
if(!contains(possibleVectors,vectorStep)) //Checking if the piece can move as requested.
{
return false;
}
if((pieceAt[destinationSquare] != NULL) && (pieceAt[destinationSquare] -> getColour() == colour)) //Checking that a piece is not moving into a friendly piece.
{
return false;
}
for(int i = 0; i < divisor - 1; i++) //Checking all cells in the path for any blocking pieces.
{
testPosition = testPosition + vectorStep;
if(pieceAt[testPosition] != NULL)
{
return false;
}
}
return true;
}
//Function to see if the piece is in check.
bool Piece::isCheck(std::map<std::vector<int>, Piece*> pieceAt)
{
return false;
}
//Function to see if the piece is in checkmate.
bool Piece::isCheckMate(std::map<std::vector<int>, Piece*> pieceAt)
{
return false;
}
//Function that changes the current position of a piece to the given position and increases moves by 1.
void Piece::makeMove(std::vector<int> destinationSquare)
{
currentPosition = destinationSquare;
numberOfMoves++;
}
//Function that changes the current position of a piece to the given position and decreases moves by 1.
void Piece::undoMove(std::vector<int> originalSquare)
{
currentPosition = originalSquare;
numberOfMoves--;
}
//Constructor for King pieces.
King::King(bool colourPar):Piece(colourPar)
{
type = (char*)"King";
possibleVectors = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
if(colour == 0)//White
{
initialPosition.push_back(5);
initialPosition.push_back(1);
}
else
{
initialPosition.push_back(5);
initialPosition.push_back(8);
}
currentPosition = initialPosition;
}
//Function that returns if a king can move to a given location on the board.
bool King::canMoveTo(std::map<std::vector<int>, Piece*> pieceAt, std::vector<int> destinationSquare)
{
std::vector<int> vectorStep = destinationSquare - currentPosition; //No need to simplify here as king can only move 1 block
std::vector<int> testPosition = currentPosition;
if(!contains(possibleVectors,vectorStep))
{
return false;
}
if((pieceAt[destinationSquare] != NULL) && (pieceAt[destinationSquare] -> getColour() == colour))
{
return false;
}
std::map<std::vector<int>, Piece*> pieceAtLocal = pieceAt;
pieceAtLocal[currentPosition] = nullptr; //Making the move and checking if the king ends up in check.
pieceAtLocal[destinationSquare] = this;
std::vector<int> previousPosition = currentPosition;
makeMove(destinationSquare);
if(isCheck(pieceAtLocal))
{
undoMove(previousPosition);
return false;
}
undoMove(previousPosition);
return true;
}
//Function to see if the king is in check.
bool King::isCheck(std::map<std::vector<int>, Piece*> pieceAt)
{
std::vector<std::vector<int>> listOfCurrentPositions = listOfKeys(pieceAt); //List of locations of all pieces.
for(int i = 0; (unsigned)i < listOfCurrentPositions.size(); i++)
{
std::vector<int> testPosition = listOfCurrentPositions[i];
auto testPiece = pieceAt[testPosition];
bool testColour = testPiece -> getColour();
if(testColour == colour) //Filtering out friendly pieces.
{
continue;
}
if(testPiece -> canMoveTo(pieceAt, currentPosition)) //If any piece can move to the kings position then the king is in check.
{
return true;
}
}
return false;
}
//Function to see if the king is in checkmate.
bool King::isCheckMate(std::map<std::vector<int>, Piece*> pieceAt)
{
if(!isCheck(pieceAt)) //If the king is not in check then no need to check anything else.
{
return false;
}
Piece* attacker;
std::vector<std::vector<int>> listOfCurrentPositions = listOfKeys(pieceAt);
std::vector<std::vector<int>> routeOfAttacker;
for(int i = 0; (unsigned)i < listOfCurrentPositions.size(); i++) //Finding the attacking piece
{
std::vector<int> testPosition = listOfCurrentPositions[i];
auto testPiece = pieceAt[testPosition];
bool testColour = testPiece -> getColour();
if(testColour == colour)
{
continue;
}
if(testPiece -> canMoveTo(pieceAt, currentPosition))
{
attacker = testPiece;
break;
}
}
std::vector<int> attackerPosition = attacker -> getPosition(); //Getting information of the attacker.
std::vector<int> vectorOfAttack = currentPosition - attackerPosition;
int length = 1;
if(attacker -> getType() != "Knight")
{
length = std::max(abs(vectorOfAttack[0]),abs(vectorOfAttack[1]));
vectorOfAttack = simplify(vectorOfAttack);
}
for(int i = 0; i < length; i++) //Making a list of all the blocks that the attacking piece passes through so the king can be defended by moving friendly piece in this path.
{
routeOfAttacker.push_back(attackerPosition);
attackerPosition = attackerPosition + vectorOfAttack;
}
std::vector<std::vector<int>> kingPossiblePositions;
for(int i = 0; i < 8; i++) //Creating a list of the possible positions that the king can move to.
{
std::vector<int> futurePosition = currentPosition + possibleVectors[i];
if(futurePosition[0] > 0 && futurePosition[0] < 9 && futurePosition[1] > 0 && futurePosition[1] < 9)
{
kingPossiblePositions.push_back(futurePosition);
}
}
for(int i = 0; i < 8; i++)
{
if(canMoveTo(pieceAt, kingPossiblePositions[i])) //Check to see if the king can move anywhere else...
{
std::vector<int> previousPosition = currentPosition;
makeMove(kingPossiblePositions[i]);
std::map<std::vector<int>, Piece*> pieceAtLocal = pieceAt; //Making a dummy move.
pieceAtLocal[previousPosition] = nullptr;
pieceAtLocal[currentPosition] = this;
if(!isCheck(pieceAtLocal)) //...without being in check again.
{
undoMove(previousPosition);
return false;
}
undoMove(previousPosition);
}
}
for(int i = 0; (unsigned)i < listOfCurrentPositions.size(); i++) //Attempting to move all friendly pieces in the route of the attacker.
{
std::vector<int> testPosition = listOfCurrentPositions[i];
auto testPiece = pieceAt[testPosition];
bool testColour = testPiece -> getColour();
if(testColour != colour) //Filetring out all enemy pieces.
{
continue;
}
for(int j = 0; (unsigned)j < routeOfAttacker.size(); j++)
{
if(testPiece -> canMoveTo(pieceAt, routeOfAttacker[j]))
{
return false;
}
}
}
return true;
}
//Function which lists all the keys in a map.
std::vector<std::vector<int>> King::listOfKeys(std::map<std::vector<int>, Piece*> pieceAt)
{
std::vector<std::vector<int>> outputList;
std::pair<std::vector<int>, Piece*> check;
BOOST_FOREACH(check, pieceAt)
{
if(check.second != NULL)
{
outputList.push_back(check.first);
}
}
return outputList;
}
//Constructor for Rook pieces.
Rook::Rook(int colourPar, char alignmentPar):Piece(colourPar, alignmentPar)
{
type = (char*)"Rook";
possibleVectors = {{1,0},{0,1},{-1,0},{0,-1}};
if(colour == 0)
{
if(alignment == 'l')
{
initialPosition.push_back(1);
}
else
{
initialPosition.push_back(8);
}
initialPosition.push_back(1);
}
else
{
if(alignment == 'l')
{
initialPosition.push_back(1);
}
else
{
initialPosition.push_back(8);
}
initialPosition.push_back(8);
}
currentPosition = initialPosition;
}
//Constructor for the Bishop pieces.
Bishop::Bishop(int colourPar, char alignmentPar):Piece(colourPar, alignmentPar)
{
type = (char*)"Bishop";
possibleVectors = {{1,1},{-1,1},{-1,-1},{1,-1}};
if(colour == 0)
{
if(alignment == 'l')
{
initialPosition.push_back(3);
}
else
{
initialPosition.push_back(6);
}
initialPosition.push_back(1);
}
else
{
if(alignment == 'l')
{
initialPosition.push_back(3);
}
else
{
initialPosition.push_back(6);
}
initialPosition.push_back(8);
}
currentPosition = initialPosition;
}
//Constructor for Queen pieces.
Queen::Queen(int colourPar):Piece(colourPar)
{
type = (char*)"Queen";
possibleVectors = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
if(colour == 0)
{
initialPosition.push_back(4);
initialPosition.push_back(1);
}
else
{
initialPosition.push_back(4);
initialPosition.push_back(8);
}
currentPosition = initialPosition;
}
//Constructor for Knight pieces.
Knight::Knight(int colourPar, char alignmentPar):Piece(colourPar, alignmentPar)
{
type = (char*)"Knight";
possibleVectors = {{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
if(colour == 0)
{
if(alignment == 'l')
{
initialPosition.push_back(2);
}
else
{
initialPosition.push_back(7);
}
initialPosition.push_back(1);
}
else
{
if(alignment == 'l')
{
initialPosition.push_back(2);
}
else
{
initialPosition.push_back(7);
}
initialPosition.push_back(8);
}
currentPosition = initialPosition;
}
//Function that returns if a knight can move to a given location on the board.
bool Knight::canMoveTo(std::map<std::vector<int>, Piece*> pieceAt, std::vector<int> destinationSquare)
{
std::vector<int> vectorStep = destinationSquare - currentPosition;
std::vector<int> testPosition = currentPosition;
//No need to check for any pieces in the way.
if(!contains(possibleVectors,vectorStep))
{
return false;
}
if((pieceAt[destinationSquare] != NULL) && (pieceAt[destinationSquare] -> getColour() == colour))
{
return false;
}
return true;
}
//Constructor for Pawn pieces.
Pawn::Pawn(int colourPar, int pawnNumberPar):Piece(colourPar)
{
type = (char*)"Pawn";
pawnNumber = pawnNumberPar;
if(colour == 0)
{
possibleVectors = POSSIBLE_VECTORS_WHITE;
initialPosition = {pawnNumber, 2};
}
else
{
possibleVectors = POSSIBLE_VECTORS_BLACK;
initialPosition = {pawnNumber, 7};
}
currentPosition = initialPosition;
}
//Function that returns if a pawn can move to a given location on the board.
bool Pawn::canMoveTo(std::map<std::vector<int>, Piece*> pieceAt, std::vector<int> destinationSquare)
{
std::vector<int> vectorStep = destinationSquare - currentPosition;
std::vector<int> testPosition = currentPosition;
std::vector<std::vector<int>> currentPossibleVectors = possibleVectors;
if(pieceAt[currentPosition + possibleVectors[1]] != NULL && (destinationSquare == currentPosition + possibleVectors[1] || destinationSquare == currentPosition + possibleVectors[0])) //If there is a piece right infront of the pawn.
{
return false;
}
if(pieceAt[currentPosition + possibleVectors[0]] != NULL && destinationSquare == currentPosition + possibleVectors[0]) //If there is a piece in the way of the two forward move.
{
return false;
}
//Possible vectors are of the form {first 2 cell move, single forward move, right take move, left take move}.
//The code below just chooses the right vectors to be used in the given situation.
if(numberOfMoves != 0) //If the pawn is on its first move.
{
if(pieceAt[destinationSquare] == NULL) //If there is no piece at the destination block.
{
currentPossibleVectors = std::vector<std::vector<int>>(possibleVectors.begin()+1, possibleVectors.begin()+2);
}
else
{
currentPossibleVectors = std::vector<std::vector<int>>(possibleVectors.begin()+1, possibleVectors.end());
}
}
else
{
if(pieceAt[destinationSquare] == NULL)
{
currentPossibleVectors = std::vector<std::vector<int>>(possibleVectors.begin(), possibleVectors.begin()+2);
}
}
if(!contains(currentPossibleVectors,vectorStep))
{
return false;
}
if((pieceAt[destinationSquare] != NULL) && (pieceAt[destinationSquare] -> getColour() == colour)) //If the pawn is trying to take a friendly piece.
{
return false;
}
return true;
}