-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSearch.cpp
317 lines (274 loc) · 6.71 KB
/
Search.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
#include <windows.h>
#include <iostream>
using namespace std;
#include <stdio.h>
#include <string>
#include "Checkers.h"
#include "EdAccess.h"
#define CHECK_INTERVAL 50000
bool StopRequest;
bool AnalyseMode;
double PerftNodes;
double CheckNodes;
int EdRoot[3];
void Perft(unsigned depth)
{
Move *old_MP = MP;
GenerateAllMoves();
--depth;
for (Move *m = old_MP; m < MP; ++m)
{
MakeMove(m, 0);
if (depth) Perft(depth);
else ++PerftNodes;
UnmakeMove(m);
}
MP = old_MP;
}
char **pv_str = 0;
// m - òåêóùèé õîä
// cm - íîìåð òåêóùåãî õîäà
// tm - âñåãî õîäîâ
void PrintPV(int depth, int score, unsigned cm, unsigned tm, Move *m)
{
int time = GetTimeElaps();
if (pfSearchInfoEx)
{
char str[64];
// èíèöèàëèçàöèÿ ìàññèâà äëÿ õðàíåíèÿ ëó÷øåãî âàðèàíòà
if (!pv_str)
{
pv_str = new char *[2];
pv_str[0] = new char[1024];
pv_str[1] = new char[1];
pv_str[1][0] = 0;
}
// ëó÷øèé âàðèàíò
pv_str[0][0] = 0;
for (unsigned i = 0; PV[0][i].from && i < 6; i++)
{
MoveToStr(&PV[0][i], str);
if (pv_str[0][0]) strcat(pv_str[0], " ");
strcat(pv_str[0], str);
if (strlen(pv_str[0]) > 900) break;
}
// òåêóùèé õîä
char cm_str[32];
if (time > 1000)
{
itoa(cm + 1, cm_str, 10);
strcat(cm_str, "/");
itoa(tm, cm_str + strlen(cm_str), 10);
strcat(cm_str, " ");
MoveToStr(m, cm_str + strlen(cm_str));
}
else cm_str[0] = 0;
// ñêîðîñòü
int speed = 0;
if (time > 1000) speed = int (Nodes / time);
char speed_str[16];
itoa(speed, speed_str, 10);
char score_str[16];
if (abs(score) > ED_WIN - 1000)
{
if (score > 0) score_str[0] = '+';
else score_str[0] = '-';
if (abs(score) > WIN - 1000)
{
score_str[1] = 'X';
itoa((WIN - abs(score) + 1) / 2, score_str + 2, 10);
}
else
{
score_str[1] = 'D';
itoa((ED_WIN - abs(score) + 1) / 2, score_str + 2, 10);
}
}
else itoa(score, score_str, 10);
char depth_str[16];
itoa(depth, depth_str, 10);
pfSearchInfoEx(score_str, depth_str, speed_str, pv_str, cm_str);
return;
}
else if (pfSearchInfo)
{
char str[64];
// ëó÷øèé âàðèàíò
std::string pv_str;
for (unsigned i = 0; PV[0][i].from; i++)
{
MoveToStr(&PV[0][i], str);
if (pv_str != "") pv_str += " ";
pv_str += str;
}
// òåêóùèé õîä
std::string cm_str;
if (time > 1000)
{
cm_str = itoa(cm + 1, str, 10);
cm_str += "/";
cm_str += itoa(tm, str, 10);
cm_str += " ";
MoveToStr(m, str);
cm_str += str;
}
// ñêîðîñòü
int speed = 0;
if (time > 1000) speed = int (Nodes / time);
pfSearchInfo(score, depth, speed, (char *)pv_str.c_str(), (char *)cm_str.c_str());
return;
}
// âûâîä â êîíñîëüíîì ðåæèìå
cout.width(2);
cout << depth << ' ';
cout.width(3);
cout << score << ' ';
cout.width(6);
cout << time << ' ';
cout.width(10);
cout.setf(ios::fixed);
cout << Nodes << ' ';
char move[128];
for (int i = 0; PV[0][i].from; ++i)
{
MoveToStr(&PV[0][i], move);
if (i) cout << ' ';
cout << move;
}
cout << endl;
cout.flush();
}
// ìèíèìàêñíûé ïîèñê ïî äåðåâó õîäîâ
int TreeSearch(int depth, unsigned ply)
{
// ïîêà íåò ëó÷øåãî âàðèàíòà
PV[ply][ply].from = 0;
if (ply >= MAX_DEPTH) return Eval();
// íóæíî ëè ñðî÷íî çàâåðøàòü ïîèñê?
if (Nodes >= CheckNodes)
{
CheckNodes = Nodes + CHECK_INTERVAL;
if (!AnalyseMode && CheckTime())
{
StopRequest = true;
return 0;
}
}
// äîñòóï ê ýíäøïèëüíîé áàçå
if (!EdNocaptures && Pieces <= EdPieces)
{
int res = EdProbe();
if (res != EdAccess::not_found)
{
if (res != EdRoot[stm] || !Reversible[ply - 1])
{
if (res == EdAccess::win) return ED_WIN - ply;
if (res == EdAccess::lose) return -ED_WIN + ply;
if (res == EdAccess::draw) return 0;
MessageBox(0, "unknown value from EdAccess", "Error", 0);
}
}
}
// ãåíåðàöèÿ õîäîâ
Move *old_MP = MP;
if (depth > 0) GenerateAllMoves();
else
{
// íå îñòàíàâëèâàåìñÿ çà ãîðèçîíòîì, åñëè åñòü âçÿòèÿ
GenerateCaptures();
if (MP == old_MP) return Eval();
}
// äîñòóï ê ýíäøïèëüíîé áàçå, ñæàòîé ñ ïîòåðåé ïîçèöèé ñî âçÿòèÿìè
if (EdNocaptures && Pieces <= EdPieces && !old_MP->cap_sq[0])
{
int res = EdProbe();
if (res != EdAccess::not_found)
{
if (res != EdRoot[stm] || !Reversible[ply - 1])
{
if (res == EdAccess::win) return ED_WIN - ply;
if (res == EdAccess::lose) return -ED_WIN + ply;
if (res == EdAccess::draw) return 0;
MessageBox(0, "unknown value from EdAccess", "Error", 0);
}
}
}
// ðåêóðñèâíûé ïîèñê
int best_score = -WIN + ply;
for (Move *m = old_MP; m < MP; ++m)
{
MakeMove(m, ply);
int score = -TreeSearch(depth - 1, ply + 1);
UnmakeMove(m);
if (StopRequest) break;
if (score > best_score)
{
best_score = score;
// îáíîâëÿåì ëó÷øèé âàðèàíò
PV[ply][ply] = *m;
int i;
for (i = ply + 1; PV[ply + 1][i].from; i++)
PV[ply][i] = PV[ply + 1][i];
PV[ply][i].from = 0;
}
}
MP = old_MP;
return best_score;
}
// ïîèñê â êîðíå äåðåâà õîäîâ
Move RootSearch()
{
StartTimer();
StopRequest = false;
Nodes = 0;
CheckNodes = CHECK_INTERVAL;
// ãåíåðàöèÿ õîäîâ
MP = MoveBuffer;
GenerateAllMoves();
if (MP == MoveBuffer) return *MP;
if (!AnalyseMode && MP == MoveBuffer + 1)
{
// åäèíñòâåííûé õîä âîçâðàùàåì ñðàçó
MP = MoveBuffer;
return *MP;
}
if (!EdNocaptures || !MoveBuffer[0].cap_sq[0]) EdRoot[stm] = EdProbe();
else EdRoot[stm] = EdAccess::not_found;
if (EdRoot[stm] == EdAccess::win) EdRoot[stm ^ CHANGE_COLOR] = EdAccess::lose;
else if (EdRoot[stm] == EdAccess::lose) EdRoot[stm ^ CHANGE_COLOR] = EdAccess::win;
else EdRoot[stm ^ CHANGE_COLOR] = EdRoot[stm];
int last_score = 0;
// èòåðàòèâíîå óãëóáëåíèå
for (int depth = 1; depth < MAX_DEPTH; depth++)
{
int best_score = -WIN;
// äëÿ êàæäîãî õîäà âûçûâàåì ïîèñê
for (Move *m = MoveBuffer; m < MP; m++)
{
PrintPV(depth, last_score, m - MoveBuffer, MP - MoveBuffer, m);
MakeMove(m, 0);
int score = -TreeSearch(depth - 1, 1);
UnmakeMove(m);
if (StopRequest) break;
if (score > best_score)
{
// íàøëè ëó÷øèé õîä
best_score = score;
// îáíîâëÿåì ëó÷øèé âàðèàíò
PV[0][0] = *m;
unsigned i;
for (i = 1; PV[1][i].from; i++)
PV[0][i] = PV[1][i];
PV[0][i].from = 0;
// ïåðåìåùàåì õîä â íà÷àëî ñïèñêà õîäîâ
Move best = *m;
for (Move *pm = m; pm > MoveBuffer; pm--) *pm = *(pm - 1);
*MoveBuffer = best;
}
PrintPV(depth, last_score = best_score, m - MoveBuffer, MP - MoveBuffer, m);
}
if (StopRequest) break;
}
MP = MoveBuffer;
return PV[0][0];
}