-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.cpp
More file actions
57 lines (42 loc) · 1.42 KB
/
Copy pathPawn.cpp
File metadata and controls
57 lines (42 loc) · 1.42 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
#include "Pawn.h"
Pawn::Pawn(Position pos, char type, Board* board) : Piece(pos, type, board)
{
}
Pawn::~Pawn()
{
}
bool Pawn::checkMove(const Position& pos) const
{
int numDiff = _index.getNumber() - pos.getNumber();
int letDiff = _index.getLetter() - pos.getLetter();
int colorDirection = isWhite() ? -1 : 1;
//on the first turn, the pawn gets a double step
bool isValidBigInitMove = (numDiff == 2 * (colorDirection) &&
letDiff == 0 &&
(*_board)[pos] == nullptr &&
isAtStartPosition());
//the pawn can "eat" an enemy's piece if it's on a one-step diagonal (bonus)
bool isValidDiagonalMove = (numDiff == 1 * (colorDirection) &&
abs(letDiff) == 1 &&
(*_board)[pos] != nullptr &&
isEnemy(*(*_board)[pos]));
//normal one-step
bool isValidSmallMove = (numDiff == 1 * (colorDirection) &&
letDiff == 0 &&
(*_board)[pos] == nullptr);
bool isValidMove = isValidSmallMove || isValidDiagonalMove || isValidBigInitMove;
//if the pawn reaches the other side of the board, it is promoted(bonus)
bool isPromotion = (isValidMove &&
pos.getNumber() == (SIDE_LEN - 1) * isWhite());
if (isPromotion)
{
Piece* promotion = new Queen(pos, isWhite() ? 'Q' : 'q', _board);
(*_board).promote(promotion, _index);
}
return isValidMove;
}
bool Pawn::isAtStartPosition() const
{
return (isWhite() && _index.getNumber() == 1)
|| (!isWhite() && _index.getNumber() == 6);
}