-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.c
More file actions
223 lines (190 loc) · 6.08 KB
/
search.c
File metadata and controls
223 lines (190 loc) · 6.08 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
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include "main.h"
#include "moveHandler.h"
#include "search.h"
#include "eval.h"
#include "sorting.h"
#include "hashing.h"
#include "utils.h"
#define INF 9223372036854775807
int64_t m_alphabeta(Board *p_board, uint8_t depth, evaluation_t alpha, evaluation_t beta, uint8_t maximizer);
int64_t m_alphaBetaCaptures(Board *p_board, evaluation_t alpha, evaluation_t beta);
uint8_t m_numBoardRepeates(Board *p_board);
Hashmap *p_tt = NULL;
uint32_t leafNodesEvaluated = 0;
uint32_t transpositionHits = 0;
uint8_t color = 0; //Color of the player to find the best move for, this is set in findbestmove and passed to evaluation
Move findBestMove(Board *p_board, uint8_t depth)
{
color = p_board->turn;
leafNodesEvaluated = 0;
transpositionHits = 0;
if(!p_tt) p_tt = createHashmap(1 << 20);
ArrayList *p_legalMoves = getLegalMoves(p_board);
sort(p_legalMoves, p_board);
Move bestMove;
int64_t alpha = -INF;
int64_t beta = INF;
int64_t value = -INF;
for (uint8_t i = 0; i < p_legalMoves->elements; i++)
{
performMove(&p_legalMoves->array[i], p_board);
int64_t tmpValue = m_alphabeta(p_board, depth - 1, alpha, beta, 0);
undoMove(&p_legalMoves->array[i], p_board);
// select max and set new best move
if (tmpValue > value)
{
value = tmpValue;
bestMove = p_legalMoves->array[i];
}
alpha = fmaxl(alpha, value);
if (alpha >= beta)
{
uint8_t repeats = m_numBoardRepeates(p_board);
appendToHashmap(p_tt, p_board, beta, depth, repeats, LOWER_BOUND);
break;
}
}
freeMoveList(p_legalMoves);
//freehashmap(p_tt);
printf("Evaluation: %ld\n", value);
printf("Leafnodes evaluated: %d\n", leafNodesEvaluated);
printf("Transpositions hit: %d\n", transpositionHits);
printf("Table Size: %ld\n", p_tt->size);
printf("Phase: %d (0 = opening)\n", getPhase(p_board));
printf("Best move: ");
printMove(&bestMove);
return bestMove;
}
int64_t m_alphabeta(Board *p_board, uint8_t depth, int64_t alpha, int64_t beta, uint8_t maximizer)
{
uint8_t repeats = m_numBoardRepeates(p_board);
if(repeats == 2)
{
return STALEMATE;
}
int64_t transpositionLookup = getEvaluation(p_tt, p_board, depth, repeats, alpha, beta);
if (transpositionLookup != EVAL_LOOKUP_FAILED)
{
transpositionHits++;
return transpositionLookup;
}
// 50 move rule makes a draw
if (p_board->halfMoves == 50)
{
return 0;
}
ArrayList *p_legalMoves = getLegalMoves(p_board);
// If the depth is reach or there are no legal moves (checkmate or stalemate)
if (p_legalMoves->elements == 0)
{
leafNodesEvaluated++;
freeMoveList(p_legalMoves);
evaluation_t terminationEval = evaluateBoard(p_board, NO_LEGAL_MOVES, color);
if(terminationEval == STALEMATE)
{
return terminationEval;
}
// This makes a checkmate at a shorter depth more attractive
// For the opponent a shorter checkmate is also less attractive
return terminationEval * (1 + depth);
}
if (depth == 0)
{
leafNodesEvaluated++;
freeMoveList(p_legalMoves);
return evaluateBoard(p_board, LEGAL_MOVES_EXIST, color); /*-m_alphaBetaCaptures(p_board, -beta, -alpha);*/
}
sort(p_legalMoves, p_board);
uint8_t wasCut = 0;
int64_t value;
if (maximizer)
{
value = -INF;
for (uint8_t i = 0; i < p_legalMoves->elements; i++)
{
performMove(&p_legalMoves->array[i], p_board);
value = fmaxl(value, m_alphabeta(p_board, depth - 1, alpha, beta, 0));
undoMove(&p_legalMoves->array[i], p_board);
alpha = fmaxl(alpha, value);
if (alpha >= beta)
{
appendToHashmap(p_tt, p_board, beta, depth, repeats, LOWER_BOUND);
wasCut = 1;
break;
}
}
}
else
{
value = INF;
for (uint8_t i = 0; i < p_legalMoves->elements; i++)
{
performMove(&p_legalMoves->array[i], p_board);
value = fminl(value, m_alphabeta(p_board, depth - 1, alpha, beta, 1));
undoMove(&p_legalMoves->array[i], p_board);
beta = fminl(beta, value);
if (beta <= alpha)
{
appendToHashmap(p_tt, p_board, alpha, depth, repeats, UPPER_BOUND);
wasCut = 1;
break;
}
}
}
if (!wasCut)
{
appendToHashmap(p_tt, p_board, value, depth, repeats, EXACT);
}
freeMoveList(p_legalMoves);
return value;
}
int64_t m_alphaBetaCaptures(Board *p_board, int64_t alpha, int64_t beta)
{
// Get the evaluation of the board, to compair the position
// without captures to the positions to with captures
ArrayList *p_legalMoves = getLegalMoves(p_board);
int64_t eval = evaluateBoard(p_board, p_legalMoves->elements ? LEGAL_MOVES_EXIST : NO_LEGAL_MOVES, color);
if (eval >= beta)
{
freeMoveList(p_legalMoves);
return beta;
}
if (eval > alpha)
{
alpha = eval;
}
filterNonCaptureMoves(p_legalMoves);
sort(p_legalMoves, p_board);
for(uint16_t i = 0; i < p_legalMoves->elements; i++)
{
performMove(&p_legalMoves->array[i], p_board);
eval = m_alphaBetaCaptures(p_board, -beta, -alpha);
undoMove(&p_legalMoves->array[i], p_board);
if (eval >= beta)
{
freeMoveList(p_legalMoves);
return beta;
}
if (eval > alpha)
{
alpha = eval;
}
}
freeMoveList(p_legalMoves);
return alpha;
}
uint8_t m_numBoardRepeates(Board *p_board)
{
uint16_t tailIndex = (p_board->fullMoves - 1) * 2 + (p_board->turn == BLACK);
uint8_t repeats = 0;
for (uint16_t i = 0; i < tailIndex; i++)
{
repeats += p_board->gameHashHistory[i] == p_board->hash;
}
return repeats;
}