-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.h
283 lines (233 loc) · 6.79 KB
/
Engine.h
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
/* GenChess, a genesis chess engine
* Copyright (C) 2010-2014, Justin Madru (justin.jdm64@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ENGINE_H__
#define __ENGINE_H__
#include <climits>
#include <vector>
#include "Array.h"
#include "Util.h"
#include "Board.h"
#include "TransTable.h"
#define MAX_DEPTH 10
#define MIN_SCORE -(INT_MAX - 4)
#define MAX_SCORE (INT_MAX - 4)
#define CHECKMATE_SCORE -(INT_MAX - 4)
#define STALEMATE_SCORE 0
template<class MoveType>
class Engine
{
private:
MoveList<MoveType> *curr;
Board<MoveType> *board;
TransTable<MoveType> *tt;
Array<MoveType> captureKiller;
Array<MoveType> moveKiller;
Array<MoveType> placeKiller;
Array<MoveType> pvMove;
int maxNg;
Array<bool> tactical;
Array<bool> ismate;
void pickRandomMove()
{
const int score = curr->at(0).score;
int end = 0;
for (auto item : *curr) {
if (item.score != score)
break;
end++;
}
pvMove[0] = curr->at(rand() % end).move;
}
int Quiescence(int alpha, int beta, int depth)
{
auto ptr = board->getMoveList(board->getStm(), tactical[depth]? MoveClass::ALL : MoveClass::CAPTURE);
if (ptr->empty()) {
board->recycle(ptr);
return tactical[depth]? CHECKMATE_SCORE + board->getPly() : -board->eval();
}
int best = MIN_SCORE, score = -board->eval();
if (score >= beta) {
board->recycle(ptr);
return score;
}
alpha = max(alpha, score);
const MoveFlags undoFlags = board->getMoveFlags();
ptr->sort();
for (auto& item : *ptr) {
// set check for opponent
tactical[depth + 1] = item.check;
board->make(item.move);
score = -Quiescence(-beta, -alpha, depth + 1);
board->unmake(item.move, undoFlags);
if (score >= beta) {
board->recycle(ptr);
return score;
}
best = max(best, score);
alpha = max(alpha, score);
}
board->recycle(ptr);
return best;
}
bool NegaMoveType(int &alpha, const int beta, int &best, const int depth, const int limit, Array<MoveType> &killer, const MoveClass type)
{
const MoveFlags undoFlags = board->getMoveFlags();
MoveType move;
best = MIN_SCORE;
// Try Killer Move
if (board->validMove(killer[depth], move)) {
ismate[depth] = false;
board->make(move);
// set check for opponent
tactical[depth + 1] = board->incheck(board->getStm());
best = -NegaScout(-beta, -alpha, depth + 1, limit);
board->unmake(move, undoFlags);
if (best >= beta) {
tt->setItem(board->hash(), best, move, limit - depth, CUT_NODE);
return true;
} else if (best > alpha) {
alpha = best;
pvMove[depth] = move;
}
}
// Try all of moveType Moves
auto ptr = board->getMoveList(board->getStm(), type);
if (ptr->empty()) {
board->recycle(ptr);
return false;
}
ptr->sort();
ismate[depth] = false;
int b = alpha + 1;
for (auto& item : *ptr) {
board->make(item.move);
// set check for opponent
tactical[depth + 1] = item.check;
item.score = -NegaScout(-b, -alpha, depth + 1, limit);
if (item.score > alpha && item.score < beta)
item.score = -NegaScout(-beta, -alpha, depth + 1, limit);
board->unmake(item.move, undoFlags);
best = max(best, item.score);
if (best >= beta) {
killer[depth] = item.move;
tt->setItem(board->hash(), best, killer[depth], limit - depth, CUT_NODE);
board->recycle(ptr);
return true;
} else if (best > alpha) {
alpha = best;
pvMove[depth] = item.move;
}
b = alpha + 1;
}
board->recycle(ptr);
return false;
}
int NegaScout_hashMiss(int alpha, const int beta, const int depth, int limit);
int NegaScout(int alpha, const int beta, const int depth, int limit)
{
if (depth >= limit) {
if (!tactical[depth])
return Quiescence(alpha, beta, depth);
else
limit++;
}
TransItem<MoveType> *tt_item;
ismate[depth] = true;
pvMove[depth].setNull();
// Try Transposition Table
if (tt->getItem(board->hash(), tt_item)) {
// Try score
int ttScore;
if (tt_item->getScore(alpha, beta, limit - depth, ttScore))
return ttScore;
// Try Move
MoveType move;
if (tt_item->getMove(move)) {
if (!board->validMove(move, move))
goto hashMiss;
const MoveFlags undoFlags = board->getMoveFlags();
ismate[depth] = false;
board->make(move);
// set check for opponent
tactical[depth + 1] = board->incheck(board->getStm());
int best = -NegaScout(-beta, -alpha, depth + 1, limit);
board->unmake(move, undoFlags);
if (best >= beta) {
tt->setItem(board->hash(), best, move, limit - depth, CUT_NODE);
return best;
} else if (best > alpha) {
alpha = best;
pvMove[depth] = move;
}
}
}
hashMiss:
return NegaScout_hashMiss(alpha, beta, depth, limit);
}
void search(int alpha, const int beta, const int depth, const int limit)
{
const MoveFlags undoFlags = board->getMoveFlags();
curr = curr? curr : board->getMoveList(board->getStm(), MoveClass::ALL);
int b = beta;
for (auto& item : *curr) {
tactical[depth + 1] = item.check;
board->make(item.move);
item.score = -NegaScout(-b, -alpha, depth + 1, limit);
if (item.score > alpha && item.score < beta)
item.score = -NegaScout(-beta, -alpha, depth + 1, limit);
board->unmake(item.move, undoFlags);
if (item.score > alpha) {
alpha = item.score;
pvMove[depth] = item.move;
tt->setItem(board->hash(), alpha, pvMove[depth], limit - depth, PV_NODE);
}
b = alpha + 1;
}
curr->sort();
}
public:
Engine(Board<MoveType> *_Board, TransTable<MoveType> *TT) : curr(nullptr), board(_Board), tt(TT)
{
maxNg = 5;
}
~Engine()
{
}
MoveType think()
{
srand(time(nullptr));
curr = nullptr;
auto start = Timer::now();
for (int depth = 1; depth <= maxNg; depth++) {
search(MIN_SCORE, MAX_SCORE, 0, depth);
cout << "stats depth " << depth << " time " << Timer::ms_diff(start) << endl;
}
#ifdef PRINT_HASH_STATS
sixInt st = tt->stats();
cout << "stats total=" << st.one + st.two << " hits=" << 100 * st.one / double(st.one + st.two)
<< "% scorehits=" << 100 * st.three / double(st.three + st.four)
<< "% movehits=" << 100 * st.five / double(st.five + st.six) << "%" << endl;
tt->clearStats();
#endif
// Randomize opening
if (board->getPly() < 7)
pickRandomMove();
board->recycle(curr);
return pvMove[0];
}
};
#endif