-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rook.cpp
67 lines (64 loc) · 2.55 KB
/
Rook.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
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
#include "Rook.h"
#include <algorithm>
Rook::Rook(const Point& position, Color color) : ChessPiece(position, color, PieceType::Rook) {}
MoveCode Rook::checkMove(const Board& board, const Point& destination) const {
MoveCode code = checkSanity(board, destination);
if (code != MoveCode::Valid)
return code;
if (_position.first == destination.first) {
for (int y = std::min(_position.second, destination.second) + 1;
y < std::max(_position.second, destination.second); y++) {
if (board.getPiece(Point(_position.first, y)).getType() != PieceType::Empty)
return MoveCode::InvalidMove;
}
return MoveCode::Valid;
}
if (_position.second == destination.second) {
for (int x = std::min(_position.first, destination.first) + 1;
x < std::max(_position.first, destination.first); x++) {
if (board.getPiece(Point(x, _position.second)).getType() != PieceType::Empty)
return MoveCode::InvalidMove;
}
return MoveCode::Valid;
}
return MoveCode::InvalidMove;
}
void Rook::getAvailableMoves(const Board& board, ChessMoves& moves) const
{
for (int x = _position.first + 1; x < BOARD_WIDTH; x++) {
if (board.getPiece(Point(x, _position.second)).getColor() == _color)
break;
if (board.getPiece(Point(x, _position.second)).getType() != PieceType::Empty) {
moves.push_back(new CaptureMove(*this, board.getPiece(Point(x, _position.second))));
break;
}
moves.push_back(new ChessMove(*this, Point(x, _position.second)));
}
for (int x = _position.first - 1; x > 0; x--) {
if (board.getPiece(Point(x, _position.second)).getColor() == _color)
break;
if (board.getPiece(Point(x, _position.second)).getType() != PieceType::Empty) {
moves.push_back(new CaptureMove(*this, board.getPiece(Point(x, _position.second))));
break;
}
moves.push_back(new ChessMove(*this, Point(x, _position.second)));
}
for (int y = _position.second; y < BOARD_HEIGHT; y++) {
if (board.getPiece(Point(_position.first, y)).getColor() == _color)
break;
if (board.getPiece(Point(_position.first, y)).getType() != PieceType::Empty) {
moves.push_back(new CaptureMove(*this, board.getPiece(Point(_position.first, y))));
break;
}
moves.push_back(new ChessMove(*this, Point(_position.first, y)));
}
for (int y = _position.second; y > 0; y--) {
if (board.getPiece(Point(_position.first, y)).getColor() == _color)
break;
if (board.getPiece(Point(_position.first, y)).getType() != PieceType::Empty) {
moves.push_back(new CaptureMove(*this, board.getPiece(Point(_position.first, y))));
break;
}
moves.push_back(new ChessMove(*this, Point(_position.first, y)));
}
}