-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchess.cpp
More file actions
282 lines (251 loc) · 8.62 KB
/
Copy pathchess.cpp
File metadata and controls
282 lines (251 loc) · 8.62 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
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
// Author: Simone Madeo
#include "types.h"
#define FILE_INPUT // Comment this to test the program online
/* Check if knight in (r, c) for color myColor can be played */
inline void checkKnight(Board &board, int r, int c, int myColor, vector<Coord> &x)
{
if (r < BOARD_DIM && c < BOARD_DIM && r >= 0 && c >= 0 && board.getColor(r, c) != myColor) {
x.push_back(Coord(r, c));
}
}
/* Return all the possible moves for piece x */
void enumerate(Board &board, Piece &x, int myColor, int opponentQueenID, vector<Coord> &moves)
{
int foundCheckmate = -1;
int i, j;
int r = x.pos.r;
int c = x.pos.c;
char piece = x.type;
square_type curColor;
if (piece == 'Q' || piece == 'R') {
/* Horizontal/vertical moves */
for (i = r + 1, j = c; i < BOARD_DIM; i++) {
curColor = board.getColor(i, j);
if (curColor != myColor)
moves.push_back(Coord(i, j));
if (curColor != EMPTY)
break;
}
for (i = r, j = c + 1; j < BOARD_DIM; j++) {
curColor = board.getColor(i, j);
if (curColor != myColor)
moves.push_back(Coord(i, j));
if (curColor != EMPTY)
break;
}
for (i = r - 1, j = c; i >= 0; i--) {
curColor = board.getColor(i, j);
if (curColor != myColor)
moves.push_back(Coord(i, j));
if (curColor != EMPTY)
break;
}
for (i = r, j = c - 1; j >= 0; j--) {
curColor = board.getColor(i, j);
if (curColor != myColor)
moves.push_back(Coord(i, j));
if (curColor != EMPTY)
break;
}
}
if (piece == 'Q' || piece == 'B') {
/* Diagonal moves */
for (i = r + 1, j = c + 1; (i < BOARD_DIM && j < BOARD_DIM); i++, j++) {
curColor = board.getColor(i, j);
if (curColor != myColor)
moves.push_back(Coord(i, j));
if (curColor != EMPTY)
break;
}
for (i = r + 1, j = c - 1; (i < BOARD_DIM && j >= 0); i++, j--) {
curColor = board.getColor(i, j);
if (curColor != myColor)
moves.push_back(Coord(i, j));
if (curColor != EMPTY)
break;
}
for (i = r - 1, j = c + 1; (i >= 0 && j < BOARD_DIM); i--, j++) {
curColor = board.getColor(i, j);
if (curColor != myColor)
moves.push_back(Coord(i, j));
if (curColor != EMPTY)
break;
}
for (i = r - 1, j = c - 1; (i >= 0 && j >= 0); i--, j--) {
curColor = board.getColor(i, j);
if (curColor != myColor)
moves.push_back(Coord(i, j));
if (curColor != EMPTY)
break;
}
}
if (piece == 'N') {
/* Knight moves */
checkKnight(board, r + 2, c + 1, myColor, moves);
checkKnight(board, r + 2, c - 1, myColor, moves);
checkKnight(board, r + 1, c - 2, myColor, moves);
checkKnight(board, r + 1, c + 2, myColor, moves);
checkKnight(board, r - 2, c + 1, myColor, moves);
checkKnight(board, r - 2, c - 1, myColor, moves);
checkKnight(board, r - 1, c + 2, myColor, moves);
checkKnight(board, r - 1, c - 2, myColor, moves);
}
/* Optimization: if the opposite queen can be captured, other combinations
are discarded */
for (int i = 0; i < moves.size() && foundCheckmate == -1; i++)
if (board.getID(moves[i].r, moves[i].c) == opponentQueenID)
foundCheckmate = i;
if (foundCheckmate != -1) {
moves[0] = moves[foundCheckmate];
moves.resize(1);
}
}
/* Recursive function controlling the end of the game: return TRUE if white has
a forced win.
board: current board status,
A: player who is moving
B: the other player
n: move number
m: max moves allowed
pieceID: id of A's moving piece
r: new row for the moving piece
c: new column for the moving piece
queenAID, id of A's queen
queenBID, id of B's queen
*/
bool moves(Board board, vector<Piece> A, vector<Piece> B, int n, int m,
int pieceID, int r, int c, int queenAID, int queenBID)
{
int playerID = (n % 2) + 1; // 1 (white) or 2 (black)
int opponentID = ((n + 1) % 2) + 1; // 1 (white) or 2 (black)
int old_r = A[pieceID].pos.r; // previos position of the moving piece
int old_c = A[pieceID].pos.c; // previos position of the moving piece
int takenSquareID = board.getID(r, c); // new square of the moving piece
int nB = B.size();
bool result;
/* Remove captured piece */
if (takenSquareID != -1) {
/* Check for checkmate */
if (B[takenSquareID].type == 'Q') {
if (playerID == WHITE) {
/* White moved */
return true;
} else {
/* Black moved */
return false;
}
}
B[takenSquareID].active = false;
}
/* Check number of moves */
if (n == m - 1)
return false;
/* Update the squares */
board.set(r, c, pieceID, (square_type)playerID);
board.set(old_r, old_c, -1, EMPTY);
/* Update the position of the moving piece */
A[pieceID].pos.r = r;
A[pieceID].pos.c = c;
/* Analyze all opponent's possible moves */
for (int i = 0; i < nB; i++) {
if (B[i].active) {
vector<Coord> nextMove;
enumerate(board, B[i], opponentID, queenAID, nextMove);
for (int j = 0; j < nextMove.size(); j++) {
result = moves(board, B, A, n + 1, m, i, nextMove[j].r,
nextMove[j].c, queenBID, queenAID);
if (opponentID == WHITE && result) {
// At least one white combination led to forced checkmate
vector<Coord>().swap(nextMove);
return true;
}
else if (opponentID == BLACK && !result) {
// At least one black comb. did not lead to forced checkmate
vector<Coord>().swap(nextMove);
return false;
}
}
vector<Coord>().swap(nextMove);
}
}
if (opponentID == BLACK) {
/* All black combinations led to forced checkmate */
return true;
}
/* At least one white combination did not lead to forced checkmate */
return false;
}
int main(int argc, char *argv[])
{
int g, w, b, m, r, r_, c_, wQueenID, bQueenID;
char c, p;
bool result;
#ifdef FILE_INPUT
FILE *fp;
char tmp[1];
if (argc != 2) {
cout << "Usage: chess [textfile]\n";
exit(1);
}
fp = fopen(argv[1], "r");
fscanf(fp, "%i", &g);
#else
cin >> g;
#endif
for (int z = 0; z < g; z++) {
Board board;
vector<Piece> W, B;
result = false;
#ifdef FILE_INPUT
fscanf(fp, "%i", &w);
fscanf(fp, "%i", &b);
fscanf(fp, "%i", &m);
#else
cin >> w >> b >> m;
#endif
for (int j = 0; j < w + b; j++) {
#ifdef FILE_INPUT
fscanf(fp, "%s", tmp);
p = tmp[0];
fscanf(fp, "%s", tmp);
c = tmp[0];
fscanf(fp, "%s", tmp);
r = atoi(tmp);
#else
cin >> p >> c >> r;
#endif
r_ = BOARD_DIM - r;
c_ = c - 'A';
if (j < w) {
/* Update white pieces */
W.push_back(Piece(p, r_, c_));
board.set(r_, c_, j, WHITE);
if (p == 'Q')
wQueenID = j;
} else {
/* Update black pieces */
B.push_back(Piece(p, r_, c_));
board.set(r_, c_, j - w, BLACK);
if (p == 'Q')
bQueenID = j - w;
}
}
// Use 'board.print(W, B);' to print the current checkboard
/* Check all white's first moves */
for (int i = 0; i < W.size() && !result; i++) {
vector<Coord> nextMove;
enumerate(board, W[i], 1, bQueenID, nextMove);
for (int j = 0; j < nextMove.size() && !result; j++)
result = moves(board, W, B, 0, m, i, nextMove[j].r, nextMove[j].c, wQueenID, bQueenID);
vector<Coord>().swap(nextMove);
}
if (result)
cout << "YES\n";
else
cout << "NO\n";
}
#ifdef FILE_INPUT
fclose(fp);
#endif
return 0;
}