-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
308 lines (229 loc) · 7.18 KB
/
search.c
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
#include "ark.h"
#define INFINITE 30000
#define MATE 29000
// Check if time up, or interrupt from GUI
static void CheckUp(S_SEARCHINFO* info) {
if (info->timeset == TRUE && getMillis() > info->stoptime) {
info->stopped = TRUE;
}
ReadInput(info);
}
static void PickBestMove(int moveNum, S_MOVELIST* list) {
S_MOVE temp;
int bestScore = 0;
int bestNum = 0;
for (int i = moveNum; i < list->count; i++) {
if (list->moves[i].score > bestScore) {
bestScore = list->moves[i].score;
bestNum = i;
}
}
temp = list->moves[bestNum];
list->moves[bestNum] = list->moves[moveNum];
list->moves[moveNum] = temp;
}
static int IsRepetition(const S_BOARD *board){
for(int his = board->hisply - board->fiftyMoves; his < board->hisply - 1; his++){
ASSERT(his >= 0 && his < MAX_GAME_MOVES);
if(board->history[his].posKey == board->posKey){
return TRUE;
}
}
return FALSE;
}
// Clear things for a new search
static void ClearForSearch(S_BOARD *board, S_SEARCHINFO *info){
for(int i = 0; i < 13 ; i++){
for(int j = 0; j < BRD_SQ_NUM; j++){
board->searchHistory[i][j] = 0;
}
}
for(int i = 0; i < 2 ; i++){
for(int j = 0; j < MAX_DEPTH; j++){
board->searchKillers[i][j] = 0;
}
}
ClearTable(board->pvTable);
board->ply = 0;
info->stopped = FALSE;
info->nodes = 0;
info->fhf = 0;
info->fh = 0;
}
// Search for Quiet Moves
static int Quiescence(int alpha, int beta, S_BOARD *board,S_SEARCHINFO *info){
ASSERT(CheckBoard(board));
if (info->nodes % 2047 == 0) {
CheckUp(info);
}
info->nodes++;
if ((IsRepetition(board) || board->fiftyMoves >= 100) && board->ply) {
return 0;
}
// Thats the maximum u can go
if (board->ply > MAX_DEPTH - 1) {
return EvalPosition(board);
}
int score = EvalPosition(board);
if (score >= beta) {
return beta;
}
if (score > alpha) {
alpha = score;
}
S_MOVELIST list[1];
GenerateAllCaptures(board, list);
int moveNum = 0;
int legal = 0;
int oldAlpha = alpha;
int bestMove = NOMOVE;
score = -INFINITE;
int PvMove = ProbePvTable(board);
if (PvMove != NOMOVE) {
for (int i = 0; i < list->count; ++i) {
if (list->moves[i].move == PvMove) {
list->moves[i].score = 2000000;
break;
}
}
}
for (moveNum = 0; moveNum < list->count; moveNum++) {
PickBestMove(moveNum, list);
if (!MakeMove(board, list->moves[moveNum].move)) {
continue;
}
legal++;
score = -Quiescence(-beta, -alpha, board, info);
TakeMove(board);
if (info->stopped == TRUE) {
return 0;
}
if (score > alpha) {
if (score >= beta) {
// Beta cutoff
if (legal == 1) {
info->fhf++;
}
info->fh++;
return beta;
}
alpha = score;
bestMove = list->moves[moveNum].move;
}
}
if (alpha != oldAlpha) {
StorePvTable(board, bestMove);
}
return alpha;
}
static int AlphaBeta(int alpha, int beta, int depth, S_BOARD *board,S_SEARCHINFO *info, int DoNull){
ASSERT(CheckBoard(board));
if(depth == 0){
return Quiescence(alpha, beta, board, info);
}
if (info->nodes % 2047 == 0) {
CheckUp(info);
}
info->nodes++;
if ((IsRepetition(board) || board->fiftyMoves >= 100) && board->ply) {
return 0;
}
// Thats the maximum u can go
if(board->ply > MAX_DEPTH - 1){
return EvalPosition(board);
}
S_MOVELIST list[1];
GenerateAllMoves(board, list);
int moveNum = 0;
int legal = 0;
int oldAlpha = alpha;
int bestMove = NOMOVE;
int score = -INFINITE;
int pvMove = ProbePvTable(board);
if (pvMove != NOMOVE) {
for (moveNum = 0; moveNum < list->count; moveNum++) {
if (list->moves[moveNum].move == pvMove) {
list->moves[moveNum].score = 2000000;
break;
}
}
}
for(moveNum = 0; moveNum < list->count; moveNum++){
PickBestMove(moveNum, list);
if(!MakeMove(board, list->moves[moveNum].move)){
continue;
}
legal++;
score = -AlphaBeta(-beta, -alpha, depth - 1, board, info, TRUE);
TakeMove(board);
if (info->stopped == TRUE) {
return 0;
}
if(score > alpha){
if(score >= beta){
// Beta cutoff
if(legal == 1){
info->fhf++;
}
if (!(list->moves[moveNum].move & MFLAGCAP)) {
board->searchKillers[1][board->ply] = board->searchKillers[0][board->ply];
board->searchKillers[0][board->ply] = list->moves[moveNum].move;
}
info->fh++;
return beta;
}
alpha = score;
bestMove = list->moves[moveNum].move;
if (!(list->moves[moveNum].move & MFLAGCAP)) {
board->searchHistory[board->pieces[FROSQ(bestMove)]][TOSQ(bestMove)] += depth;
}
}
}
if(legal == 0){
if(isSqAttacked(board->kingSq[board->side], board->side ^ 1, board)){
// To Find the shortest path to mate
return -MATE + board->ply;
}else{
return 0;
}
}
if(alpha != oldAlpha){
StorePvTable(board, bestMove);
}
return alpha;
}
void SearchPosition(S_BOARD *board, S_SEARCHINFO *info){
// Iterative Deepening, search init
// for depth equals 1 to MAX_DEPTH
// search with alpha beta
// next depth
// Until time runs out
// U can search for a particular depth, more efficient by Move Ordering and PV Table
int bestMove = NOMOVE; // From PV TABLE
int bestScore = -INFINITE; // From the current iteration
int currentDepth = 0;
int pvNum = 0;
int pvMoves = 0;
ClearForSearch(board, info);
for(currentDepth = 1; currentDepth <= info->depth; currentDepth++){
bestScore = AlphaBeta(-INFINITE, INFINITE, currentDepth, board, info, TRUE);
// out of time?
if (info->stopped == TRUE) {
break;
}
pvMoves = GetPvLine(board, currentDepth);
bestMove = board->pvArray[0];
//printf("Depth:%d score:%d move:%s nodes:%ld ",
// currentDepth, bestScore, PrMove(bestMove), info->nodes);
printf("info score cp %d depth %d nodes %ld time %lld ", bestScore, currentDepth, info->nodes, getMillis() - info->starttime);
pvMoves = GetPvLine(board, currentDepth);
printf("pv");
for (pvNum = 0; pvNum < pvMoves; ++pvNum) {
printf(" %s", PrMove(board->pvArray[pvNum]));
}
printf("\n");
//printf("Ordering:%.2f\n", (info->fhf / info->fh));
}
// info score cp 13 depth 1 nodes 13 time 15 pv f1b5
printf("bestmove %s\n", PrMove(bestMove));
}