Skip to content

Commit 6bbe7bd

Browse files
UndefinedCppUndefinedCpp
authored andcommitted
chore: add version message
1 parent 3985faf commit 6bbe7bd

File tree

7 files changed

+59
-49
lines changed

7 files changed

+59
-49
lines changed

build.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
parser.add_argument('--optimize', '-O', action='store_true',
77
help='Optimize the build', default=True)
88
parser.add_argument('--arch', '-a', type=str, default='native')
9-
parser.add_argument('--target', type=str, default='engine.exe')
9+
parser.add_argument('--target', type=str, default='engine')
1010
parser.add_argument('--run', '-r', action='store_true',
1111
help='Run the program after building', default=False)
1212
parser.add_argument('--static', action='store_true')
1313
parser.add_argument('--iterative', action='store_true')
14+
parser.add_argument('--release', action='store_true')
1415

1516
SOURCE_DIR = './src'
1617

@@ -28,6 +29,10 @@
2829
if os.path.exists('./build/engine.exe'):
2930
os.rename('./build/engine.exe', './build/engine.old.exe')
3031

32+
target_name = f'./build/{args.target}.{args.arch}.exe' \
33+
if args.release \
34+
else f'./build/{args.target}.exe'
35+
3136
command = [
3237
'g++',
3338
'-O3' if args.optimize else '',
@@ -36,9 +41,9 @@
3641
'-std=c++17',
3742
f'-march={args.arch}',
3843
'-o',
39-
f'./build/{args.target}',
44+
target_name,
4045
]
41-
if args.static:
46+
if args.static or args.release:
4247
command.extend([
4348
'-static-libstdc++',
4449
'-static-libgcc',

src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
using namespace std;
66

77
int main() {
8-
cout << "Emerald Chess Engine by UndefinedCpp, indev version" << endl;
9-
tt.init(1024 * 1024); // temporary initialize transposition table
8+
cout << "Emerald Chess Engine by UndefinedCpp, version " << ENGINE_VERSION
9+
<< endl;
10+
tt.init(8 * 1024 * 1024); // todo refactor, currently fixed at 128MB
1011

1112
std::string input;
1213
while (std::getline(std::cin, input)) {

src/search.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,17 @@ Value Searcher::qsearch(Value alpha, Value beta, int plyRemaining,
9494
template <NodeType NT>
9595
Value Searcher::negamax(Value alpha, Value beta, int plyRemaining,
9696
int plyFromRoot, bool canNullMove) {
97+
// Check game status
98+
const auto [gameOver, gameResultValue] = checkGameStatus(pos);
99+
if (gameOver) {
100+
return gameResultValue.addPly(plyFromRoot);
101+
}
97102
// Check time
98103
if (checkTime()) {
99104
searchAborted = true;
100105
return 0;
101106
}
102107

103-
// Check game status
104-
const auto [gameOver, gameResultValue] = checkGameStatus(pos);
105-
if (gameOver) {
106-
return gameResultValue.addPly(plyFromRoot);
107-
}
108108
diagnosis.nodes++;
109109

110110
if (plyFromRoot >= diagnosis.seldepth) {
@@ -137,9 +137,13 @@ Value Searcher::negamax(Value alpha, Value beta, int plyRemaining,
137137

138138
// Set up move ordering
139139
MoveOrderer<OrderMode::DEFAULT> orderer;
140-
Move ttHashMove = entry ? entry->move() : Move::NO_MOVE; // note +11.59 elo
141-
orderer.init(pos, &killerMoves[plyFromRoot],
142-
ttHashMove.isValid() ? &ttHashMove : nullptr);
140+
141+
if (entry) { // Hash move from transposition table if available
142+
Move ttHashMove = entry->move();
143+
orderer.init(pos, &killerMoves[plyFromRoot], &ttHashMove);
144+
} else {
145+
orderer.init(pos, &killerMoves[plyFromRoot]);
146+
}
143147

144148
Move bestMove(Move::NO_MOVE);
145149
Move m;
@@ -166,6 +170,7 @@ Value Searcher::negamax(Value alpha, Value beta, int plyRemaining,
166170
}
167171
}
168172

173+
// Principal Variation Search
169174
pos.makeMove(m);
170175
Value eval;
171176
if (movesSearched == 0) {
@@ -189,13 +194,6 @@ Value Searcher::negamax(Value alpha, Value beta, int plyRemaining,
189194
// Update killer and history heuristics for non-capture moves
190195
if (!pos.isCapture(m)) {
191196
this->killerMoves[plyFromRoot].add(m);
192-
193-
// // todo always refactor this
194-
// int bonus = std::clamp(plyRemaining * plyRemaining, -500,
195-
// 500); int &ref =
196-
// this->historyHeuristics[(int)pos.sideToMove()]
197-
// [m.from().index()][m.to().index()];
198-
// ref += bonus - ref * std::abs(bonus) / 500;
199197
}
200198
return beta;
201199
}
@@ -264,38 +262,40 @@ std::pair<Move, Value> Searcher::search() {
264262

265263
const auto timeBegin = std::chrono::steady_clock::now();
266264
startTime = timeBegin;
267-
diagnosis.nodes = 0;
268-
diagnosis.qnodes = 0;
269-
result = negamax_root(1, MATE_VALUE, MATED_VALUE);
270-
Value guessValue = result.second;
271-
272-
for (int depth = 2; depth <= 32; ++depth) {
273-
result = negamax_root(depth, MATE_VALUE, MATED_VALUE);
274-
if (searchAborted) {
275-
break;
276-
}
277-
278-
pvMoveFromIteration = result.first;
279-
pvScoreFromIteration = result.second;
280-
guessValue = pvScoreFromIteration;
281265

266+
for (int depth = 1; depth <= 32; ++depth) {
282267
const auto timeEnd = std::chrono::steady_clock::now();
283268
const uint32_t timeElapsed =
284269
std::chrono::duration_cast<std::chrono::milliseconds>(timeEnd -
285270
timeBegin)
286271
.count();
287-
// Print uci output
288-
std::cout << "info depth " << depth << " score " << result.second
289-
<< " seldepth " << (int)diagnosis.seldepth << " hashfull "
290-
<< tt.hashfull() << " time " << timeElapsed << " nodes "
291-
<< (int)diagnosis.nodes << " pv "
292-
<< chess::uci::moveToUci(pvMoveFromIteration) << std::endl;
293-
// std::cout << "info string non-qs nodes: "
294-
// << (int)(diagnosis.nodes - diagnosis.qnodes) << std::endl;
295-
296272
if (timeElapsed > maxThinkingTime) {
297273
break;
298274
}
275+
276+
result = negamax_root(depth, MATE_VALUE, MATED_VALUE);
277+
if (searchAborted) {
278+
break;
279+
}
280+
281+
// Print uci output
282+
if (timeElapsed >= 1) {
283+
uint64_t nps = diagnosis.nodes * 1000 / timeElapsed;
284+
std::cout << "info depth " << depth << " score " << result.second
285+
<< " seldepth " << (int)diagnosis.seldepth << " hashfull "
286+
<< tt.hashfull() << " time " << timeElapsed << " nodes "
287+
<< (int)diagnosis.nodes << " nps " << nps << " pv "
288+
<< chess::uci::moveToUci(pvMoveFromIteration)
289+
<< std::endl;
290+
} else {
291+
std::cout << "info depth " << depth << " score " << result.second
292+
<< " seldepth " << (int)diagnosis.seldepth << " hashfull "
293+
<< tt.hashfull() << " time " << timeElapsed << " nodes "
294+
<< (int)diagnosis.nodes << std::endl;
295+
}
296+
297+
pvMoveFromIteration = result.first;
298+
pvScoreFromIteration = result.second;
299299
}
300300
std::cout << std::flush;
301301
return {pvMoveFromIteration, pvScoreFromIteration};

src/timemgr.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
#include "timemgr.h"
22
#include <algorithm>
3+
#include <cmath>
34

45
void TimeManager::update(uint32_t remainingTime, uint32_t increment) {
56
_remainingTime = remainingTime;
67
_increment = increment;
78
}
89

910
uint32_t TimeManager::spareTime(int ply) {
10-
if (_remainingTime <= 500) {
11-
return _increment;
11+
if (_remainingTime <= 1000) {
12+
float ratio = _remainingTime / 1000.0f;
13+
return (_remainingTime + _increment) * ratio * ratio / 4.0;
1214
}
13-
if (ply >= 35) {
15+
if (ply >= 40) {
1416
if (_remainingTime <= 100) {
1517
return _increment / 2;
1618
} else {
1719
return std::max(20u, _remainingTime / 4);
1820
}
1921
} else {
2022
return std::max(_increment - 1,
21-
_remainingTime / (35 - ply) + _increment);
23+
_remainingTime / (40 - ply) + _increment);
2224
}
2325
}

src/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ struct KillerHeuristics {
2121
bool has(Move move) {
2222
return move.move() == killer1 || move.move() == killer2;
2323
}
24-
};
24+
};

src/uci.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void execute(const std::string &command) {
2121

2222
// <Command> uci
2323
if (token == "uci") {
24-
std::cout << "id name Emerald" << std::endl;
24+
std::cout << "id name Emerald " << ENGINE_VERSION << std::endl;
2525
std::cout << "id author UndefinedCpp" << std::endl;
2626
// TODO Send options
2727
std::cout << "uciok" << std::endl;

src/uci.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <sstream>
55
#include <string>
66

7+
#define ENGINE_VERSION "0.1.1"
8+
79
namespace uci {
810

911
void execute(const std::string &command);

0 commit comments

Comments
 (0)