-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
237 lines (207 loc) · 6.15 KB
/
engine.cpp
File metadata and controls
237 lines (207 loc) · 6.15 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
#include "engine.h"
#include "board.h"
#include "move_gen.h"
#include <algorithm>
#include <chrono>
#include <climits>
using namespace std;
bool stopSearch = false;
long long nodesSearched = 0;
auto startTime = chrono::high_resolution_clock::now();
int timeLimit = 1000; // default 1s
enum NodeType { EXACT, ALPHA, BETA };
struct TTEntry {
uint64_t hash;
int depth;
int score;
NodeType type;
};
const int TT_SIZE = 1 << 20; // ~1M entries
TTEntry tt[TT_SIZE];
void clearTT() {
for (int i = 0; i < TT_SIZE; i++) {
tt[i].hash = 0;
}
}
void checkTime() {
/*
check every 2048 nodes if we have exceeded the time limit
checking every 2048 nodes is a heuristic to reduce the overhead of checking
the time limit
*/
if (nodesSearched % 2048 == 0) {
auto currentTime = chrono::high_resolution_clock::now();
auto elapsed =
chrono::duration_cast<chrono::milliseconds>(currentTime - startTime)
.count();
if (elapsed > timeLimit) {
stopSearch = true;
}
}
}
int minimax(Board &board, int depth, int alpha, int beta, bool isWhite) {
nodesSearched++;
checkTime();
if (stopSearch)
return 0;
// TT Lookup
uint64_t hash = board.hash;
TTEntry &entry = tt[hash % TT_SIZE];
if (entry.hash == hash && entry.depth >= depth) {
if (entry.type == EXACT)
return entry.score;
if (entry.type == ALPHA && entry.score <= alpha)
return alpha;
if (entry.type == BETA && entry.score >= beta)
return beta;
}
if (depth == 0)
return evaluate(board);
vector<Move> moves = generateAllMoves(board, isWhite);
if (moves.empty()) {
if (isKingInCheck(board, isWhite))
return isWhite ? -1000000 : 1000000;
return 0; // Stalemate
}
int originalAlpha = alpha;
int bestScore = isWhite ? -2000000 : 2000000;
if (isWhite) {
for (auto &it : moves) {
makeMove(board, it, true);
int score = minimax(board, depth - 1, alpha, beta, false);
unMakeMove(board, it, true);
if (stopSearch)
return 0;
alpha = max(alpha, score);
if (beta <= alpha)
break;
}
bestScore = alpha;
} else {
for (auto &it : moves) {
makeMove(board, it, false);
int score = minimax(board, depth - 1, alpha, beta, true);
unMakeMove(board, it, false);
if (stopSearch)
return 0;
beta = min(beta, score);
if (beta <= alpha)
break;
}
bestScore = beta;
}
// Store in Transposition Table
// We store type because if we have gotten a score with pruning then we dont
// exactly know the exact score, it could be anything between alpha and beta,
// so we store the type to indicate that the score is not exact.
entry.hash = hash;
entry.score = bestScore;
entry.depth = depth;
if (bestScore <= originalAlpha)
entry.type = ALPHA;
else if (bestScore >= beta)
entry.type = BETA;
else
entry.type = EXACT;
return bestScore;
}
string iterativeDeepening(Board &board, int timeLimitMs, bool isWhite) {
startTime = chrono::high_resolution_clock::now();
timeLimit = timeLimitMs;
stopSearch = false;
nodesSearched = 0;
Move bestMoveOverall = {0, 0, NONE, NONE};
int totalBestScore = 0;
// Start from depth 1 and increase
for (int depth = 1; depth <= 50; depth++) {
Move bestMoveAtDepth = {0, 0, NONE, NONE};
int currentBestScore = isWhite ? -2000000 : 2000000;
vector<Move> moves = generateAllMoves(board, isWhite);
if (moves.empty())
break;
for (auto &it : moves) {
makeMove(board, it, isWhite);
int score = minimax(board, depth - 1, -2000000, 2000000, !isWhite);
unMakeMove(board, it, isWhite);
if (stopSearch)
break;
if (isWhite) {
if (score > currentBestScore) {
currentBestScore = score;
bestMoveAtDepth = it;
}
} else {
if (score < currentBestScore) {
currentBestScore = score;
bestMoveAtDepth = it;
}
}
}
if (!stopSearch) {
bestMoveOverall = bestMoveAtDepth;
totalBestScore = currentBestScore;
auto currentTime = chrono::high_resolution_clock::now();
auto elapsed =
chrono::duration_cast<chrono::milliseconds>(currentTime - startTime)
.count();
if (elapsed == 0)
elapsed = 1; // avoid div by zero
// Output info to GUI
cout << "info depth " << depth << " score cp " << totalBestScore
<< " nodes " << nodesSearched << " nps "
<< (nodesSearched * 1000 / elapsed) << " time " << elapsed << " pv "
<< moveToUCI(bestMoveOverall) << endl;
} else {
break;
}
// If we already spent more than 50% of our time, don't start a new depth
auto currentTime = chrono::high_resolution_clock::now();
auto elapsed =
chrono::duration_cast<chrono::milliseconds>(currentTime - startTime)
.count();
if (elapsed > timeLimit / 2)
break;
}
// if bestMoveOverall.from == 0, it means we didn't find a move
if (bestMoveOverall.from == 0) {
vector<Move> moves = generateAllMoves(board, isWhite);
if (!moves.empty())
return moveToUCI(moves[0]);
return "none";
}
return moveToUCI(bestMoveOverall);
}
string findBestMove(Board &board, int depth, bool isWhite) {
// Keeping this for compatibility but we will use iterativeDeepening in UCI
stopSearch = false;
timeLimit = 1000000; // effectively no limit
nodesSearched = 0;
Move bestMove = {0, 0, NONE, NONE};
vector<Move> moves = generateAllMoves(board, isWhite);
if (isWhite) {
int bestScore = INT_MIN;
for (auto &it : moves) {
makeMove(board, it, true);
int score = minimax(board, depth - 1, INT_MIN, INT_MAX, false);
unMakeMove(board, it, true);
if (score > bestScore) {
bestScore = score;
bestMove = it;
}
}
} else {
int bestScore = INT_MAX;
for (auto &it : moves) {
makeMove(board, it, false);
int score = minimax(board, depth - 1, INT_MIN, INT_MAX, true);
unMakeMove(board, it, false);
if (score < bestScore) {
bestScore = score;
bestMove = it;
}
}
}
if (bestMove.from == 0)
return "none";
return moveToUCI(bestMove);
}