-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queen.cpp
143 lines (137 loc) · 4.95 KB
/
Queen.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "Queen.h"
#include <algorithm>
Queen::Queen(const Point& p, Color c) : ChessPiece(p, c, PieceType::Queen){}
MoveCode Queen::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;
}
if (_position.first - destination.first == _position.second - destination.second)
{
int y = std::min(_position.second, destination.second) + 1;
for (int x = std::min(_position.first, destination.first) + 1;
x < std::max(_position.first, destination.first) && y < std::max(_position.second, destination.second); x++)
{
if (board.getPiece(Point(x, y)).getType() != PieceType::Empty)
return MoveCode::InvalidMove;
y++;
}
return MoveCode::Valid;
}
if (_position.first - destination.first == -(_position.second - destination.second))
{
int y = std::max(_position.second, destination.second) - 1;
for (int x = std::min(_position.first, destination.first) + 1;
x < std::max(_position.first, destination.first) && y > std::min(_position.second, destination.second); x++)
{
if (board.getPiece(Point(x, y)).getType() != PieceType::Empty)
return MoveCode::InvalidMove;
y--;
}
return MoveCode::Valid;
}
return MoveCode::InvalidMove;
}
void Queen::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)));
}
int y = _position.second + 1;
for (int x = _position.first + 1; x < BOARD_WIDTH && y < BOARD_HEIGHT; x++) {
if (board.getPiece(Point(x, y)).getColor() == _color)
break;
if (board.getPiece(Point(x, y)).getType() != PieceType::Empty) {
moves.push_back(new CaptureMove(*this, board.getPiece(Point(x, y))));
break;
}
moves.push_back(new ChessMove(*this, Point(x, y)));
y++;
}
y = _position.second - 1;
for (int x = _position.first - 1; x > 0 && y > 0; x--) {
if (board.getPiece(Point(x, y)).getColor() == _color)
break;
if (board.getPiece(Point(x, y)).getType() != PieceType::Empty) {
moves.push_back(new CaptureMove(*this, board.getPiece(Point(x, y))));
break;
}
moves.push_back(new ChessMove(*this, Point(x, y)));
y--;
}
y = _position.second - 1;
for (int x = _position.second; x < BOARD_WIDTH && y > 0; x++) {
if (board.getPiece(Point(x, y)).getColor() == _color)
break;
if (board.getPiece(Point(x, y)).getType() != PieceType::Empty) {
moves.push_back(new CaptureMove(*this, board.getPiece(Point(x, y))));
break;
}
moves.push_back(new ChessMove(*this, Point(x, y)));
y++;
}
y = _position.second + 1;
for (int x = _position.second; x > 0 && y < BOARD_HEIGHT; x--) {
if (board.getPiece(Point(x, y)).getColor() == _color)
break;
if (board.getPiece(Point(x, y)).getType() != PieceType::Empty) {
moves.push_back(new CaptureMove(*this, board.getPiece(Point(x, y))));
break;
}
moves.push_back(new ChessMove(*this, Point(x, y)));
y--;
}
}