-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.cpp
More file actions
40 lines (34 loc) · 1.28 KB
/
Copy pathKing.cpp
File metadata and controls
40 lines (34 loc) · 1.28 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
#include "King.h"
King::King(Colour colour) //
: Piece(KING, colour){}
std::vector<std::string> King::getLegalTargets(std::string const& from, ChessBoard & board){
std::string target = "A1", source = from;
std::vector<std::string> legal_positions;
/*Iterating through the board*/
/* No named constants used to stay closer to chess notation --> legibility*/
for ( ; target[0] <= 'H' ; ++target[0]){
for ( ; target[1] <= '8'; ++target[1]){
// Checking whether a given square is next to the current position of the king
if (!isNextTo(source, target))
continue;
//Checking whether a given square is blocked by a piece of the same colour
if (!board.isEmpty(target) && board.sameColour(source, target))
continue;
legal_positions.push_back(target);
}
target[1] = '1';
}
//Remove any moves from the vector that would leave the king in check
if (this->getColour() == board.getNextMove()) {
if (!legal_positions.empty()){
for (int i = legal_positions.size() - 1; i >= 0; i--){
if (board.moveExposesKing(source, legal_positions[i]))
legal_positions.erase(legal_positions.begin() + i);
}
}
}
return legal_positions;
}
void King::addMove(){move_count++; }
int King::getMoveCount(){return move_count; }
/*END OF FILE*/