-
Notifications
You must be signed in to change notification settings - Fork 0
/
Knight.cpp
36 lines (31 loc) · 1.56 KB
/
Knight.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
#include "Knight.h"
Knight::Knight(const Point& p, Color c)
: ChessPiece(p, c, PieceType::Knight) {}
MoveCode Knight::checkMove(const Board& board, const Point& destination) const
{
MoveCode code = checkSanity(board, destination);
if (code != MoveCode::Valid)
return code;
if ((abs(_position.first - destination.first) == 2 && abs(_position.second - destination.second) == 1) ||
(abs(_position.first - destination.first) == 1 && abs(_position.second - destination.second) == 2))
return MoveCode::Valid;
return MoveCode::InvalidMove;
}
void Knight::getAvailableMoves(const Board& board, ChessMoves& moves) const {
for (int x = _position.first - 1; x <= _position.first + 1; x += 2)
for (int y = _position.second - 2; y <= _position.second + 2; y += 4) {
if (Point(x, y).inBounds(BOARD_WIDTH, BOARD_HEIGHT) && board.getPiece(Point(x, y)).getColor() != _color)
if (board.getPiece(Point(x, y)).getType() != PieceType::Empty)
moves.push_back(new CaptureMove(*this, board.getPiece(Point(x, y))));
else
moves.push_back(new ChessMove(*this, Point(x, _position.second + 1)));
}
for (int x = _position.first - 2; x <= _position.first + 2; x += 4)
for (int y = _position.second - 1; y <= _position.second + 1; y += 2) {
if (Point(x, y).inBounds(BOARD_WIDTH, BOARD_HEIGHT) && board.getPiece(Point(x, y)).getColor() != _color)
if (board.getPiece(Point(x, y)).getType() != PieceType::Empty)
moves.push_back(new CaptureMove(*this, board.getPiece(Point(x, y))));
else
moves.push_back(new ChessMove(*this, Point(x, _position.second + 1)));
}
}