-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piece.cpp
339 lines (321 loc) · 12.8 KB
/
Piece.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// -*-c++-*-
#include "Piece.h"
#include "game.h"
using namespace std;
Piece::Piece(_Piece* pointer)
{
ptr = (void*) pointer;
}
int Piece::id()
{
return ((_Piece*)ptr)->id;
}
int Piece::owner() const
{
return ((_Piece*)ptr)->owner;
}
int Piece::file()
{
return ((_Piece*)ptr)->file;
}
int Piece::rank()
{
return ((_Piece*)ptr)->rank;
}
int Piece::hasMoved()
{
return ((_Piece*)ptr)->hasMoved;
}
int Piece::type()
{
return ((_Piece*)ptr)->type;
}
int Piece::move(int file, int rank, int type)
{
return pieceMove( (_Piece*)ptr, file, rank, type);
}
//If king is in check function (sent in the board, a move, and the piece moving)
//returns true if king is in check.
bool Piece::isKingInCheck(Piece* (&chessBoard)[9][9], PieceMove currentMove, Piece & sentPiece){
bool isCheck = false;
//Initializing new board that will copy sent board (to create new move and see if king becomes in check)
Piece *tempChessBoard[9][9];
//Initializing piece to equal the piece sent in after the move occurs.
Piece currentPiece;
//cout <<"Before: "<<endl;
for (size_t rank=8; rank>0; rank--){
//cout << "|";
for (size_t file=1; file<=8; file++){
tempChessBoard[rank][file] = chessBoard[rank][file];
/*if(tempChessBoard[rank][file] != NULL){
if(tempChessBoard[rank][file]->owner() == 1){
cout << "*";
} else{
cout << " ";
}
cout << (char)tempChessBoard[rank][file]->type() << " ";
}else{
cout << " ";
}
cout << "|";*/
}
//cout << endl;
}
tempChessBoard[currentMove.rank][currentMove.file] = &sentPiece;
currentPiece = *tempChessBoard[currentMove.rank][currentMove.file];
tempChessBoard[sentPiece.rank()][sentPiece.file()] = NULL;
/*cout <<"After: "<<endl;
for (size_t rank=8; rank>0; rank--){
cout << "|";
for (size_t file=1; file<=8; file++){
if(tempChessBoard[rank][file] != NULL){
if(tempChessBoard[rank][file]->owner() == 1){
cout << "*";
} else{
cout << " ";
}
cout << (char)tempChessBoard[rank][file]->type() << " ";
}else{
cout << " ";
}
cout << "|";
}
cout << endl;
}*/
if(currentPiece.owner() == 1){
//Checking if enemy pawn is able to take the king from (-1, -1) and (-1, +1) diagonals (DIFFERENT IF PLAYER IS BLACK OR WHITE)
if(currentPiece.rank() - 1 > 0 && currentPiece.file() - 1 > 0
&& tempChessBoard[currentPiece.rank() - 1][currentPiece.file() - 1] != NULL
&& tempChessBoard[currentPiece.rank() - 1][currentPiece.file() - 1]->owner() != currentPiece.owner()
&& (char)tempChessBoard[currentPiece.rank() - 1][currentPiece.file() - 1]->type() == 'P'){
isCheck = true;
return isCheck;
}
if(currentPiece.rank() - 1 > 0 && currentPiece.file() + 1 <= 8
&& tempChessBoard[currentPiece.rank() - 1][currentPiece.file() + 1] != NULL
&& tempChessBoard[currentPiece.rank() - 1][currentPiece.file() + 1]->owner() != currentPiece.owner()
&& (char)tempChessBoard[currentPiece.rank() - 1][currentPiece.file() + 1]->type() == 'P'){
isCheck = true;
return isCheck;
}
} else {
//Checking if enemy pawn is able to take the king from (+1, -1) and (+1, +1) diagonals (DIFFERENT IF PLAYER IS BLACK OR WHITE)
if(currentPiece.rank() + 1 <= 8 && currentPiece.file() - 1 > 0
&& tempChessBoard[currentPiece.rank() + 1][currentPiece.file() - 1] != NULL
&& tempChessBoard[currentPiece.rank() + 1][currentPiece.file() - 1]->owner() != currentPiece.owner()
&& (char)tempChessBoard[currentPiece.rank() + 1][currentPiece.file() - 1]->type() == 'P'){
isCheck = true;
return isCheck;
}
if(currentPiece.rank() + 1 <= 8 && currentPiece.file() + 1 <= 8
&& tempChessBoard[currentPiece.rank() + 1][currentPiece.file() + 1] != NULL
&& tempChessBoard[currentPiece.rank() + 1][currentPiece.file() + 1]->owner() != currentPiece.owner()
&& (char)tempChessBoard[currentPiece.rank() + 1][currentPiece.file() + 1]->type() == 'P'){
isCheck = true;
return isCheck;
}
}
//Checking straight line after the king (up, down, right, and left) for Rook or Queen piece of other player that is able to take the king.
for(int i = currentPiece.rank() + 1; i <= 8; i++){
if(tempChessBoard[i][currentPiece.file()] != NULL && tempChessBoard[i][currentPiece.file()]->owner() == currentPiece.owner()){
break;
}
else if(tempChessBoard[i][currentPiece.file()] != NULL && tempChessBoard[i][currentPiece.file()]->owner() != currentPiece.owner()
&& ((char)tempChessBoard[i][currentPiece.file()]->type() == 'R' || (char)tempChessBoard[i][currentPiece.file()]->type() == 'Q')){
isCheck = true;
return isCheck;
}
}
for(int i = currentPiece.rank() - 1; i > 0; i--){
if(tempChessBoard[i][currentPiece.file()] != NULL && tempChessBoard[i][currentPiece.file()]->owner() == currentPiece.owner()){
break;
}
else if(tempChessBoard[i][currentPiece.file()] != NULL && tempChessBoard[i][currentPiece.file()]->owner() != currentPiece.owner()
&& ((char)tempChessBoard[i][currentPiece.file()]->type() == 'R' || (char)tempChessBoard[i][currentPiece.file()]->type() == 'Q')){
isCheck = true;
return isCheck;
}
}
for(int i = currentPiece.file() + 1; i <= 8; i++){
if(tempChessBoard[currentPiece.rank()][i] != NULL && tempChessBoard[currentPiece.rank()][i]->owner() == currentPiece.owner()){
break;
}
else if(tempChessBoard[currentPiece.rank()][i] != NULL && tempChessBoard[currentPiece.rank()][i]->owner() != currentPiece.owner()
&& ((char)tempChessBoard[currentPiece.rank()][i]->type() == 'R' || (char)tempChessBoard[currentPiece.rank()][i]->type() == 'Q')){
isCheck = true;
return isCheck;
}
}
for(int i = currentPiece.file() - 1; i > 0; i--){
if(tempChessBoard[currentPiece.rank()][i] != NULL && tempChessBoard[currentPiece.rank()][i]->owner() == currentPiece.owner()){
break;
}
else if(tempChessBoard[currentPiece.rank()][i] != NULL && tempChessBoard[currentPiece.rank()][i]->owner() != currentPiece.owner()
&& ((char)tempChessBoard[currentPiece.rank()][i]->type() == 'R' || (char)tempChessBoard[currentPiece.rank()][i]->type() == 'Q')){
isCheck = true;
return isCheck;
}
}
//Checking all 4 diagonal lines after the king for a Bishop or Queen piece of other player that is able to take the king.
int breakFlag = 0;
int rankCounter = 0;
int fileCounter;
for(int i = currentPiece.rank() + 1; i <= 8; i++){
fileCounter = 0;
for(int j = currentPiece.file() + 1; j <= 8; j++){
if(rankCounter == fileCounter){
if(tempChessBoard[i][j] != NULL && tempChessBoard[i][j]->owner() == currentPiece.owner()){
breakFlag = 1;
break;
}
else if(tempChessBoard[i][j] != NULL && tempChessBoard[i][j]->owner() != currentPiece.owner()
&& ((char)tempChessBoard[i][j]->type() == 'B' || (char)tempChessBoard[i][j]->type() == 'Q')){
isCheck = true;
return isCheck;
}
}
fileCounter++;
}
if(breakFlag == 1){
break;
}
rankCounter++;
}
breakFlag = 0;
rankCounter = 0;
for(int i = currentPiece.rank() - 1; i > 0; i--){
fileCounter = 0;
for(int j = currentPiece.file() + 1; j <= 8; j++){
if(rankCounter == fileCounter){
if(tempChessBoard[i][j] != NULL && tempChessBoard[i][j]->owner() == currentPiece.owner()){
breakFlag = 1;
break;
}
else if(tempChessBoard[i][j] != NULL && tempChessBoard[i][j]->owner() != currentPiece.owner()
&& ((char)tempChessBoard[i][j]->type() == 'B' || (char)tempChessBoard[i][j]->type() == 'Q')){
isCheck = true;
return isCheck;
}
}
fileCounter++;
}
if(breakFlag == 1){
break;
}
rankCounter++;
}
breakFlag = 0;
rankCounter = 0;
for(int i = currentPiece.rank() + 1; i <= 8; i++){
fileCounter = 0;
for(int j = currentPiece.file() - 1; j > 0; j--){
if(rankCounter == fileCounter){
if(tempChessBoard[i][j] != NULL && tempChessBoard[i][j]->owner() == currentPiece.owner()){
breakFlag = 1;
break;
}
else if(tempChessBoard[i][j] != NULL && tempChessBoard[i][j]->owner() != currentPiece.owner()
&& ((char)tempChessBoard[i][j]->type() == 'B' || (char)tempChessBoard[i][j]->type() == 'Q')){
isCheck = true;
return isCheck;
}
}
fileCounter++;
}
if(breakFlag == 1){
break;
}
rankCounter++;
}
breakFlag = 0;
rankCounter = 0;
for(int i = currentPiece.rank() - 1; i > 0; i--){
fileCounter = 0;
for(int j = currentPiece.file() - 1; j > 0; j--){
if(rankCounter == fileCounter){
if(tempChessBoard[i][j] != NULL && tempChessBoard[i][j]->owner() == currentPiece.owner()){
breakFlag = 1;
break;
}
else if(tempChessBoard[i][j] != NULL && tempChessBoard[i][j]->owner() != currentPiece.owner()
&& ((char)tempChessBoard[i][j]->type() == 'B' || (char)tempChessBoard[i][j]->type() == 'Q')){
isCheck = true;
return isCheck;
}
}
fileCounter++;
}
if(breakFlag == 1){
break;
}
rankCounter++;
}
//Checking for 8 possible places that enemy horse can be located in order to take the king.
if(currentPiece.rank() + 2 <= 8 && currentPiece.file() + 1 <= 8
&& tempChessBoard[currentPiece.rank() + 2][currentPiece.file() + 1] != NULL
&& (char)tempChessBoard[currentPiece.rank() + 2][currentPiece.file() + 1]->type() == 'N'
&& tempChessBoard[currentPiece.rank() + 2][currentPiece.file() + 1]->owner() != currentPiece.owner()){
isCheck = true;
return isCheck;
}
if(currentPiece.rank() + 2 <= 8 && currentPiece.file() - 1 > 0
&& tempChessBoard[currentPiece.rank() + 2][currentPiece.file() - 1] != NULL
&& (char)tempChessBoard[currentPiece.rank() + 2][currentPiece.file() - 1]->type() == 'N'
&& tempChessBoard[currentPiece.rank() + 2][currentPiece.file() - 1]->owner() != currentPiece.owner()){
isCheck = true;
return isCheck;
}
if(currentPiece.rank() - 2 > 0 && currentPiece.file() + 1 <= 8
&& tempChessBoard[currentPiece.rank() - 2][currentPiece.file() + 1] != NULL
&& (char)tempChessBoard[currentPiece.rank() - 2][currentPiece.file() + 1]->type() == 'N'
&& tempChessBoard[currentPiece.rank() - 2][currentPiece.file() + 1]->owner() != currentPiece.owner()){
isCheck = true;
return isCheck;
}
if(currentPiece.rank() - 2 > 0 && currentPiece.file() - 1 > 0
&& tempChessBoard[currentPiece.rank() - 2][currentPiece.file() - 1] != NULL
&& (char)tempChessBoard[currentPiece.rank() - 2][currentPiece.file() - 1]->type() == 'N'
&& tempChessBoard[currentPiece.rank() - 2][currentPiece.file() - 1]->owner() != currentPiece.owner()){
isCheck = true;
return isCheck;
}
if(currentPiece.rank() + 1 <= 8 && currentPiece.file() + 2 <= 8
&& tempChessBoard[currentPiece.rank() + 1][currentPiece.file() + 2] != NULL
&& (char)tempChessBoard[currentPiece.rank() + 1][currentPiece.file() + 2]->type() == 'N'
&& tempChessBoard[currentPiece.rank() + 1][currentPiece.file() + 2]->owner() != currentPiece.owner()){
isCheck = true;
return isCheck;
}
if(currentPiece.rank() + 1 <= 8 && currentPiece.file() - 2 > 0
&& tempChessBoard[currentPiece.rank() + 1][currentPiece.file() - 2] != NULL
&& (char)tempChessBoard[currentPiece.rank() + 1][currentPiece.file() - 2]->type() == 'N'
&& tempChessBoard[currentPiece.rank() + 1][currentPiece.file() - 2]->owner() != currentPiece.owner()){
isCheck = true;
return isCheck;
}
if(currentPiece.rank() - 1 > 0 && currentPiece.file() + 2 <= 8
&& tempChessBoard[currentPiece.rank() - 1][currentPiece.file() + 2] != NULL
&& (char)tempChessBoard[currentPiece.rank() - 1][currentPiece.file() + 2]->type() == 'N'
&& tempChessBoard[currentPiece.rank() - 1][currentPiece.file() + 2]->owner() != currentPiece.owner()){
isCheck = true;
return isCheck;
}
if(currentPiece.rank() - 1 > 0 && currentPiece.file() - 2 > 0
&& tempChessBoard[currentPiece.rank() - 1][currentPiece.file() - 2] != NULL
&& (char)tempChessBoard[currentPiece.rank() - 1][currentPiece.file() - 2]->type() == 'N'
&& tempChessBoard[currentPiece.rank() - 1][currentPiece.file() - 2]->owner() != currentPiece.owner()){
isCheck = true;
return isCheck;
}
return isCheck;
}
std::ostream& operator<<(std::ostream& stream,Piece ob)
{
stream << "id: " << ((_Piece*)ob.ptr)->id <<'\n';
stream << "owner: " << ((_Piece*)ob.ptr)->owner <<'\n';
stream << "file: " << ((_Piece*)ob.ptr)->file <<'\n';
stream << "rank: " << ((_Piece*)ob.ptr)->rank <<'\n';
stream << "hasMoved: " << ((_Piece*)ob.ptr)->hasMoved <<'\n';
stream << "type: " << ((_Piece*)ob.ptr)->type <<'\n';
return stream;
}