-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.cpp
More file actions
209 lines (177 loc) · 9.75 KB
/
Move.cpp
File metadata and controls
209 lines (177 loc) · 9.75 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "game.h"
std::vector<position> Game::ValidCooByType(position cooStart, int pieceType, bool pieceTeam) {
std::vector <position > possibleMove;
if (pieceType == 1) possibleMove = checkPawnMove(cooStart, pieceTeam);
if (pieceType == 2) possibleMove = checkBishopMove(cooStart);
if (pieceType == 3) possibleMove = checkKnightMove(cooStart);
if (pieceType == 4) possibleMove = checkRookMove(cooStart);
if (pieceType == 5) possibleMove = checkQueenMove(cooStart);
if (pieceType == 6) {
if (m_antiRecursion) possibleMove = checkKingMoveAR(cooStart, pieceTeam);
else possibleMove = checkKingMove(cooStart, pieceTeam);
}
return possibleMove;
}
std::vector<position> Game::checkPawnMove(position coo, bool team)
{
std::vector<position> ValidCoo;
if (team == true) {
if (coo.first == 6) {
if (m_board[coo.first - 2][coo.second].isEmpty() && m_board[coo.first - 1][coo.second].isEmpty()) {
ValidCoo.push_back(newPosition(coo.first - 2, coo.second));
}
}
if(coo.first -1 >= 0) if (m_board[coo.first - 1][coo.second].isEmpty()) ValidCoo.push_back(newPosition(coo.first - 1, coo.second));
if (coo.first - 1 >= 0 && coo.second + 1 <= 7)if ((!isSameTeam(coo, newPosition(coo.first - 1, coo.second + 1))) && !m_board[coo.first - 1][coo.second + 1].isEmpty()) ValidCoo.push_back(newPosition(coo.first - 1, coo.second + 1));
if (coo.first - 1 >= 0 && coo.second - 1 >= 0)if (!isSameTeam(coo, newPosition(coo.first - 1, coo.second - 1)) && !m_board[coo.first - 1][coo.second - 1].isEmpty()) ValidCoo.push_back(newPosition(coo.first - 1, coo.second - 1));
if (m_enPassant.Available) {
if (coo.second == m_enPassant.ligne + 1 || coo.second == m_enPassant.ligne - 1) {
if (coo.first == 4) {
ValidCoo.push_back(newPosition(5, m_enPassant.ligne));
}
}
}
}
else {
if (coo.first == 1) {
if (m_board[coo.first + 2][coo.second].isEmpty() && m_board[coo.first + 1][coo.second].isEmpty()) {
ValidCoo.push_back(newPosition(coo.first + 2, coo.second));
}
}
if (coo.first +1 <= 7) if (m_board[coo.first + 1][coo.second].isEmpty()) ValidCoo.push_back(newPosition(coo.first + 1, coo.second));
if (coo.first + 1 <= 7 && coo.second + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first + 1, coo.second + 1)) && !m_board[coo.first + 1][coo.second + 1].isEmpty()) ValidCoo.push_back(newPosition(coo.first + 1, coo.second + 1));
if (coo.first + 1 <= 7 && coo.second - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first + 1, coo.second - 1)) && !m_board[coo.first + 1][coo.second - 1].isEmpty()) ValidCoo.push_back(newPosition(coo.first + 1, coo.second - 1));
if (m_enPassant.Available) {
if (coo.second == m_enPassant.ligne + 1 || coo.second == m_enPassant.ligne - 1) {
if (coo.first == 3) {
ValidCoo.push_back(newPosition(2, m_enPassant.ligne));
}
}
}
}
return ValidCoo;
}
std::vector<position> Game::checkBishopMove(position coo)
{
std::vector<position> ValidCoo;
int x = coo.first;
int y = coo.second;
bool pieceFound = false;
while (x != 0 && y != 0 && !pieceFound) {
x--;
y--;
if (!m_board[x][y].isEmpty()) pieceFound = true;
if (!isSameTeam(coo, newPosition(x, y))) ValidCoo.push_back(newPosition(x, y));
}
x = coo.first;
y = coo.second;
pieceFound = false;
while (x != 7 && y != 0 && !pieceFound) {
x++;
y--;
if (!m_board[x][y].isEmpty()) pieceFound = true;
if (!isSameTeam(coo, newPosition(x, y))) ValidCoo.push_back(newPosition(x, y));
}
x = coo.first;
y = coo.second;
pieceFound = false;
while (x != 0 && y != 7 && !pieceFound) {
x--;
y++;
if (!m_board[x][y].isEmpty()) pieceFound = true;
if (!isSameTeam(coo, newPosition(x, y))) ValidCoo.push_back(newPosition(x, y));
}
x = coo.first;
y = coo.second;
pieceFound = false;
while (x != 7 && y != 7 && !pieceFound) {
x++;
y++;
if (!m_board[x][y].isEmpty()) pieceFound = true;
if (!isSameTeam(coo, newPosition(x, y))) ValidCoo.push_back(newPosition(x, y));
}
return ValidCoo;
}
std::vector<position> Game::checkKnightMove(position coo)
{
std::vector<position> ValidCoo;
if (coo.first + 1 <= 7 && coo.second + 2 <= 7 ) if( !isSameTeam(coo, newPosition(coo.first + 1, coo.second + 2))) ValidCoo.push_back(newPosition(coo.first + 1, coo.second + 2));
if (coo.first + 2 <= 7 && coo.second + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first + 2, coo.second + 1))) ValidCoo.push_back(newPosition(coo.first + 2, coo.second + 1));
if (coo.first - 1 >= 0 && coo.second + 2 <= 7) if (!isSameTeam(coo, newPosition(coo.first - 1, coo.second + 2))) ValidCoo.push_back(newPosition(coo.first - 1, coo.second + 2));
if (coo.first - 2 >= 0 && coo.second + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first - 2, coo.second + 1))) ValidCoo.push_back(newPosition(coo.first - 2, coo.second + 1));
if (coo.first + 1 <= 7 && coo.second - 2 >= 0) if (!isSameTeam(coo, newPosition(coo.first + 1, coo.second - 2))) ValidCoo.push_back(newPosition(coo.first + 1, coo.second - 2));
if (coo.first + 2 <= 7 && coo.second - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first + 2, coo.second - 1))) ValidCoo.push_back(newPosition(coo.first + 2, coo.second - 1));
if (coo.first - 1 >= 0 && coo.second - 2 >= 0) if (!isSameTeam(coo, newPosition(coo.first - 1, coo.second - 2))) ValidCoo.push_back(newPosition(coo.first - 1, coo.second - 2));
if (coo.first - 2 >= 0 && coo.second - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first - 2, coo.second - 1))) ValidCoo.push_back(newPosition(coo.first - 2, coo.second - 1));
return ValidCoo;
}
std::vector<position> Game::checkRookMove(position coo)
{
std::vector<position> ValidCoo;
int xmin = coo.first;
bool pieceFound = false;
while (xmin != 0 && !pieceFound) {
xmin--;
if (!m_board[xmin][coo.second].isEmpty()) pieceFound = true;
if (!isSameTeam(coo, newPosition(xmin, coo.second))) ValidCoo.push_back(newPosition(xmin, coo.second));
}
int xmax = coo.first;
pieceFound = false;
while (xmax != 7 && !pieceFound) {
xmax++;
if (!m_board[xmax][coo.second].isEmpty()) pieceFound = true;
if (!isSameTeam(coo, newPosition(xmax, coo.second)))ValidCoo.push_back(newPosition(xmax, coo.second));
}
int ymin = coo.second;
pieceFound = false;
while (ymin != 0 && !pieceFound) {
ymin--;
if (!m_board[coo.first][ymin].isEmpty()) pieceFound = true;
if (!isSameTeam(coo, newPosition(coo.first, ymin))) ValidCoo.push_back(newPosition(coo.first, ymin));
}
int ymax = coo.second;
pieceFound = false;
while (ymax != 7 && !pieceFound) {
ymax++;
if (!m_board[coo.first][ymax].isEmpty()) pieceFound = true;
if (!isSameTeam(coo, newPosition(coo.first, ymax)))ValidCoo.push_back(newPosition(coo.first, ymax));
}
return ValidCoo;
}
std::vector<position> Game::checkQueenMove(position coo)
{
std::vector<position> validCoo1, validCoo2;
validCoo1 = checkBishopMove(coo);
validCoo2 = checkRookMove(coo);
validCoo1.insert(validCoo1.end(), validCoo2.begin(), validCoo2.end());
return validCoo1;
}
std::vector<position> Game::checkKingMove(position coo, bool team)
{
m_antiRecursion = !m_antiRecursion;
std::vector<position> ValidCoo;
if (coo.first - 1 >= 0 && coo.second - 1 >= 0) if ( !isSameTeam(coo, newPosition(coo.first - 1, coo.second - 1))) ValidCoo.push_back(newPosition(coo.first - 1, coo.second - 1));
if (coo.first + 1 <= 7 && coo.second + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first + 1, coo.second + 1))) ValidCoo.push_back(newPosition(coo.first + 1, coo.second + 1));
if (coo.first - 1 >= 0 && coo.second + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first - 1, coo.second + 1))) ValidCoo.push_back(newPosition(coo.first - 1, coo.second + 1));
if (coo.first + 1 <= 7 && coo.second - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first + 1, coo.second - 1))) ValidCoo.push_back(newPosition(coo.first + 1, coo.second - 1));
if (coo.second + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first, coo.second + 1))) ValidCoo.push_back(newPosition(coo.first, coo.second + 1));
if (coo.second - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first, coo.second - 1))) ValidCoo.push_back(newPosition(coo.first, coo.second - 1));
if (coo.first + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first + 1, coo.second))) ValidCoo.push_back(newPosition(coo.first + 1, coo.second));
if (coo.first - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first - 1, coo.second)))ValidCoo.push_back(newPosition(coo.first - 1, coo.second));
std::vector<position> castlingCoo = checkForCastling(team);
for (int i = 0; i <castlingCoo.size(); i++) ValidCoo.push_back(castlingCoo[i]);
return ValidCoo;
}
std::vector<position> Game::checkKingMoveAR(position coo, int team)
{
std::vector<position> ValidCoo;
if (coo.first - 1 >= 0 && coo.second - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first - 1, coo.second - 1))) ValidCoo.push_back(newPosition(coo.first - 1, coo.second - 1));
if (coo.first + 1 <= 7 && coo.second + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first + 1, coo.second + 1))) ValidCoo.push_back(newPosition(coo.first + 1, coo.second + 1));
if (coo.first - 1 >= 0 && coo.second + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first - 1, coo.second + 1))) ValidCoo.push_back(newPosition(coo.first - 1, coo.second + 1));
if (coo.first + 1 <= 7 && coo.second - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first + 1, coo.second - 1))) ValidCoo.push_back(newPosition(coo.first + 1, coo.second - 1));
if (coo.second + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first, coo.second + 1)) ) ValidCoo.push_back(newPosition(coo.first, coo.second + 1));
if (coo.second - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first, coo.second - 1))) ValidCoo.push_back(newPosition(coo.first, coo.second - 1));
if (coo.first + 1 <= 7) if (!isSameTeam(coo, newPosition(coo.first + 1, coo.second))) ValidCoo.push_back(newPosition(coo.first + 1, coo.second));
if (coo.first - 1 >= 0) if (!isSameTeam(coo, newPosition(coo.first - 1, coo.second))) ValidCoo.push_back(newPosition(coo.first - 1, coo.second));
return ValidCoo;
}