-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.cpp
executable file
·325 lines (257 loc) · 9.55 KB
/
Engine.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
318
319
320
321
322
323
324
325
#include "Engine.h"
int Engine::evaluatedNodes;
int Engine::totalFound;
int Engine::totalCollisions;
int Engine::difficultyToDepth[] = { 3, 4, 4, 4 };
void Engine::init(int depth, int hashTableSize) {
if(hashTableSize <= 0 || hashTableSize > 200000000) {
printf("The hash table size is not correct!\n");
throw new exception();
}
if((hashTableSize & (hashTableSize -1)) > 0) {
printf("The hash table size is not correct (not a power of two)!\n");
throw new exception();
}
this->depth = depth;
this->hashTableSize = hashTableSize;
this->hashBitMask = hashTableSize - 1;
hashTable = new HashTableEntry*[hashTableSize];
}
Engine::Engine(difficulty_t difficulty, int hashTableSize) {
this->difficulty = difficulty;
init(difficultyToDepth[difficulty], hashTableSize);
}
Engine::~Engine() {
// delete the entries from the hash table
for(int i = 0; i < hashTableSize; i++)
if(hashTable[i] != NULL)
delete hashTable[i];
delete hashTable;
}
void Engine::loadOpeningBook(string filename) {
ifstream input(filename.c_str());
if(!input) {
printf("Opening book file (\"%s\") can't be opened!\n", filename.c_str());
throw new exception();
}
string aux, line, FEN, moveStr;
while(input) {
getline(input, line);
stringstream ss(line);
if(line.size() < 20) // blank line or short comment
continue;
FEN = "";
for(int part = 0; part < 6; part++) {
ss >> aux;
FEN += aux;
if(aux == "//")
break;
if(part < 5)
FEN += " ";
}
if(FEN == "//") // this line is a comment
continue;
moveStr = "";
ss >> moveStr;
ss >> aux;
moveStr = moveStr + " " + aux;
aux = "";
ss >> aux;
if(aux.size() > 0)
moveStr = moveStr + " " + aux;
if(openingBook.count(FEN) == 0) {
vector<Move> v;
v.push_back(Move(moveStr));
openingBook[FEN] = v;
}
else
openingBook[FEN].push_back(Move(moveStr));
}
}
pair<Move, pair<double, int> > Engine::getBestMove(Position *pos) {
string FEN = pos->toFEN();
//printf("%s\n", FEN.c_str());
if(openingBook.count(FEN) > 0) {
srand(time(NULL));
unsigned int randomMove = rand() % openingBook[FEN].size();
// printf("%d %d\n", openingBook[FEN].size(), randomMove);
return make_pair(openingBook[FEN][randomMove], make_pair(BOOK_MOVE, 0));
}
int runOnDepth = this->depth;
if(difficulty >= HARD) {
if(pos->getNumberOfPieces() <= 20) {
runOnDepth = 5;
if(pos->getNumberOfPieces() <= 14) {
runOnDepth = 6;
if(pos->getNumberOfPieces() <= 10) {
runOnDepth = 7;
if(pos->getNumberOfPieces() <= 8) {
runOnDepth = 8;
}
}
}
}
// if(pos->getNumberOfPieces() <= 14) {
// runOnDepth = 6;
// if(pos->getNumberOfPieces() <= 8) {
// runOnDepth = 8;
// }
// }
}
if(difficulty == FORCE)
runOnDepth++;
pair<Move, double> res = miniMax(pos, -INF * 2, +INF * 2, runOnDepth);
pair<Move, pair<double, int> > ret = make_pair(res.first, make_pair(res.second, runOnDepth));
return ret;
}
#define __HASH_TABLE
pair<Move, double> Engine::miniMax(Position *p, double alpha, double beta, int depthLeft) {
pair<Move, double> best = make_pair(Move(), -INF);
pair<Move, double> crt;
vector<Move*> availableMoves;
/*evaluatedNodes++;
if(evaluatedNodes % 1000000 == 0)
printf("%d\n", evaluatedNodes);
*/
if(p->isDrawByRules())
return make_pair(Move(), 0.0);
// find valid moves
p->getAllAvailableMoves(availableMoves);
if(availableMoves.size() == 0) { // the game is over
if(p->givingCheck.size() > 0) { // it's checkmate
best.second -= depthLeft; // (not perfect, but better)
return best;
}
else // it's stalemate
return make_pair(Move(), 0.0);
}
// cut the search
if(depthLeft == 0) {
Position::deallocateMoveList(availableMoves);
return make_pair(Move(), quiescence(p, alpha, beta, 1));
}
#ifdef __HASH_TABLE
int hashTablePos = p->hashValue & this->hashBitMask;
bool foundInTable = false;
if(hashTable[hashTablePos] != NULL && hashTable[hashTablePos]->pos.equals(*p) && hashTable[hashTablePos]->depth >= depthLeft) {
// printf("Found %d!\n", hashTable[hashTablePos]->scoreType);
totalFound++;
foundInTable = true;
hashTable[hashTablePos]->used = true;
if(hashTable[hashTablePos]->scoreType == EXACT) {
Position::deallocateMoveList(availableMoves);
return make_pair(hashTable[hashTablePos]->move, hashTable[hashTablePos]->score);
}
else // LOWER_BOUND or not sufficient depth
alpha = max(alpha, hashTable[hashTablePos]->score);
vector<Move*> :: iterator m = availableMoves.begin();
for(; m != availableMoves.end(); m++)
if(hashTable[hashTablePos]->move == **m)
break;
if(m == availableMoves.end()) {
printf("Move not found, something wrong with the hash table!\n");
printf("move: %s\n", hashTable[hashTablePos]->move.toString().c_str());
p->debug_printBoard();
throw new exception();
}
// highest priority in the move order
(*m)->hashFlag = true;
}
#endif
// auxiliary object on which we perform the moves
Position next(*p);
next.sortMoves(availableMoves);
score_t scoreType = EXACT;
for(vector<Move*> :: iterator it = availableMoves.begin(); it != availableMoves.end(); it++) {
next.performMove(*it);
//fprintf(stderr, "%s\n", (*it)->toString().c_str());
crt = miniMax(&next, -beta, -alpha, depthLeft - 1);
crt.second = -crt.second;
next.revertLastMove();
alpha = max(alpha, crt.second);
if(crt.second > best.second || best.first.fromRow == -1) {
best.first = Move(**it);
best.second = crt.second;
}
if(best.second >= beta) {
scoreType = LOWER_BOUND;
break;
}
}
Position::deallocateMoveList(availableMoves);
#ifdef __HASH_TABLE
if(foundInTable) {
// we try to update
if(!hashTable[hashTablePos]->pos.equals(*p))
throw new exception();
hashTable[hashTablePos]->scoreType = scoreType;
hashTable[hashTablePos]->score = best.second;
hashTable[hashTablePos]->depth = depthLeft;
}
else {
// try to put it in the table
if((hashTable[hashTablePos] == NULL) || (!hashTable[hashTablePos]->used)) {
if(hashTable[hashTablePos] != NULL) {
if(hashTable[hashTablePos]->pos.equals(*p) && depthLeft < hashTable[hashTablePos]->depth) {
printf("p: %d\n", depthLeft);
printf("HT: %d\n", hashTable[hashTablePos]->depth);
throw new exception();
}
// printf("Collision!\n");
// p->debug_printBoard();
// hashTable[hashTablePos]->pos.debug_printBoard();
// printf("%d\n", hashTable[hashTablePos]->pos.equals(*p));
totalCollisions++;
delete hashTable[hashTablePos];
}
hashTable[hashTablePos] = new HashTableEntry(p, scoreType, best.second, best.first, depthLeft);
}
}
#endif
return best;
}
double Engine::quiescence(Position *p, double alpha, double beta, int depth) {
double crt;
vector<Move*> captureMoves;
if(p->isDrawByRules())
return 0.0;
double standPad = p->getEstimatedScore(this->difficulty);
if(standPad >= beta)
return beta;
alpha = max(alpha, standPad);
// find only captures
p->getQCaptureMoves(captureMoves);
if(captureMoves.size() == 0) { // the position is quiet
if(p->givingCheck.size() > 0) {
vector<Move*> availableMoves;
// could be optimized
p->getAllAvailableMoves(availableMoves);
if(availableMoves.size() == 0) // it's checkmate
return -INF - (100 - depth); // (not perfect, but better)
else
return standPad;
}
else
return standPad;
}
/*evaluatedNodes++;
if(evaluatedNodes % 1000000 == 0)
printf("%d\n", evaluatedNodes);
*/
// auxiliary object
Position next(*p);
next.sortMoves(captureMoves);
// printf("Q: %d\n", depth);
for(vector<Move*> :: iterator it = captureMoves.begin(); it != captureMoves.end(); it++) {
next.performMove(*it);
//printf("%s // %d\n", (*it)->toString().c_str(), depth);
crt = quiescence(&next, -beta, -alpha, depth + 1);
crt = -crt;
next.revertLastMove();
alpha = max(alpha, crt);
if(crt >= beta)
break;
}
Position::deallocateMoveList(captureMoves);
return alpha;
}