-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpiece.cc
474 lines (426 loc) · 15.2 KB
/
piece.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <vector>
#include "piece.h"
using namespace std;
Piece::Piece(Colour colour, PieceType type):
colour{colour}, type{type} {}
Piece::Piece(int row, int col, PieceType type, Colour colour):
row{row}, col{col}, type{type}, colour{colour} {} // Default constructor
// Returns thex value of row.
int Piece::getRow() const {
return row;
}
// Returns the value of col.
int Piece::getCol() const {
return col;
}
void Piece::setRow(int r) {
row = r;
}
void Piece::setCol(int c) {
col = c;
}
PieceType Piece::getState() const {
return type;
}
Colour Piece::getColour() const {
return colour;
}
void Piece::setCoords(int r, int c) {
row = r;
col = c;
}
void Piece::attach(Observer *o) {
observers.emplace_back(o);
} // Attaches observers (TextDisplay and GraphicDisplay)
// Piece will call this to let observers TextDisplay and GraphicDisplay know that they've moved so that the displays can update accordingly
void Piece::notifyObservers() {
for (auto p: observers) {
p->notify(this, getRow(), getCol());
}
}
bool Piece::checkDiagonal(Board &b, int newRow, int newCol) {
int rise = newRow - row;
int run = newCol - col;
int boardSize = b.getBoardSize();
// Check that the path of the bishop is either y = x or y = -x.
if (abs(rise) != abs(run)) {
return false;
}
// Check SE direction.
if (rise > 0 && run > 0) {
for (int i = 1; i < rise && newRow + i < boardSize && newCol + i < boardSize; ++i) {
if (!b.getPiece(row + i, col + i)) {
return false;
}
}
}
// Check NW direction.
else if (rise < 0 && run < 0) {
for (int i = 1; i < rise && newRow - i > 0 && newCol - i > 0; ++i) {
if (b.getPiece(row - i, col - i) != nullptr) {
return false;
}
}
}
// Check NE direction.
else if (rise < 0 && run > 0) {
for (int i = 1; i < rise && newRow - i > 0 && newCol + i < boardSize; ++i) {
if (b.getPiece(row - i, col + i) != nullptr) {
return false;
}
}
}
// Check SE direction.
else {
for (int i = 1; i < rise && newRow + i < boardSize && newCol - i > 0; ++i) {
if (b.getPiece(row + i, col - i)) {
return false;
}
}
}
// If there is nothing in the way, return true.
return true;
}
bool Piece::checkHorizontalVertical(const Board &b, int newRow, int newCol) {
int rise = newRow - row; // Rise is positive when you are moving down.
int run = newCol - col; // Run is positive when you are moving right.
if ((abs(rise) > 0) && (abs(run) > 0)) { // Cannot move both on row and column simultaneously.
return false;
}
if (run == 0 && rise > 0) { // Checking vertical down.
for (int i = 1; i < rise; ++i) {
if (b.getPiece(row + i, col)) return false; // Piece found.
}
} else if (run == 0 && rise < 0) { // Checking vertical up.
for (int i = 1; i < abs(rise); ++i) {
if (b.getPiece(row - i, col)) return false;
}
} else if (rise == 0 && run > 0) { // Checking horizontal right.
for (int i = 1; i < run; ++i) {
if (b.getPiece(row, col + i)) return false;
}
} else if (rise == 0 && run < 0) { // Checking horizontal left.
for (int i = 1; i < abs(run); ++i) {
if (b.getPiece(row, col - i)) return false;
}
}
return true;
}
bool Piece::checkForCheckPreMove(Board &b, int newRow, int newCol) {
int origRow = getRow();
int origCol = getCol();
Colour c = getColour();
// Check if there are Pieces that attack king in the new position.
Piece * attacking = b.getPiece(newRow, newCol); // Keep track of what we are temporarily removing.
b.placePieceTemporarily(origRow, origCol, newRow, newCol);
if (b.checkIfInCheck(c)) {
b.placePieceTemporarily(newRow, newCol, origRow, origCol);
b.setPiece(attacking, newRow, newCol);
return false;
}
// Return Piece to original square.
b.placePieceTemporarily(newRow, newCol, origRow, origCol);
b.setPiece(attacking, newRow, newCol);
return true;
}
void Piece::capturePiece(Board &b, int captureRow, int captureCol) {
b.removePiece(captureRow, captureRow);
// delete b.getPiece(captureRow, captureCol);
// notifyObserversCapture(captureRow, captureCol);
}
void Piece::placePiece(Board & b, int row, int col) {
int origRow = this->row;
int origCol = this->col;
setCoords(row, col);
b.removePiece(row, col);
b.setPiece(this, row, col);
b.setPiece(nullptr, origRow, origCol);
// b.removePiece(origRow, origCol);
this->notifyObservers();
}
vector<Piece *> Piece::checkAttackingMe(const Board &b, int row, int col, Colour c) {
vector<Piece *> pieces{};
int boardSize = b.getBoardSize();
Piece * currPiece;
Piece * p;
// Check diagonals and rows/columns for the first Queen, Rook, or Bishop or opposite Colour.
int tempR = row - 1;
int tempC = col;
// Check N
while (tempR >= 0) {
p = b.getPiece(tempR, tempC);
if (p && p->getColour() == getColour()) {
break;
} else if (p) { // enemy piece
if (p->getState() == PieceType::Queen || p->getState() == PieceType::Rook) {
pieces.emplace_back(p);
}
break;
}
--tempR;
}
// Check E
tempR = row;
tempC = col + 1;
while (tempC < boardSize) {
p = b.getPiece(tempR, tempC);
if (p && p->getColour() == getColour()) {
break;
} else if (p) { // enemy piece
if (p->getState() == PieceType::Queen || p->getState() == PieceType::Rook) {
pieces.emplace_back(p);
}
break;
}
++tempC;
}
// Check S
tempR = row + 1;
tempC = col;
while (tempR < boardSize) {
p = b.getPiece(tempR, tempC);
if (p && p->getColour() == getColour()) {
break;
} else if (p) { // enemy piece
if (p->getState() == PieceType::Queen || p->getState() == PieceType::Rook) {
pieces.emplace_back(p);
}
break;
}
++tempR;
}
// Check W
tempR = row;
tempC = col - 1;
while (tempC >= 0) {
p = b.getPiece(tempR, tempC);
if (p && p->getColour() == getColour()) {
break;
} else if (p) { // enemy piece
if (p->getState() == PieceType::Queen || p->getState() == PieceType::Rook) {
pieces.emplace_back(p);
}
break;
}
--tempC;
}
// Check NE
tempR = row - 1;
tempC = col + 1;
while (tempR >= 0 && tempC < boardSize) {
p = b.getPiece(tempR, tempC);
if (p && p->getColour() == getColour()) {
break;
} else if (p) { // enemy piece
if (p->getState() == PieceType::Queen || p->getState() == PieceType::Bishop) {
pieces.emplace_back(p);
}
break;
}
--tempR;
++tempC;
}
// Check SE
tempR = row + 1;
tempC = col + 1;
while (tempR < boardSize && tempC < boardSize) {
p = b.getPiece(tempR, tempC);
if (p && p->getColour() == getColour()) {
break;
} else if (p) { // enemy piece
if (p->getState() == PieceType::Queen || p->getState() == PieceType::Bishop) {
pieces.emplace_back(p);
}
break;
}
++tempR;
++tempC;
}
// Check SW
tempR = row + 1;
tempC = col - 1;
while (tempC >= 0 && tempR < boardSize) {
p = b.getPiece(tempR, tempC);
if (p && p->getColour() == getColour()) {
break;
} else if (p) { // enemy piece
if (p->getState() == PieceType::Queen || p->getState() == PieceType::Bishop) {
pieces.emplace_back(p);
}
break;
}
++tempR;
--tempC;
}
// Check NW
tempR = row - 1;
tempC = col - 1;
while (tempC >= 0 && tempR >= 0) {
p = b.getPiece(tempR, tempC);
if (p && p->getColour() == getColour()) {
break;
} else if (p) { // enemy piece
if (p->getState() == PieceType::Queen || p->getState() == PieceType::Bishop) {
pieces.emplace_back(p);
}
break;
}
--tempR;
--tempC;
}
// Check for Pawns where White moves up the board and Black moves down.
if (c == Colour::WHITE && row > 0) {
if (col > 0) { // Check that there is a column to the left of you.
// Check if there is a Piece NW of current Piece, and it is a Black Pawn.
if (b.getPiece(row - 1, col - 1) && (b.getPiece(row - 1, col - 1)->getState() == PieceType::Pawn) && (b.getPiece(row - 1, col - 1)->getColour() == Colour::BLACK)) {
pieces.emplace_back(b.getPiece(row - 1, col - 1));
}
}
if (col + 1 < boardSize) { // Check that there is a column to the right of you.
// Check if there is a Piece NE of current Piece, and it is a Black Pawn.
if (b.getPiece(row - 1, col + 1) && (b.getPiece(row - 1, col + 1)->getState() == PieceType::Pawn) && (b.getPiece(row - 1, col + 1)->getColour() == Colour::BLACK)) {
pieces.emplace_back(b.getPiece(row - 1, col + 1));
}
}
} else if (row + 1 < boardSize) {
if (col > 0) { // Check that there is a column to the left of you.
// Check if there is a Piece SW of current Piece, and it is a White Pawn.
if (b.getPiece(row + 1, col - 1) && (b.getPiece(row + 1, col - 1)->getState() == PieceType::Pawn) && (b.getPiece(row + 1, col - 1)->getColour() == Colour::WHITE)) {
pieces.emplace_back(b.getPiece(row + 1, col - 1));
}
}
if (col + 1 < boardSize) { // Check that there is a column to the right of you.
// Check if there is a Piece SE of current Piece, and it is a White Pawn.
if (b.getPiece(row + 1, col + 1) && (b.getPiece(row + 1, col + 1)->getState() == PieceType::Pawn) && (b.getPiece(row + 1, col + 1)->getColour() == Colour::WHITE)) {
pieces.emplace_back(b.getPiece(row + 1, col + 1));
}
}
}
// Check for King.
if (row + 1 < boardSize) {
// Check S of p.
currPiece = b.getPiece(row + 1, col);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::King)) {
pieces.emplace_back(currPiece);
}
// Check SE of p.
if (col + 1 < boardSize) {
currPiece = b.getPiece(row + 1, col + 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::King)) {
pieces.emplace_back(currPiece);
}
}
// Check SW of p.
if (col > 0) {
currPiece = b.getPiece(row + 1, col - 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::King)) {
pieces.emplace_back(currPiece);
}
}
}
if (row > 0) {
// Check N of p.
currPiece = b.getPiece(row - 1, col);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::King)) {
pieces.emplace_back(currPiece);
}
// Check NE of p.
if (col + 1 < boardSize) {
currPiece = b.getPiece(row - 1, col + 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::King)) {
pieces.emplace_back(currPiece);
}
}
// Check NW of p.
if (col > 0) {
currPiece = b.getPiece(row - 1, col - 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::King)) {
pieces.emplace_back(currPiece);
}
}
}
// Check E of p.
if (col + 1 < boardSize) {
currPiece = b.getPiece(row, col + 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::King)) {
pieces.emplace_back(currPiece);
}
}
// Check W of p.
if (col > 0) {
currPiece = b.getPiece(row, col - 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::King)) {
pieces.emplace_back(currPiece);
}
}
// Check Knights.
// Check top-most two.
if (row >= 2) {
if (col + 1 < boardSize) {
currPiece = b.getPiece(row - 2, col + 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::Knight)) {
pieces.emplace_back(currPiece);
}
}
if (col > 0) {
currPiece = b.getPiece(row - 2, col - 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::Knight)) {
pieces.emplace_back(currPiece);
}
}
}
// Check right-most two.
if (col + 2 < boardSize) { // we are in the 5 leftmost cols
if (row + 1 < boardSize) {
currPiece = b.getPiece(row + 1, col + 2);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::Knight)) {
pieces.emplace_back(currPiece);
}
}
if (row > 0) { // we are not in top row
currPiece = b.getPiece(row - 1, col + 2);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::Knight)) {
pieces.emplace_back(currPiece);
}
}
}
// Check bottom-most two.
if (row + 2 < boardSize) { // top 5 rows
if (col + 1 < boardSize) {
currPiece = b.getPiece(row + 2, col + 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::Knight)) {
pieces.emplace_back(currPiece);
}
}
if (col > 0) { // we are not in leftmost col
currPiece = b.getPiece(row + 2, col - 1);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::Knight)) {
pieces.emplace_back(currPiece);
}
}
}
//Check left-most two.
if (col >= 2) {
if (row + 1 < boardSize) {
currPiece = b.getPiece(row + 1, col - 2);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::Knight)) {
pieces.emplace_back(currPiece);
}
}
if (row > 0) {
currPiece = b.getPiece(row - 1, col - 2);
if (currPiece && (currPiece->getColour() != c) && (currPiece->getState() == PieceType::Knight)) {
pieces.emplace_back(currPiece);
}
}
}
return pieces;
}
vector<vector<int>> Piece::checkEnPassantMoves(Board &b) {
return vector<vector<int>>{{-1,-1}};
}
bool Piece::checkForPromotion(int row) {
return false;
}
Piece::~Piece() {
}