-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generate Moves.h
510 lines (404 loc) · 15.5 KB
/
Generate Moves.h
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//
// Moves.h
// Cmd Line Chess AI
//
// Created by Robert Lewis on 1/12/21.
// Copyright © 2021 Robert Lewis. All rights reserved.
//
#include "Boards.h"
bool inCheck(int location, std::array<piece, 64> currentBoard, int kingColour);
std::array<piece, 64> makeMove(std::array<piece, 64> oldBoard, int colourToMove, int move);
std::vector<int> pawnMove(int tLocation, int tColour, std::array<piece, 64> currentBoard) {
std::vector<int> moveList;
//Forward
int location = tLocation + 8 * tColour;
if (currentBoard[location].colour == 0) {
if(location >= 56 or location <= 7) moveList.push_back(50000 + 100 * tLocation + location);
else moveList.push_back(100 * tLocation + location);
location += 8 * tColour;
//Forward x 2
if (currentBoard[location].colour == 0 and currentBoard[tLocation].move2) {
moveList.push_back(40000 + 100 * tLocation + location);
}
}
//Capture
location = tLocation + 1 + 8 * tColour;
if (currentBoard[location].colour == -1 * tColour and location % 8 == tLocation % 8 + 1) {
if(location >= 56 or location <= 7) moveList.push_back(50000 + 100 * tLocation + location);
else moveList.push_back(100 * tLocation + location);
}
location = tLocation - 1 + 8 * tColour;
if (currentBoard[location].colour == -1 * tColour and location % 8 == tLocation % 8 - 1) {
if(location >= 56 or location <= 7) moveList.push_back(50000 + 100 * tLocation + location);
else moveList.push_back(100 * tLocation + location);
}
//En Passant
if (currentBoard[tLocation + 1].enPassant) moveList.push_back(30000 + tLocation * 100 + tLocation + 9);
if (currentBoard[tLocation - 1].enPassant) moveList.push_back(30000 + tLocation * 100 + tLocation + 7);
return moveList;
}
std::vector<int> rookMove(int tLocation, int tColour, std::array<piece, 64> currentBoard) {
std::vector<int> moveList;
//Left
int location = tLocation % 8;
int offset = -1;
while (location + offset >= 0) {
if (currentBoard[tLocation + offset].colour != tColour) {
moveList.push_back(100 * tLocation + (tLocation + offset));
if (currentBoard[tLocation + offset].colour == -1 * tColour) {
break;
}
}
else break;
offset --;
}
//Right
offset = 1;
while (location + offset < 8) {
if (currentBoard[tLocation + offset].colour != tColour) {
moveList.push_back(100 * tLocation + (tLocation + offset));
if (currentBoard[tLocation + offset].colour == -1 * tColour) {
break;
}
}
else break;
offset ++;
}
//Down
offset = -1;
while (tLocation + offset * 8 >= 0) {
if (currentBoard[tLocation + offset * 8].colour != tColour) {
moveList.push_back(100 * tLocation + (tLocation + offset * 8));
if (currentBoard[tLocation + offset * 8].colour == -1 * tColour) {
break;
}
}
else break;
offset --;
}
//Up
offset = 1;
while (tLocation + offset * 8 < 64) {
if (currentBoard[tLocation + offset * 8].colour != tColour) {
moveList.push_back(100 * tLocation + (tLocation + offset * 8));
if (currentBoard[tLocation + offset * 8].colour == -1 * tColour) {
break;
}
}
else break;
offset ++;
}
return moveList;
}
std::vector<int> knightMove(int tLocation, int tColour, std::array<piece, 64> currentBoard) {
std::vector<int> moveList;
int file = tLocation % 8;
if (file + 1 < 8){
if(tLocation + 17 < 64 and currentBoard[tLocation + 17].colour != tColour) moveList.push_back(tLocation * 100 + tLocation + 17);
if (tLocation - 15 >= 0 and currentBoard[tLocation - 15].colour != tColour) moveList.push_back(tLocation * 100 + tLocation - 15);
if (file + 2 < 8) {
if(tLocation + 10 < 64 and currentBoard[tLocation + 10].colour != tColour) moveList.push_back(tLocation * 100 + tLocation + 10);
if (tLocation - 6 >= 0 and currentBoard[tLocation - 6].colour != tColour) moveList.push_back(tLocation * 100 + tLocation - 6);
}
}
if (file - 1 >= 0){
if(tLocation + 15 < 64 and currentBoard[tLocation + 15].colour != tColour) moveList.push_back(tLocation * 100 + tLocation + 15);
if (tLocation - 17 >= 0 and currentBoard[tLocation - 17].colour != tColour) moveList.push_back(tLocation * 100 + tLocation - 17);
if (file - 2 >= 0) {
if(tLocation + 6 < 64 and currentBoard[tLocation + 6].colour != tColour) moveList.push_back(tLocation * 100 + tLocation + 6);
if (tLocation - 10 >= 0 and currentBoard[tLocation - 10].colour != tColour) moveList.push_back(tLocation * 100 + tLocation - 10);
}
}
return moveList;
}
std::vector<int> bishopMove(int tLocation, int tColour, std::array<piece, 64> currentBoard) {
std::vector<int> moveList;
//Down Left
int location = tLocation % 8;
int offset = -1;
while (location + offset >= 0 and tLocation + offset * 9 >= 0) {
if (currentBoard[tLocation + offset * 9].colour != tColour) {
moveList.push_back(tLocation * 100 + tLocation + 9 * offset);
if (currentBoard[tLocation + offset * 9].colour == -1 * tColour) {
break;
}
}
else break;
offset --;
}
//Down Right
offset = -1;
while (location - offset < 8 and tLocation + offset * 7 >= 0) {
if (currentBoard[tLocation + offset * 7].colour != tColour) {
moveList.push_back(tLocation * 100 + tLocation + 7 * offset);
if (currentBoard[tLocation + offset * 7].colour == -1 * tColour) {
break;
}
}
else break;
offset --;
}
//Up Right
offset = 1;
while (location + offset < 8 and tLocation + offset * 9 < 64) {
if (currentBoard[tLocation + offset * 9].colour != tColour) {
moveList.push_back(tLocation * 100 + tLocation + 9 * offset);
if (currentBoard[tLocation + offset * 9].colour == -1 * tColour) {
break;
}
}
else break;
offset ++;
}
//Up Left
offset = 1;
while (location - offset >= 0 and tLocation + offset * 7 < 64) {
if (currentBoard[tLocation + offset * 7].colour != tColour) {
moveList.push_back(tLocation * 100 + tLocation + 7 * offset);
if (currentBoard[tLocation + offset * 7].colour == -1 * tColour) {
break;
}
}
else break;
offset ++;
}
return moveList;
}
std::vector<int> queenMove(int tLocation, int tColour, std::array<piece, 64> currentBoard) {
std::vector<int> moveList = bishopMove(tLocation, tColour, currentBoard);
std::vector<int> rookMoveList = rookMove(tLocation, tColour, currentBoard);
moveList.insert(std::end(moveList), std::begin(rookMoveList), std::end(rookMoveList));
return moveList;
}
std::vector<int> kingMove(int tLocation, int tColour, std::array<piece, 64> currentBoard) {
std::vector<int> moveList;
int location = tLocation % 8;
//Up
int moveTo = tLocation + 8;
if (currentBoard[moveTo].colour != tColour and moveTo < 64) {
moveList.push_back(tLocation * 100 + moveTo);
}
//Up Right
moveTo = tLocation + 9;
if (currentBoard[moveTo].colour != tColour and moveTo < 64 and location + 1 < 8) {
moveList.push_back(tLocation * 100 + moveTo);
}
//Right
moveTo = tLocation + 1;
if (currentBoard[moveTo].colour != tColour and location + 1 < 8) {
moveList.push_back(tLocation * 100 + moveTo);
}
//Down Right
moveTo = tLocation - 7;
if (currentBoard[moveTo].colour != tColour and moveTo >= 0 and location + 1 < 8) {
moveList.push_back(tLocation * 100 + moveTo);
}
//Down
moveTo = tLocation - 8;
if (currentBoard[moveTo].colour != tColour and moveTo >= 0) {
moveList.push_back(tLocation * 100 + moveTo);
}
//Down Left
moveTo = tLocation - 9;
if (currentBoard[moveTo].colour != tColour and moveTo >= 0 and location - 1 >= 0) {
moveList.push_back(tLocation * 100 + moveTo);
}
//Left
moveTo = tLocation - 1;
if (currentBoard[moveTo].colour != tColour and location - 1 >= 0) {
moveList.push_back(tLocation * 100 + moveTo);
}
//Up Left
moveTo = tLocation + 7;
if (currentBoard[moveTo].colour != tColour and moveTo < 64 and location - 1 >= 0) {
moveList.push_back(tLocation * 100 + moveTo);
}
//Castle Kingside
if (currentBoard[tLocation].castle == true and currentBoard[tLocation + 1].colour == 0 and currentBoard[tLocation + 2].colour == 0 and currentBoard[tLocation + 3].castle == true and currentBoard[tLocation + 3].colour == tColour) {
if (!inCheck(tLocation, currentBoard, tColour) and !inCheck(tLocation + 1, currentBoard, tColour)) {
moveList.push_back(10000 + tLocation * 100 + tLocation + 2);
}
}
//Castle Queenside
if (currentBoard[tLocation].castle == true and currentBoard[tLocation - 1].colour == 0 and currentBoard[tLocation - 2].colour == 0 and currentBoard[tLocation - 3].colour == 0 and currentBoard[tLocation - 4].castle == true and currentBoard[tLocation - 4].colour == tColour) {
if (!inCheck(tLocation, currentBoard, tColour) and !inCheck(tLocation - 1, currentBoard, tColour)) {
moveList.push_back(20000 + tLocation * 100 + tLocation - 2);
}
}
return moveList;
}
int findKing(std::array<piece, 64> currentBoard, int kingColour) {
for (int i = 0; i < 64; i ++) {
if (currentBoard[i].value == 500 * kingColour) return i;
}
printBoard(currentBoard);
while (true) std::cout << "NO King!!!!!!!!!!!! " << kingColour << std::endl;
}
std::vector<int> piece::generatePieceMove(int location, int colour, char name, std::array<piece, 64> currentBoard) {
char ID = std::tolower(name);
std::vector<int> moveList;
switch (ID) {
case 'p':
moveList = pawnMove(location, colour, currentBoard);
break;
case 'r':
moveList = rookMove(location, colour, currentBoard);
break;
case 'n':
moveList = knightMove(location, colour, currentBoard);
break;
case 'b':
moveList = bishopMove(location, colour, currentBoard);
break;
case 'q':
moveList = queenMove(location, colour, currentBoard);
break;
case 'k':
moveList = kingMove(location, colour, currentBoard);
break;
case ' ':
moveList = {};
break;
}
return moveList;
}
std::vector<int> generateAllMoves(std::array<piece, 64> currentBoard, int colourToMove) {
std::vector<int> moveList;
for(int i = 0; i < 64; i ++) {
std::vector<int> newMovesList;
if (currentBoard[i].colour == colourToMove) {
newMovesList = currentBoard[i].generatePieceMove(i, currentBoard[i].colour, currentBoard[i].name, currentBoard);
moveList.insert(std::end(moveList), std::begin(newMovesList), std::end(newMovesList));
}
}
int i = 0;
std::array<piece, 64> tempBoard;
while (i < moveList.size()) {
tempBoard = makeMove(currentBoard, colourToMove, moveList[i]);
if (inCheck(findKing(tempBoard, colourToMove), tempBoard, colourToMove)) {
moveList.erase(moveList.begin() + i);
}
else i ++;
}
// for (int i = 0; i < rejectedMoves.size(); i ++) std::cout << rejectedMoves[i] << " ";
return moveList;
}
bool inCheck(int kingLocation, std::array<piece, 64> currentBoard, int kingColour) {
std::vector<int> moveList;
std::vector<int> newMovesList;
int kingFile = kingLocation % 8;
//pawn
int destination = kingLocation + 1 + 8 * kingColour;
if ((kingLocation + 1) % 8 == kingFile + 1 and destination % 64 == destination) {
moveList.push_back(destination);
if (currentBoard[destination].value == -1 * kingColour) return true;
}
destination = kingLocation - 1 + 8 * kingColour;
if ((kingLocation - 1) % 8 == kingFile - 1 and destination % 64 == destination){
moveList.push_back(destination);
if (currentBoard[destination].value == -1 * kingColour) return true;
}
//right
int offset = 1;
destination = kingLocation + offset;
if (abs(destination) % 8 == kingLocation % 8 + offset){
if (currentBoard[destination].value == -500 * kingColour) return true;
while (abs(destination) % 8 == kingLocation % 8 + offset) {
if (currentBoard[destination].value == -5 * kingColour or currentBoard[destination].value == -9 * kingColour) return true;
else if (currentBoard[destination].colour == kingColour) break;
offset ++;
destination = kingLocation + offset;
}
}
//left
offset = -1;
destination = kingLocation + offset;
if (abs(destination) % 8 == kingLocation % 8 + offset){
if (currentBoard[destination].value == -500 * kingColour) return true;
while (abs(destination) % 8 == kingLocation % 8 + offset) {
if (currentBoard[destination].value == -5 * kingColour or currentBoard[destination].value == -9 * kingColour) return true;
else if (currentBoard[destination].colour == kingColour) break;
offset --;
destination = kingLocation + offset;
}
}
//up
offset = 1;
destination = kingLocation + offset * 8;
if (abs(destination) % 64 == destination){
if (currentBoard[destination].value == -500 * kingColour) return true;
while (abs(destination) % 64 == destination) {
if (currentBoard[destination].value == -5 * kingColour or currentBoard[destination].value == -9 * kingColour) return true;
else if (currentBoard[destination].colour == kingColour) break;
offset ++;
destination = kingLocation + offset * 8;
}
}
//down
offset = -1;
destination = kingLocation + offset * 8;
if (abs(destination) % 64 == destination){
if (currentBoard[destination].value == -500 * kingColour) return true;
while (abs(destination) % 64 == destination) {
if (currentBoard[destination].value == -5 * kingColour or currentBoard[destination].value == -9 * kingColour) return true;
else if (currentBoard[destination].colour == kingColour) break;
offset --;
destination = kingLocation + offset * 8;
}
}
//Up Right
offset = 1;
destination = kingLocation + offset * 9;
if (abs(destination) % 64 == destination and abs(destination) % 8 == kingLocation % 8 + offset){
if (currentBoard[destination].value == -500 * kingColour) return true;
while (abs(destination) % 64 == destination and abs(destination) % 8 == kingLocation % 8 + offset) {
if (currentBoard[destination].value == -3 * kingColour or currentBoard[destination].value == -9 * kingColour) return true;
else if (currentBoard[destination].colour == kingColour) break;
offset ++;
destination = kingLocation + offset * 9;
}
}
//Up Left
offset = 1;
destination = kingLocation + offset * 7;
if (abs(destination) % 64 == destination and abs(destination) % 8 == kingLocation % 8 - offset){
if (currentBoard[destination].value == -500 * kingColour) return true;
while (abs(destination) % 64 == destination and abs(destination) % 8 == kingLocation % 8 - offset) {
if (currentBoard[destination].value == -3 * kingColour or currentBoard[destination].value == -9 * kingColour) return true;
else if (currentBoard[destination].colour == kingColour) break;
offset ++;
destination = kingLocation + offset * 7;
}
}
//Down Left
offset = -1;
destination = kingLocation + offset * 7;
if (abs(destination) % 64 == destination and abs(destination) % 8 == kingLocation % 8 - offset){
if (currentBoard[destination].value == -500 * kingColour) return true;
while (abs(destination) % 64 == destination and abs(destination) % 8 == kingLocation % 8 - offset) {
if (currentBoard[destination].value == -3 * kingColour or currentBoard[destination].value == -9 * kingColour) return true;
else if (currentBoard[destination].colour == kingColour) break;
offset --;
destination = kingLocation + offset * 7;
}
}
//Down Right
offset = -1;
destination = kingLocation + offset * 9;
if (abs(destination) % 64 == destination and abs(destination) % 8 == kingLocation % 8 + offset){
if (currentBoard[destination].value == -500 * kingColour) return true;
while (abs(destination) % 64 == destination and abs(destination) % 8 == kingLocation % 8 + offset) {
if (currentBoard[destination].value == -3 * kingColour or currentBoard[destination].value == -9 * kingColour) return true;
else if (currentBoard[destination].colour == kingColour) break;
offset --;
destination = kingLocation + offset * 9;
}
}
//Knight
moveList = knightMove(kingLocation, kingColour, currentBoard);
for (int i = 0; i < moveList.size(); i ++) {
if (currentBoard[moveList[i] % 100].value == -2.5 * kingColour) return true;;
}
return false;
}
/* Moves_h */