-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEngine_AI.cpp
More file actions
666 lines (570 loc) · 16.9 KB
/
Engine_AI.cpp
File metadata and controls
666 lines (570 loc) · 16.9 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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include "engine_ai.h"
#include <windows.h>
#include <ctime>
//Evaluation points
//for material advantage
const int values[] = { 0, 100, 300, 300, 500, 900, 100000};
//Different evaluation bonus points for
//Positional advantage
const int mobility_value = 3;
const int castle_value = 60;
const int early_queen = 20;
const int bishop_long_diagonal = 30;
const int developed_knight = 35;
const int double_bishop = 45;
const int doubled_pawn = 10;
const int pawn_forward_bonus = 5;
moves EngineAI::GetBestMove(int level)
{
total_moves = 0;
quies_extra_max = 0;
moves result;
this->level = level;
int max_time;
bool first_modified = false;
//Game difficulty
switch(level)
{
case AI_EASY:
max_time = 1;
break;
case AI_MEDIUM:
max_time = 4;
break;
case AI_HARD:
max_time = 10;
break;
}
//Decide game stage
if(previous_moves.size()<20)
{
quies_factor = 0.5;
stage = AI_OPENING;
}
else if(previous_moves.size()>50)
{
quies_factor = 0.25;
stage = AI_END_GAME;
}
else
{
quies_factor = 0.25;
stage = AI_MIDDLE_GAME;
}
//Create thread to look for best move
HANDLE hThread = CreateThread(NULL, 0, AIThread, (void*)this, 0, 0);
modified = false;
max_d = 1;
int init_time = time(NULL);
while(true)
{
Sleep(5);
if(modified)
{
first_modified=true;
modified = false;
max_d++;
hThread = CreateThread(NULL, 0, AIThread, (void*)this, 0, 0);
}
if(first_modified && (time(NULL)-init_time>max_time))
{
TerminateThread(hThread, 0);
break;
}
}
return best_move;
}
long unsigned int __stdcall EngineAI::AIThread(void *input)
{
//Create EngineAI object to look for best move
EngineAI *engine_ai = (EngineAI*)input;
engine_ai->best_move = engine_ai->minimax_base(engine_ai->max_d);
engine_ai->modified = true;
return 0;
}
void EngineAI::PrintInfo()
{
//Print AI Level
printf("AI level: ");
switch(level)
{
case AI_EASY:
printf("Easy\n");
break;
case AI_MEDIUM:
printf("Medium\n");
break;
case AI_HARD:
printf("Hard\n");
break;
}
//Print game stage
printf("Game stage: ");
switch(stage)
{
case AI_OPENING:
printf("Opening\n");
break;
case AI_MIDDLE_GAME:
printf("Middle game\n");
break;
case AI_END_GAME:
printf("End game\n");
break;
}
//Print other AI statistics
printf("Base max depth: %u\n", (modified)? max_d-1 : max_d);
printf("Total leaves evaluated: %u\n", total_moves);
printf("Maximum extra moves for quiescence: %u\n", quies_extra_max);
printf("\n");
}
int EngineAI::EvaluateOpening()
{
int result = 0;
//Relative piece values
for(int i=1;i<=6;i++)
result += values[i]*(pieces_lost1[i] - pieces_lost2[i]);
//Add castling bonus
if(castled1)
result-=castle_value;
if(castled2)
result+=castle_value;
//mobility
result += mobility_value*(CountPossibleMoves(false) - CountPossibleMoves(true));
//Early Queen
if(board_matrix[3][0]!=5)
result+=early_queen;
if(board_matrix[3][7]!=-5)
result-=early_queen;
//Doubled pawn
for(int x=0;x<8;x++)
{
bool white_pawn = false;
bool black_pawn = false;
for(int y=0;y<8;y++)
{
if(board_matrix[x][y]==1)
{
if(white_pawn)
result+=doubled_pawn;
else
white_pawn=true;
}
else if(board_matrix[x][y]==-1)
{
if(black_pawn)
result-=doubled_pawn;
else
black_pawn=true;
}
}
}
//Developed knight
if(board_matrix[1][0]!=3)
result-=developed_knight;
if(board_matrix[6][0]!=3)
result-=developed_knight;
if(board_matrix[1][7]!=-3)
result+=developed_knight;
if(board_matrix[6][7]!=-3)
result+=developed_knight;
return result;
}
int EngineAI::EvaluateMiddleGame()
{
int result = 0;
for(int i=1;i<=6;i++)
result += values[i]*(pieces_lost1[i] - pieces_lost2[i]);
//Double bishop
if(pieces_lost1[2]==1)
result+=double_bishop;
if(pieces_lost2[2]==1)
result-=double_bishop;
//Doubled pawn
for(int x=0;x<8;x++)
{
bool white_pawn = false;
bool black_pawn = false;
for(int y=0;y<8;y++)
{
if(board_matrix[x][y]==1)
{
if(white_pawn)
result+=doubled_pawn;
else
white_pawn=true;
}
else if(board_matrix[x][y]==-1)
{
if(black_pawn)
result-=doubled_pawn;
else
black_pawn=true;
}
}
}
return result;
}
int EngineAI::EvaluateEndGame()
{
int result = 0;
for(int i=1;i<=6;i++)
result += values[i]*(pieces_lost1[i] - pieces_lost2[i]);
//Doubled pawn
for(int x=0;x<8;x++)
{
bool white_pawn = false;
bool black_pawn = false;
for(int y=0;y<8;y++)
{
if(board_matrix[x][y]==1)
{
if(white_pawn)
result+=doubled_pawn;
else
white_pawn=true;
}
else if(board_matrix[x][y]==-1)
{
if(black_pawn)
result-=doubled_pawn;
else
black_pawn=true;
}
}
}
//Favor pawn movement
for(int x=0;x<8;x++)
for(int y=0;y<8;y++)
{
if(board_matrix[x][y]==1)
result-=pawn_forward_bonus*(y);
if(board_matrix[x][y]==-1)
result+=pawn_forward_bonus*(8-y);
}
return result;
}
//Main evaluation function
//Calls appropriate evaluation function based
//on game stage
int EngineAI::EvaluateFunction()
{
total_moves++;
switch(stage)
{
case AI_OPENING:
return EvaluateOpening();
case AI_MIDDLE_GAME:
return EvaluateMiddleGame();
case AI_END_GAME:
return EvaluateEndGame();
}
}
//Counts the total number of possible moves
//for a given game state
int EngineAI::CountPossibleMoves(bool player1)
{
int result = 0;
for(int x0=0;x0<8;x0++)
for(int y0=0;y0<8;y0++)
{
if((player1 && board_matrix[x0][y0]>0) || (!player1 && board_matrix[x0][y0]<0))
{
for(int x1=0;x1<8;x1++)
for(int y1=0;y1<8;y1++)
if(IsValidMoveBase(x0, y0, x1, y1))
result++;
}
}
return result;
}
bool move_list_compare(pair<int, moves> t1, pair<int, moves> t2)
{
return t1.first>t2.first;
}
//Minimax part for maximizing player
int EngineAI::maximize(int depth, int max_depth, int alpha, int beta, int extra)
{
if(depth<=max_depth+extra)
{
if(IsCheckmate2())
{
pieces_lost2[6]++;
float result = EvaluateFunction();
pieces_lost2[6]--;
return result;
}
else if(IsStalemate2())
{
return EvaluateFunction();
}
else
{
int max_here = -100000000;
//Create move list
vector<pair<int, moves> > move_list;
for(int x0=0;x0<8;x0++)
{
for(int y0=0;y0<8;y0++)
{
if(board_matrix[x0][y0]<0)
{
for(int x1=0;x1<8;x1++)
{
for(int y1=0;y1<8;y1++)
{
if(IsValidMove(x0,y0,x1,y1))
{
//Do move
int prev_0 = board_matrix[x0][y0];
int prev_1 = MakeMove(x0, y0, x1, y1);
move_list.push_back(make_pair(prev_1, moves(x0, y0, x1, y1)));
//Undo move
UndoMove(prev_0, prev_1, x0, y0, x1, y1);
}
}
}
}
}
}
sort(move_list.begin(), move_list.end(), move_list_compare);
for(vector<pair<int, moves> >::iterator it = move_list.begin();it!=move_list.end();it++)
{
int x0 = (it->second).x0;
int y0 = (it->second).y0;
int x1 = (it->second).x1;
int y1 = (it->second).y1;
//Do move
int prev_0 = board_matrix[x0][y0];
int prev_1 = MakeMove(x0, y0, x1, y1);
if(prev_1 >= 9)
{
pieces_lost1[5]++;
pieces_lost1[prev_1 - 9]++;
}
else if(prev_1 == -7)
castled2 = true;
else if(prev_1 == -8)
pieces_lost1[1]++;
else
pieces_lost1[prev_1]++;
int value_evaluated = minimize(depth+1, max_depth, alpha, beta, extra);
max_here = max(value_evaluated, max_here);
alpha = max(max_here, alpha);
//Undo move
if(prev_1 >= 9)
{
pieces_lost1[5]--;
pieces_lost1[prev_1-9]--;
}
else if(prev_1 == -7)
castled2 = false;
else if(prev_1 == -8)
pieces_lost1[1]--;
else
pieces_lost1[prev_1]--;
UndoMove(prev_0, prev_1, x0, y0, x1, y1);
if(beta<=alpha)
return max_here;
}
return max_here;
}
}
//Quiescence search
int total_pieces_lost = 0;
for(int i=1;i<=6;i++)
total_pieces_lost+= pieces_lost1[i] + pieces_lost2[i];
extra = quies_factor*(float)total_pieces_lost;
if(depth<=max_depth+extra)
{
quies_extra_max = max(extra, quies_extra_max);
return maximize(depth, max_depth, alpha, beta, extra);
}
return EvaluateFunction();
}
//Minimax part for the minimizing player
int EngineAI::minimize(int depth, int max_depth, int alpha, int beta, int extra)
{
if(depth<=max_depth+extra)
{
if(IsCheckmate1())
{
pieces_lost1[6]++;
int result = EvaluateFunction();
pieces_lost1[6]--;
return result;
}
else if(IsStalemate1())
return EvaluateFunction();
else
{
int min_here = 100000000;
//Create move list
vector<pair<int, moves> > move_list;
for(int x0=0;x0<8;x0++)
{
for(int y0=0;y0<8;y0++)
{
if(board_matrix[x0][y0]>0)
{
for(int x1=0;x1<8;x1++)
{
for(int y1=0;y1<8;y1++)
{
if(IsValidMove(x0,y0,x1,y1))
{
//Do move
int prev_0 = board_matrix[x0][y0];
int prev_1 = MakeMove(x0, y0, x1, y1);
move_list.push_back(make_pair(prev_1, moves(x0, y0, x1, y1)));
//Undo move
UndoMove(prev_0, prev_1, x0, y0, x1, y1);
}
}
}
}
}
}
sort(move_list.begin(), move_list.end(), move_list_compare);
for(vector<pair<int, moves> >::iterator it = move_list.begin();it!=move_list.end();it++)
{
int x0 = (it->second).x0;
int y0 = (it->second).y0;
int x1 = (it->second).x1;
int y1 = (it->second).y1;
//Do move
int prev_0 = board_matrix[x0][y0];
int prev_1 = MakeMove(x0, y0, x1, y1);
if(prev_1 <= -9)
{
pieces_lost2[5]++;
pieces_lost2[-prev_1-9]++;
}
else if(prev_1 == 7)
castled1 = true;
else if(prev_1 == 8)
pieces_lost2[1]++;
else
pieces_lost2[-prev_1]++;
int value_evaluated = maximize(depth+1, max_depth, alpha, beta, extra);
min_here = min(value_evaluated, min_here);
beta = min(min_here, beta);
//Undo move
if(prev_1 <= -9)
{
pieces_lost2[5]--;
pieces_lost2[-prev_1 - 9]--;
}
else if(prev_1 == 7)
castled1 = false;
else if(prev_1 == 8)
pieces_lost2[1]--;
else
pieces_lost2[-prev_1]--;
UndoMove(prev_0, prev_1, x0, y0, x1, y1);
if(beta<=alpha)
return min_here;
}
return min_here;
}
}
//Quiescence
int total_pieces_lost = 0;
for(int i=1;i<=6;i++)
total_pieces_lost+= pieces_lost1[i] + pieces_lost2[i];
extra = quies_factor*(float)total_pieces_lost;
if(depth<=max_depth+extra)
{
quies_extra_max = max(extra, quies_extra_max);
return minimize(depth, max_depth, alpha, beta, extra);
}
return EvaluateFunction();
}
//Base function for minimax
//Sets appropriate variables
//and then does the maximizing part
moves EngineAI::minimax_base(int max_depth, int alpha, int beta)
{
for(int i=0;i<7;i++)
{
pieces_lost1[i] = 0;
pieces_lost2[i] = 0;
}
moves result{0,0,0,0};
if (!(IsCheckmate2()) && !(IsStalemate2()))
{
int max_here = -100000000;
//Create move list
vector<pair<int, moves> > move_list;
for(int x0=0;x0<8;x0++)
{
for(int y0=0;y0<8;y0++)
{
if(board_matrix[x0][y0]<0)
{
for(int x1=0;x1<8;x1++)
{
for(int y1=0;y1<8;y1++)
{
if(IsValidMove(x0,y0,x1,y1))
{
//Do move
int prev_0 = board_matrix[x0][y0];
int prev_1 = MakeMove(x0, y0, x1, y1);
move_list.push_back(make_pair(prev_1, moves(x0, y0, x1, y1)));
//Undo move
UndoMove(prev_0, prev_1, x0, y0, x1, y1);
}
}
}
}
}
}
sort(move_list.begin(), move_list.end(), move_list_compare);
for(vector<pair<int, moves> >::iterator it = move_list.begin();it!=move_list.end();it++)
{
int x0 = (it->second).x0;
int y0 = (it->second).y0;
int x1 = (it->second).x1;
int y1 = (it->second).y1;
//Do move
int prev_0 = board_matrix[x0][y0];
int prev_1 = MakeMove(x0, y0, x1, y1);
if(prev_1 >= 9)
{
pieces_lost1[5]++;
pieces_lost1[prev_1-9]++;
}
else if(prev_1 == -7)
castled2 = true;
else if(prev_1 == -8)
pieces_lost1[1]++;
else
pieces_lost1[prev_1]++;
int value_evaluated = minimize(2, max_depth, alpha, beta, 0);
if(value_evaluated > max_here)
{
max_here = value_evaluated;
result.x0 = x0;
result.y0 = y0;
result.x1 = x1;
result.y1 = y1;
}
alpha = max(max_here, alpha);
//Undo move
if(prev_1 >= 9)
{
pieces_lost1[5]--;
pieces_lost1[prev_1 - 9]--;
}
else if(prev_1 == -7)
castled2 = false;
else if(prev_1 == -8)
pieces_lost1[1]--;
else
pieces_lost1[prev_1]--;
UndoMove(prev_0, prev_1, x0, y0, x1, y1);
}
}
return result;
}