-
Notifications
You must be signed in to change notification settings - Fork 0
/
chesspiece.cpp
315 lines (278 loc) · 10.2 KB
/
chesspiece.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
#include "chesspiece.h"
#include "chessboard.h"
#include <QDebug>
#include<iostream>
ChessPiece::ChessPiece(Type type, Color color) : type(type), color(color) {}
ChessPiece::Type ChessPiece::getType() const
{
return type;
}
ChessPiece::Color ChessPiece::getColor() const
{
return color;
}
void ChessPiece::setType(Type t)
{
type = t;
}
bool ChessPiece::isEmpty() const
{
return ( type == Empty );
}
// Validation code all pieces
bool ChessPiece::isValidMove_Rook(Type type, int fromRow, int fromCol, int toRow, int toCol, const ChessBoard& board) const
{
int i;
if(type == Rook){
if(fromRow == toRow && fromCol == toCol)
return false; // Case when the piece is dropped at same location
// A Rook can move horizontal & Vertical
// Colision detection
// From Row: 7 to 0 & Col: 0 to 7
if(fromRow == toRow){
// Moved Horizontally
if(fromCol < toCol){
// Move right
for(i = fromCol + 1; i < toCol; i++){
if(!board.getPiece(fromRow, i).isEmpty())
return isValidCapture(fromRow, fromCol, toRow, toCol, board);
}
} else {
// Move left
for(i = fromCol - 1; i > toCol; i--){
if(!board.getPiece(fromRow, i).isEmpty())
return isValidCapture(fromRow, fromCol, toRow, toCol, board);
}
}
} else if (fromCol == toCol){
// Vertical Move
if(fromRow < toRow){
// Move down
for(i = fromRow + 1; i < toRow; i++){
if(!board.getPiece(i, fromCol).isEmpty())
return isValidCapture(fromRow, fromCol, toRow, toCol, board);
}
} else {
// Move up
for(i = fromRow - 1; i > toRow; i--){
if(!board.getPiece(i, fromCol).isEmpty())
return isValidCapture(fromRow, fromCol, toRow, toCol, board);
}
}
} else {
// Not a valid move
return false;
}
}
return true;
}
bool ChessPiece::isValidMove_King(Type type,int fromRow, int fromCol, int toRow, int toCol, ChessBoard& board) const
{
if (type == King) {
// Check if the destination is the same as the source
if (fromRow == toRow && fromCol == toCol)
return false;
// King can move one square in any direction (horizontal, vertical, or diagonal)
int rowDiff = qAbs(toRow - fromRow);
int colDiff = qAbs(toCol - fromCol);
qDebug() << qAbs(toRow - fromRow) << (toRow - fromRow);
qDebug() << board.getPieceType(fromRow, fromCol) << board.getPieceType(toRow, toCol);
if ((rowDiff == 1 && colDiff == 0) || // Vertical move
(rowDiff == 0 && colDiff == 1) || // Horizontal move
(rowDiff == 1 && colDiff == 1)) // Diagonal move
{
return true;
}
}
// Not a valid move for the King
return false;
}
bool ChessPiece::isValidMove_Queen(int fromRow,int fromCol,int toRow,int toCol,ChessBoard& board)const{
//A Queen can move horizontally, vertically and also diagonally.
int rowDiff = toRow - fromRow; //This is used to move diagonally.
int colDiff = toCol - fromCol;
int i;
if(fromCol==toCol && fromRow==toRow)
return false; //case when piece dropped in same location.
//Collision Detection Conditions
if(fromRow==toRow){
//Moved Horizontally
if(fromCol<toCol){
//Then move right
for(i=fromCol+1;i<toCol;i++){
if(!board.getPiece(fromRow,i).isEmpty()) //getPiece returns the piece reference at current row and col.
return isValidCapture(fromRow,fromCol,toRow,toCol,board); // isValidCapture returns true if colors are same.
}
}
else{
//Moved in left Position
for(i=fromCol-1;i>toCol;i--){
if(!board.getPiece(fromRow,i).isEmpty())
return isValidCapture(fromRow,fromCol,toRow,toCol,board);
}
}
}else if (fromCol == toCol) {
// Move in Vertical Position.
if (fromRow < toRow) {
// Moved Down
for (int i = fromRow + 1; i < toRow; i++) {
if (!board.getPiece(i, fromCol).isEmpty()) {
return isValidCapture(fromRow, fromCol, toRow, toCol, board);
}
}
} else {
// Moved up
for (int i = fromRow - 1; i > toRow; i--) {
if (!board.getPiece(i, fromCol).isEmpty()) {
return isValidCapture(fromRow, fromCol, toRow, toCol, board);
}
}
}
return true; // return true if the path is clear
}
//return false; // Not a valid move (neither horizontal, vertical, nor diagonal)
// Moved Diagonally
else if (abs(rowDiff) ==abs(colDiff))
{
// this will determine the direction of movement
int rowIncrement = (rowDiff > 0) ? 1 : -1;
int colIncrement = (colDiff > 0) ? 1 : -1;
for (int i = fromRow + rowIncrement, j = fromCol + colIncrement;
i != toRow; i += rowIncrement, j += colIncrement)
{
if (board.isSquareOccupied(i, j)) // Check if any square along the diagonal path is occupied
{
return false; // if space is occupied then it is return false.
}
}
return true; // return true if the path is clear
}
else{
return false;
}
}
bool ChessPiece::isValidMove_Pawn( int fromRow, int fromCol, int toRow, int toCol, const ChessBoard& board) const
{
int rowDiff=toRow-fromRow, colDiff=toCol-fromCol;
if ( board.getPieceColor(fromRow,fromCol)==Black && colDiff==0)//check if piece is black and moving stright or not.
{
if (rowDiff==1)//check if piece is moving only one square
{
if (!board.isSquareOccupied(toRow,toCol))// check if destination square is occupied or not.
return true;
}
else if (rowDiff==2 && fromRow==1)// check if piece is moving 2 squares from initial position.
{
if (!board.isSquareOccupied(toRow,toCol) && !board.isSquareOccupied(fromRow + 1, fromCol))
return true;
}
}
else if ( board.getPieceColor(fromRow,fromCol)==White && colDiff==0)//check if white and moving stright or not.
{
if (rowDiff == -1)//check if piece is moving only one square
{
if (!board.isSquareOccupied(toRow,toCol))// check if destination square is occupied or not.
return true;
}
else if (rowDiff==-2 && fromRow==6)// check if piece is moving 2 squares from initial position.
{
if (!board.isSquareOccupied(toRow,toCol) && !board.isSquareOccupied(fromRow - 1, fromCol))
return true;
}
}
// Diagonal capture conditions.
else if ((colDiff == 1 || colDiff == -1) && rowDiff == 1 &&
board.getPieceColor(fromRow,fromCol) == Black &&
board.isSquareOccupied(toRow,toCol))
{
return true;
}
else if ((colDiff == 1 || colDiff == -1) && rowDiff == -1 &&
board.getPieceColor(fromRow,fromCol)==White &&
board.isSquareOccupied(toRow,toCol))
{
return true;
}
return false;
}
bool ChessPiece::isValidMove_Knight(int fromRow, int fromCol, int toRow, int toCol) const
{
// Knight logic
// Night moves in L pattern
// Move up-left (two row up one left)
// Move up-right (two row up one right)
// Move bottom-left (two row down one left)
// Move bottom-right (two row down one right)
if(fromRow == toRow && fromCol == toCol)
return false;
int rowDiff = (toRow > fromRow) ? (toRow - fromRow) : (fromRow - toRow);
int colDiff = (toCol > fromCol) ? (toCol - fromCol) : (fromCol - toCol);
if ((rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2)){
// Valid Knight Move
return true;
}
return false;
}
bool ChessPiece::isValidMove_Bishop( int fromRow, int fromCol, int toRow, int toCol, const ChessBoard& board) const
{
int rowDiff=toRow-fromRow, colDiff=toCol-fromCol;
int RowDiff_positive=(rowDiff<0)?(rowDiff*-1):(rowDiff),ColDiff_positive=(colDiff<0)?(colDiff*-1):(colDiff);
if ( RowDiff_positive == ColDiff_positive)
{
if (rowDiff>0 && colDiff>0)
{
for (int i=fromRow+1,j=fromCol+1; i<toRow && j<toCol; i++,j++)
{
if (board.isSquareOccupied(i,j))
{
return false;
}
}
return true;
}
else if (rowDiff<0 && colDiff<0)
{
for (int i=fromRow-1, j=fromCol-1; i>toRow && j>toCol; i--, j-- )
{
if(board.isSquareOccupied(i,j))
{
return false;
}
}
return true;
}
else if (rowDiff<0 && colDiff>0)
{
for (int i=fromRow-1, j=fromCol+1; i>toRow && j<toCol; i--, j++ )
{
if (board.isSquareOccupied(i,j))
{
return false;
}
}
return true;
}
else if (rowDiff>0 && colDiff<0)
{
for (int i=fromRow+1 ,j=fromCol-1; i<toRow && j>toCol; i++,j-- )
{
if (board.isSquareOccupied(i,j))
{
return false;
}
}
return true;
}
}
return false;
}
// isValidCapture returns true if colors are same.
bool ChessPiece::isValidCapture(int fromRow, int fromCol, int toRow, int toCol, const ChessBoard &board) const
{
// Valid capture logic here
if(!board.isValidChessSquare(toRow, toCol))
return false;
ChessPiece piece = board.getPiece(fromRow, fromCol);
ChessPiece nextPiece = board.getPiece(toRow, toCol);
return (nextPiece.getColor() == piece.getColor());
}