-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.cpp
401 lines (366 loc) · 13.6 KB
/
pieces.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
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
#include "pieces.h"
// chess board:
// 0 - empty;
// 1 - 7 - my pieces
// 11 - 17 - enemy pieces
// return values:
// 0 - invalid move (out of board or my piece)
// 1 - empty cell
// x - enemy piece and we give a score accordinglly to the importance of the piece << TODO >>
// 2 - king
// 3 - pawn
// 4 - knight
// 5 - bishop
// 6 - rook
// 7 - queen
int check_validity(char i, char j, std::vector<std::vector<char>> &chess_board) {
if (i < 0 || i >= BOX_LENGTH || j < 0 || j >= BOX_LENGTH) return 0;
if (chess_board[i][j] == 0) return 1; // empty cell
if (chess_board[i][j] < 10) return 0; // my piece
if (chess_board[i][j] > 10) {
int temp = chess_board[i][j];
if (temp == PAWN_E) return PRIORITY_PAWN; // pawn
else if (temp == ROOK_E) return PRIORITY_ROOK; // rook
else if (temp == KNIGHT_E) return PRIORITY_KNIGHT; // knight
else if (temp == BLACK_BISHOP_E) return PRIORITY_BLACK_BISHOP; // black bishop
else if (temp == WHITE_BISHOP_E) return PRIORITY_WHITE_BISHOP; // white bishop
else if (temp == QUEEN_E) return PRIORITY_QUEEN; // queen
else if (temp == KING_E) return PRIORITY_KING; // king
else return 2;
}
return 0; // my piece
}
int check_validity_pawn(char i, char j, std::vector<std::vector<char>> &chess_board) {
int temp = check_validity(i, j, chess_board);
if (temp <= 1) return 0;
else return temp;
}
std::vector<char> get_piece_position(char p, int d,
std::vector<std::vector<char>> &positions) {
std::vector<char> piece_position;
char i = -1, j = -1; // piece positions
int contor = 0;
for (int k = 0; k < 16; k++) {
if (positions[k][2] == p) {
if (contor == d) {
i = positions[k][0];
j = positions[k][1];
break;
} else {
++contor;
}
}
}
piece_position.push_back(i);
piece_position.push_back(j);
return piece_position;
}
// return vector structure: {x, y, "check_validity" return value}
std::vector<std::vector<char>> get_piece_directions(char i, char j, std::vector<std::vector<char>> &chess_board) {
std::vector<std::vector<char>> possible_moves;
char piece_type = chess_board[i][j];
switch (piece_type) {
case 1: {// pawn
if (i == 1 && chess_board[2][j] == EMPTY_CELL) { // pt cand pionul se misca 2 patratele
check_move(i, j, 2, 0, possible_moves, chess_board);
}
check_move(i, j, 1, 0, possible_moves, chess_board);
int temp = check_validity_pawn(i + 1, j - 1, chess_board); // adversar pe diagonala stanga
if (temp >= 2) {
std::vector<char> move;
move.push_back(i + 1);
move.push_back(j - 1);
move.push_back(temp);
possible_moves.push_back(move);
}
temp = check_validity_pawn(i + 1, j + 1, chess_board); // adversar pe diagonala dreapta
if (temp >= 2) {
std::vector<char> move;
move.push_back(i + 1);
move.push_back(j + 1);
move.push_back(temp);
possible_moves.push_back(move);
}
break;
}
case 2: // rook
check_move_up(i, j, possible_moves, chess_board);
check_move_down(i, j, possible_moves, chess_board);
check_move_left(i, j, possible_moves, chess_board);
check_move_right(i, j, possible_moves, chess_board);
break;
case 3: // knight
// front moves
check_move(i, j, 2, -1, possible_moves, chess_board);
check_move(i, j, 2, 1, possible_moves, chess_board);
check_move(i, j, 1, -2, possible_moves, chess_board);
check_move(i, j, 1, 2, possible_moves, chess_board);
// back moves
check_move(i, j, -2, -1, possible_moves, chess_board);
check_move(i, j, -2, 1, possible_moves, chess_board);
check_move(i, j, -1, -2, possible_moves, chess_board);
check_move(i, j, -1, 2, possible_moves, chess_board);
break;
case 4: // black bishop
case 5: // white bishop
right_diag_up(i, j, possible_moves, chess_board);
left_diag_up(i, j, possible_moves, chess_board);
right_diag_down(i, j, possible_moves, chess_board);
left_diag_down(i, j, possible_moves, chess_board);
break;
case 6: // queen
check_move_up(i, j, possible_moves, chess_board);
check_move_down(i, j, possible_moves, chess_board);
check_move_left(i, j, possible_moves, chess_board);
check_move_right(i, j, possible_moves, chess_board);
right_diag_up(i, j, possible_moves, chess_board);
left_diag_up(i, j, possible_moves, chess_board);
right_diag_down(i, j, possible_moves, chess_board);
left_diag_down(i, j, possible_moves, chess_board);
break;
case 7: // king
check_move(i, j, 1, 0, possible_moves, chess_board);
check_move(i, j, -1, 0, possible_moves, chess_board);
check_move(i, j, 0, 1, possible_moves, chess_board);
check_move(i, j, 0, -1, possible_moves, chess_board);
check_move(i, j, 1, 1, possible_moves, chess_board);
check_move(i, j, 1, -1, possible_moves, chess_board);
check_move(i, j, -1, 1, possible_moves, chess_board);
check_move(i, j, -1, -1, possible_moves, chess_board);
break;
default:
break;
}
return possible_moves;
}
// std::vector<char> move_piece(char x_s, char y_s, char x_d, char y_d,
// std::vector<std::vector<char>> &positions,
// std::vector<std::vector<char>> &e_positions,
// std::vector<std::vector<char>> &chess_board) {
// if (chess_board[x_s][y_s] == 0) {
// std::cout << "#probleme\n";
// return NULL;
// }
//
// char piece_type = chess_board[x_s][y_s];
// if (piece_type < 10) { //it's my piece
// std::vector<char> pos_rmv;
// if (chess_board[x_d][y_d] != 0) { // taking enemy's piece
// pos_rmv.push_back(x_d);
// pos_rmv.push_back(y_d);
// pos_rmv.push_back(chess_board[x_d][y_d]);
// auto it = std::find(e_positions.begin(), e_positions.end(), pos_rmv);
// e_positions.erase(it);
// }
// chess_board[x_d][y_d] = chess_board[x_s][y_s];
// chess_board[x_s][y_s] = 0;
// std::vector<char> pos;
// pos.push_back(x_s);
// pos.push_back(y_s);
// pos.push_back(piece_type);
// auto it = std::find(positions.begin(), positions.end(), pos);
// positions.erase(it);
// pos.erase(it);
// pos.push_back(x_d);
// pos.push_back(y_d);
// pos.push_back(piece_type);
// positions.push_back(pos);
// // TODO
// // make the special move the rook and the king
// return pos_rmv;
// } else { // it's enemy's piece that's moving
// std::vector<char> pos_rmv;
// if (chess_board[x_d][y_d] != 0) { // taking my piece
// pos_rmv.push_back(x_d);
// pos_rmv.push_back(y_d);
// pos_rmv.push_back(chess_board[x_d][y_d]);
// auto it = std::find(positions.begin(), positions.end(), pos);
// positions.erase(it);
// }
// chess_board[x_d][y_d] = chess_board[x_s][y_s];
// chess_board[x_s][y_s] = 0;
// std::vector<char> pos;
// pos.push_back(x_s);
// pos.push_back(y_s);
// pos.push_back(piece_type);
// auto it = std::find(e_positions.begin(), e_positions.end(), pos);
// e_positions.erase(it);
// pos.erase(it);
// pos.push_back(x_d);
// pos.push_back(y_d);
// pos.push_back(piece_type);
// e_positions.push_back(pos);
// return pos_rmv;
// }
// }
// --------------------------------------------- Robert's attemp --------------------------------
// here we get the moves for the bishop
void rook_moves(char &i, char &j,
std::vector<std::vector<char>> &possible_moves,
std::vector<std::vector<char>> &chess_board) {
get_direct_moves(i, j, up, possible_moves, chess_board);
get_direct_moves(i, j, dw, possible_moves, chess_board);
get_direct_moves(i, j, rg, possible_moves, chess_board);
get_direct_moves(i, j, lf, possible_moves, chess_board);
}
void bishop_moves(char &i, char &j,
std::vector<std::vector<char>> &possible_moves,
std::vector<std::vector<char>> &chess_board) {
get_direct_moves(i, j, ur, possible_moves, chess_board);
get_direct_moves(i, j, ul, possible_moves, chess_board);
get_direct_moves(i, j, dr, possible_moves, chess_board);
get_direct_moves(i, j, dl, possible_moves, chess_board);
}
// get directional moves
// pair d<x, y> gives the direction in whitch to search for moves
// good for the following pieces:
// -rook
// -bishop
// -queen
// example: (1, 0) = goes forward; (-1, -1) = goes on the down left diagonal
void get_direct_moves(char i, char j, std::pair<char, char> &d,
std::vector<std::vector<char>> &possible_moves,
std::vector<std::vector<char>> &chess_board) {
char x = d.first;
char y = d.second;
while (true) {
int temp = check_validity(i + x, j + y, chess_board);
if (temp == 0) break; // our piece and is an invalid move
std::vector<char> move;
move.push_back(i + x);
move.push_back(j + y);
move.push_back(temp);
possible_moves.push_back(move);
if (temp >= 2) break; // it's an enemy piece and we can not jump over it
i += x;
j += y;
}
}
// --------------------------------------------- Teo's moves --------------------------------
void check_move_up(char i, char j, std::vector<std::vector<char>> &possible_moves,
std::vector<std::vector<char>> &chess_board) {
while (true) {
int temp = check_validity(i + 1, j, chess_board);
if (temp == 0) break;
std::vector<char> move;
move.push_back(i + 1);
move.push_back(j);
move.push_back(temp);
possible_moves.push_back(move);
if (temp >= 2) break;
++i;
}
}
void check_move_down(char i, char j, std::vector<std::vector<char>> &possible_moves,
std::vector<std::vector<char>> &chess_board) {
while (true) {
int temp = check_validity(i - 1, j, chess_board);
if (temp == 0) break;
std::vector<char> move;
move.push_back(i - 1);
move.push_back(j);
move.push_back(temp);
possible_moves.push_back(move);
if (temp >= 2) break;
--i;
}
}
void check_move_left(char i, char j, std::vector<std::vector<char>> &possible_moves,
std::vector<std::vector<char>> &chess_board) {
while (true) {
int temp = check_validity(i, j - 1, chess_board);
if (temp == 0) break;
std::vector<char> move;
move.push_back(i);
move.push_back(j - 1);
move.push_back(temp);
possible_moves.push_back(move);
if (temp >= 2) break;
--j;
}
}
void check_move_right(char i, char j, std::vector<std::vector<char>> &possible_moves,
std::vector<std::vector<char>> &chess_board) {
while (true) {
int temp = check_validity(i, j + 1, chess_board);
if (temp == 0) break;
std::vector<char> move;
move.push_back(i);
move.push_back(j + 1);
move.push_back(temp);
possible_moves.push_back(move);
if (temp >= 2) break;
++j;
}
}
void check_move(char i, char j, int i_depl, int j_depl, std::vector<std::vector<char>> &possible_moves,
std::vector<std::vector<char>> &chess_board) {
int temp = check_validity(i + i_depl, j + j_depl, chess_board);
if (temp) {
if (chess_board[i][j] == PAWN_M && temp != 1) {
return;
}
std::vector<char> move;
move.push_back(i + i_depl);
move.push_back(j + j_depl);
move.push_back(temp);
possible_moves.push_back(move);
}
}
// ------------------------------------------ Ollie's diagonal moves 8) --------------------------------
void right_diag_up(char i, char j, std::vector<std::vector<char>> &possible_moves, std::vector<std::vector<char>> &chess_board) {
while (true) {
int temp = check_validity(i + 1, j + 1, chess_board); // 1 - empty ; 2 - enemy
if (temp == 0) break;
std::vector<char> move;
move.push_back(i + 1);
move.push_back(j + 1);
move.push_back(temp);
possible_moves.push_back(move);
if (temp >= 2) break;
++i;
++j;
}
}
void left_diag_up(char i, char j, std::vector<std::vector<char>> &possible_moves, std::vector<std::vector<char>> &chess_board) {
while (true) {
int temp = check_validity(i + 1, j - 1, chess_board);
if (temp == 0) break;
std::vector<char> move;
move.push_back(i + 1);
move.push_back(j - 1);
move.push_back(temp);
possible_moves.push_back(move);
if (temp >= 2) break;
++i;
--j;
}
}
void right_diag_down(char i, char j, std::vector<std::vector<char>> &possible_moves, std::vector<std::vector<char>> &chess_board) {
while (true) {
int temp = check_validity(i - 1, j + 1, chess_board);
if (temp == 0) break;
std::vector<char> move;
move.push_back(i - 1);
move.push_back(j + 1);
move.push_back(temp);
possible_moves.push_back(move);
if (temp >= 2) break;
--i;
++j;
}
}
void left_diag_down(char i, char j, std::vector<std::vector<char>> &possible_moves, std::vector<std::vector<char>> &chess_board) {
while (true) {
int temp = check_validity(i - 1, j - 1, chess_board);
if (temp == 0) break;
std::vector<char> move;
move.push_back(i - 1);
move.push_back(j - 1);
move.push_back(temp);
possible_moves.push_back(move);
if (temp >= 2) break;
--i;
--j;
}
}