-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.cpp
More file actions
56 lines (46 loc) · 1.59 KB
/
Copy pathPawn.cpp
File metadata and controls
56 lines (46 loc) · 1.59 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
#include "Pawn.h"
Pawn::Pawn(Colour colour) //
: Piece(PAWN, colour){}
std::vector<std::string> Pawn::getLegalTargets(std::string const& from, ChessBoard & board){
std::string target = from, source = from;
std::vector<std::string> legal_positions;
int one_step = 1, two_step = 2;
//Check Colour
if (this->getColour()){
one_step = -1;
two_step = -2;
}
//Check whether pawn has already moved for validity of opening move
if (!this->getMoveCount()){
target[1] = from[1] + two_step;
if (board.isFileRankFree(source, target))
legal_positions.push_back(target);
}
//Add normal move
target[1] = from[1] + one_step;
if (board.isEmpty(target))
legal_positions.push_back(target);
//Check whether an opposing piece can be taken
target[0] = target[0] + one_step;
if (board.onBoard(target) && !board.isEmpty(target) && !board.sameColour(source, target)){
legal_positions.push_back(target);
}
target[0] = target[0] - two_step;
if (board.onBoard(target) && !board.isEmpty(target) && !board.sameColour(source, target)){
legal_positions.push_back(target);
}
//Removing any destination square 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;
}
/*End of function*/
void Pawn::addMove(){move_count++; }
int Pawn::getMoveCount(){return move_count; }
/*END OF FILE*/