-
Notifications
You must be signed in to change notification settings - Fork 0
/
King.cpp
375 lines (331 loc) · 8.59 KB
/
King.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
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
#include "StdAfx.h"
#include "Piece.h"
#include "Rook.h"
#include "King.h"
#include "Board.h"
#include "Square.h"
CKing::CKing(EColour theColour, CBoard* pBoard, short thePosition)
: CPiece(theColour, pBoard, thePosition), moved(false), castled(false), eCastleType(eUnknown)
{
}
CKing::~CKing(void)
{
}
bool CKing::move(short sourceRow, short targetRow, short sourceColumn, short targetColumn)
{
bool bResult = true;
if (!CPiece::move(sourceRow, targetRow, sourceColumn, targetColumn))
return false;
if (isMoveCastling(sourceRow, targetRow, sourceColumn, targetColumn))
{
bool bExitLoop = false;
CRook* pRook = NULL;
if (targetColumn > sourceColumn)
{
eCastleType = eRightCastle;
}
else
{
eCastleType = eLeftCastle;
}
// find correct good guy's rook
for (auto rookFinderIt = pBoard->getSquares().begin(); (rookFinderIt != pBoard->getSquares().end()) && !bExitLoop; ++rookFinderIt)
{
if ((*rookFinderIt)->isSquareOccupied())
{
if ((*rookFinderIt)->getPiece()->getColour() == eColour)
{
pRook = dynamic_cast<CRook*> ((*rookFinderIt)->getPiece());
if (pRook != NULL)
{
if (((*rookFinderIt)->getRow() == sourceRow) && ((*rookFinderIt)->getColumn() == sourceColumn))
{
// it's the correct good guy's rook
bExitLoop = true;
}
else // reset the rook pointer as this is not the right one
{
pRook = NULL;
}
}
}
}
}
// SORT THIS OUT
// castle if we have the correct rook
/*if (pRook != NULL)
{
if (eCastleType == eLeftCastle)
{
xcolumn = 1;
ycolumn = 3;
}
else if (eCastleType == eRightCastle)
{
xcolumn = 8;
ycolumn = 6;
}
if (eColour == eWhite)
{
xrow = 1;
yrow = 1;
}
else if (eColour == eBlack)
{
xrow = 8;
yrow = 8;
}
if (!pRook->move(getPositionFromRowAndColumn(xrow, xcolumn), getPositionFromRowAndColumn(yrow, ycolumn)))
{
bResult = false;
}
}*/
}
moved = true;
return bResult;
}
bool CKing::isMoveLegal(short xrow, short yrow, short xcolumn, short ycolumn)
{
if ((xrow == (yrow + 1)) && (xcolumn == ycolumn)
|| (xrow == (yrow - 1)) && (xcolumn == ycolumn)
|| (xrow == yrow) && (xcolumn == (ycolumn + 1))
|| (xrow == yrow) && (xcolumn == (ycolumn - 1))
|| (xrow == (yrow + 1)) && (xcolumn == (ycolumn - 1))
|| (xrow == (yrow - 1)) && (xcolumn == (ycolumn - 1))
|| (xrow == (yrow - 1)) && (xcolumn == (ycolumn + 1))
|| (xrow == (yrow + 1)) && (xcolumn == (ycolumn + 1)))
{
return true;
}
// we may be castling so first ensure that we are not in check
/*else if (inCheck())
{
return false;
}*/ //TBD
// now ensure that we are moving adjacent to a rook and that the rook and king have never moved in the current game
else if (isMoveCastling(xrow, yrow, xcolumn, ycolumn) && !rookHasMoved() && !moved)
{
castled = true;
return true;
}
else
{
return false;
}
}
bool CKing::isMoveBlocked(short xrow, short yrow, short xcolumn, short ycolumn)
{
bool bResult = false;
if (isMoveCastling(xrow, xcolumn, yrow, ycolumn))
{
short row = 0;
std::vector<short> columns;
columns.clear();
if (eCastleType == eLeftCastle)
{
columns.push_back(2);
columns.push_back(3);
columns.push_back(4);
}
// eRightCastle
else
{
columns.push_back(6);
columns.push_back(7);
}
if (eColour == eWhite)
{
row = 1;
}
// eBlack
else
{
row = 8;
}
// if any of the spaces that the king must travel through to castle are occupied the move is blocked
for (auto it = columns.begin(); it != columns.end(); ++it)
{
if (pBoard->getSquares()[getPositionFromRowAndColumn(row, *it)]->isSquareOccupied())
{
bResult = true;
break;
}
}
}
// same colour piece on target square is a block
if (isTargetSquareBlocked(yrow, ycolumn))
{
bResult = true;
}
return bResult;
}
bool CKing::inCheck()
{
// go through all nemesis player's pieces and do isMoveLegal and isMoveBlocked using the good guy's King as the target square
short xrow = 0;
short xcolumn = 0;
short yrow = 0;
short ycolumn = 0;
bool bExitLoop = false;
// find the good guy's King
for (auto kingFinderIt = pBoard->getSquares().begin(); (kingFinderIt != pBoard->getSquares().end()) && !bExitLoop; ++kingFinderIt)
{
if ((*kingFinderIt)->isSquareOccupied())
{
if ((*kingFinderIt)->getPiece()->getColour() == eColour) // it's the good guy's King
{
yrow = (*kingFinderIt)->getRow();
ycolumn = (*kingFinderIt)->getColumn();
bExitLoop = true;
}
}
}
for (auto it = pBoard->getSquares().begin(); it != pBoard->getSquares().end(); ++it)
{
if ((*it)->isSquareOccupied())
{
if ((*it)->getPiece()->getColour() != eColour) // it's a nemesis piece
{
xrow = (*it)->getRow();
xcolumn = (*it)->getColumn();
// y's are this King
if ((*it)->getPiece()->isMoveLegal(xrow, yrow, xcolumn, ycolumn) && !(*it)->getPiece()->isMoveBlocked(xrow, yrow, xcolumn, ycolumn))
{
return true;
}
}
}
}
return false;
}
bool CKing::checkMate()
{
short xrow = 0;
short xcolumn = 0;
pBoard->getRowsAndColumnsFromPosition(position, xrow, xcolumn);
if (inCheck() && allLegalMovesAreInCheck(xrow, xcolumn))
{
return true;
}
return false;
}
bool CKing::allLegalMovesAreInCheck(short xrow, short xcolumn)
{
if (isMoveLegal(xrow, xrow+1, xcolumn, xcolumn) && !isMoveBlocked(xrow, xrow+1, xcolumn, xcolumn) && !inCheckTest(xrow+1, xcolumn))
{
return false;
}
else if (isMoveLegal(xrow, xrow-1, xcolumn, xcolumn) && !isMoveBlocked(xrow, xrow-1, xcolumn, xcolumn) && !inCheckTest(xrow-1, xcolumn))
{
return false;
}
else if (isMoveLegal(xrow, xrow, xcolumn, xcolumn+1) && !isMoveBlocked(xrow, xrow, xcolumn, xcolumn+1) && !inCheckTest(xrow, xcolumn+1))
{
return false;
}
else if (isMoveLegal(xrow, xrow, xcolumn, xcolumn-1) && !isMoveBlocked(xrow, xrow, xcolumn, xcolumn-1) && !inCheckTest(xrow, xcolumn-1))
{
return false;
}
else if (isMoveLegal(xrow, xrow+1, xcolumn, xcolumn+1) && !isMoveBlocked(xrow, xrow+1, xcolumn, xcolumn+1) && !inCheckTest(xrow+1, xcolumn+1))
{
return false;
}
else if (isMoveLegal(xrow, xrow-1, xcolumn, xcolumn-1) && !isMoveBlocked(xrow, xrow-1, xcolumn, xcolumn-1) && !inCheckTest(xrow-1, xcolumn-1))
{
return false;
}
else if (isMoveLegal(xrow, xrow+1, xcolumn, xcolumn-1) && !isMoveBlocked(xrow, xrow+1, xcolumn, xcolumn-1) && !inCheckTest(xrow+1, xcolumn-1))
{
return false;
}
else if (isMoveLegal(xrow, xrow-1, xcolumn, xcolumn+1) && !isMoveBlocked(xrow, xrow-1, xcolumn, xcolumn+1) && !inCheckTest(xrow-1, xcolumn+1))
{
return false;
}
else
{
return true;
}
}
bool CKing::inCheckTest(short yrow, short ycolumn)
{
short xrow = 0;
short xcolumn = 0;
for (auto it = pBoard->getSquares().begin(); it != pBoard->getSquares().end(); ++it)
{
if ((*it)->isSquareOccupied())
{
if ((*it)->getPiece()->getColour() != eColour) // it's a nemesis piece
{
xcolumn = (*it)->getColumn();
xrow = (*it)->getRow();
// y's are this King
if ((*it)->getPiece()->isMoveLegal(xrow, yrow, xcolumn, ycolumn) && !(*it)->getPiece()->isMoveBlocked(xrow, yrow, xcolumn, ycolumn))
{
return true;
}
}
}
}
return false;
}
bool CKing::rookHasMoved()
{
CRook* pRook = NULL;
if (eColour == eWhite)
{
pRook = getRook(eBlack);
}
else if (eColour == eBlack)
{
pRook = getRook(eWhite);
}
else
{
return false;
}
return pRook->hasMoved();
}
bool CKing::isMoveCastling(short xrow, short yrow, short xcolumn, short ycolumn)
{
if (ycolumn > xcolumn)
{
if ((xrow == yrow) && ((ycolumn - xcolumn) == 2))
{
return true;
}
}
else if (xcolumn > ycolumn)
{
if ((xrow == yrow) && ((xcolumn - ycolumn) == 2))
{
return true;
}
}
return false;
}
CRook* CKing::getRook(EColour theColour)
{
short xrow = 0;
short xcolumn = 0;
bool bExitLoop = false;
CRook* pRook = NULL;
// find the good guy's King
for (auto rookFinderIt = pBoard->getSquares().begin(); (rookFinderIt != pBoard->getSquares().end()) && !bExitLoop; ++rookFinderIt)
{
if ((*rookFinderIt)->isSquareOccupied())
{
if ((*rookFinderIt)->getPiece()->getColour() == eColour)
{
CRook* pRook = dynamic_cast<CRook*> ((*rookFinderIt)->getPiece());
if (pRook != NULL)
{
// it's the good guy's rook
return pRook;
}
}
}
}
return pRook;
}