-
Notifications
You must be signed in to change notification settings - Fork 2
/
queen.cc
153 lines (136 loc) · 4.16 KB
/
queen.cc
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
144
145
146
147
148
149
150
151
152
153
#include "queen.h"
Queen::Queen(Colour colour):
Piece{colour, PieceType::Queen} {}
bool Queen::checkMovementValid(Board &b, int newRow, int newCol, bool calledByPlayer) {
// Checks whether the move is either an appropriate diagonal move or a horizontal/vertical move.
return (checkDiagonal(b, newRow, newCol) || checkHorizontalVertical(b, newRow, newCol));
}
vector<vector<int>> Queen::checkPossibleMoves(Board &b) {
vector<vector<int>> possibleMoves;
int boardSize = b.getBoardSize();
// Check squares N of Queen.
int row = getRow() - 1;
int col = getCol();
while (row >= 0) {
Piece * p = b.getPiece(row, col);
if (!p) {
possibleMoves.emplace_back(vector<int>{row, col});
} else if (p->getColour() != getColour()) { // enemy piece
possibleMoves.emplace_back(vector <int> {row, col});
break;
} else {
break;
}
--row;
}
// Check squares E of Queen.
row = getRow();
col = getCol() + 1;
while (col < boardSize) {
Piece * p = b.getPiece(row, col);
if (!p) {
possibleMoves.emplace_back(vector<int>{row, col});
} else if (p->getColour() != getColour()) { // enemy piece
possibleMoves.emplace_back(vector <int> {row, col});
break;
} else {
break;
}
++col;
}
// Check squares S of Queen.
row = getRow() + 1;
col = getCol();
while (row < boardSize) {
Piece * p = b.getPiece(row, col);
if (!p) {
possibleMoves.emplace_back(vector<int>{row, col});
} else if (p->getColour() != getColour()) { // enemy piece
possibleMoves.emplace_back(vector <int> {row, col});
break;
} else {
break;
}
++row;
}
// Check squares W of Queen.
row = getRow();
col = getCol() - 1;
while (col >= 0) {
Piece * p = b.getPiece(row, col);
if (!p) {
possibleMoves.emplace_back(vector<int>{row, col});
} else if (p->getColour() != getColour()) { // enemy piece
possibleMoves.emplace_back(vector <int> {row, col});
break;
} else {
break;
}
--col;
}
// Check squares NE of Queen.
row = getRow() - 1;
col = getCol() + 1;
while (row >= 0 && col < boardSize) {
Piece * p = b.getPiece(row, col);
if (!p) {
possibleMoves.emplace_back(vector <int> {row, col});
} else if (p->getColour() != getColour()) {
possibleMoves.emplace_back(vector <int> {row, col});
break;
} else {
break;
}
--row;
++col;
}
// Check squares SE of Queen.
row = getRow() + 1;
col = getCol() + 1;
while (row < boardSize && col < boardSize) {
Piece * p = b.getPiece(row, col);
if (!p) {
possibleMoves.emplace_back(vector<int>{row, col});
} else if (p->getColour() != getColour()) { // enemy piece
possibleMoves.emplace_back(vector <int> {row, col});
break;
} else {
break;
}
++row;
++col;
}
// Check squares SW of Queen.
row = getRow() + 1;
col = getCol() - 1;
while (col >= 0 && row < boardSize) {
Piece * p = b.getPiece(row, col);
if (!p) {
possibleMoves.emplace_back(vector<int>{row, col});
} else if (p->getColour() != getColour()) { // enemy piece
possibleMoves.emplace_back(vector <int> {row, col});
break;
} else {
break;
}
++row;
--col;
}
// Check squares NW of Queen.
row = getRow() - 1;
col = getCol() - 1;
while (col >= 0 && row >= 0) {
Piece * p = b.getPiece(row, col);
if (!p) {
possibleMoves.emplace_back(vector<int>{row, col});
} else if (p->getColour() != getColour()) { // enemy piece
possibleMoves.emplace_back(vector <int> {row, col});
break;
} else {
break;
}
--row;
--col;
}
return possibleMoves;
}