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
|
#include "uci.hpp"
#include "board/board.hpp"
#include "board/fen.hpp"
#include "bot.hpp"
#include "moves.hpp"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <print>
#include <sstream>
#include <string>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <unordered_map>
std::ofstream uciLog("cache/uci_log.txt", std::ios::app);
bool LoadTable(
const std::string &filename,
std::unordered_map<uint64_t, TranspositionsEntry> *transpositions) {
std::ifstream file(filename);
if (!file.is_open())
return false;
uint64_t hash;
int depth;
float eval;
while (file >> hash >> depth >> eval) {
transpositions->operator[](hash) = {depth, eval};
}
return true;
}
void SaveTranspositionTable(
const std::unordered_map<uint64_t, TranspositionsEntry> &table) {
std::ofstream file("cache/positions.txt", std::ios::trunc);
for (const auto &[key, value] : table) {
file << key << ' ' << value.depth << ' ' << value.Eval << '\n';
}
};
void LogUci(const std::string &message) {
if (!uciLog.is_open()) {
return;
}
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
uciLog << "[" << std::put_time(std::localtime(&time), "%H:%M:%S") << "] "
<< message << "\n";
uciLog.flush();
}
using namespace std;
Move UciToMove(string uci) {
Position from;
Position to;
from.file = uci[0] - 'a';
from.rank = '8' - uci[1];
to.file = uci[2] - 'a';
to.rank = '8' - uci[3];
return {from, to};
}
Game initBoard(
string startingFEN,
std::unordered_map<uint64_t, TranspositionsEntry> *transPositions) {
Game b{};
b.enPassant = IndexToPosition(0); // default value
b.canEnpassant = false;
b.state = TURN;
b.turn = true;
b.halfMoveClock = 0;
b.MoveClock = 0;
b.Transpositions = transPositions;
Piece EmptyPiece = {
false,
NONEPIECE,
};
for (int i = 0; i < 64; i++) {
b.pieces[i] = EmptyPiece;
};
setBoardFen(startingFEN, &b);
return b;
};
void Uci() {
unordered_map<uint64_t, TranspositionsEntry> transpositions = {};
Game game =
initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
&transpositions);
string line;
while (getline(cin, line)) {
string cmd = line;
LogUci("GUI -> ENGINE: " + line);
transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);
if (cmd == "quit") {
break;
}
if (cmd == "save") {
SaveTranspositionTable(transpositions);
cout << "done\n";
cout.flush();
}
if (cmd == "board") {
for (int i = 0; i < 64; i++) {
Piece piece = game.pieces[i];
if (piece.type == NONEPIECE) {
cout << " --- ";
continue;
}
printf(" %d%d", piece.type, piece.color);
if (i % 8 == 0) {
println();
}
}
cout.flush();
}
if (cmd == "load") {
LoadTable("cache/positions.txt", &transpositions);
cout << "done\n";
cout.flush();
}
if (cmd == "uci") {
cout << "uciok\n";
cout.flush();
} else if (cmd == "ucinewgame") {
game =
initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
&transpositions);
} else if (cmd == "isready") {
cout << "readyok\n";
cout.flush();
} else if (cmd.starts_with("go")) {
stringstream ss(line);
string token;
int depth = 4; // default depth
ss >> token; // go
while (ss >> token) {
if (token == "depth") {
ss >> depth;
break;
}
}
Move best = EngineGetBestMove(&game, depth);
cout << "bestmove ";
cout << (char)(best.From.file + 'a') << 8 - best.From.rank
<< (char)(best.To.file + 'a') << 8 - best.To.rank;
if (best.promotion != NONEPIECE) {
switch (best.promotion) {
case QUEEN:
cout << "q";
break;
case ROOK:
cout << "r";
break;
case KNIGHT:
cout << "n";
break;
case BISHOP:
cout << "b";
break;
default:
assert(false && "Unexpected promotion type");
}
}
cout << "\n";
cout.flush();
} else if (cmd.starts_with("position")) {
stringstream ss(line);
string token;
ss >> token; // position
ss >> token;
if (token == "startpos") {
game = initBoard(
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
&transpositions);
} else if (token == "fen") {
string fen;
string part;
// FEN has spaces, need 6 parts
for (int i = 0; i < 6; i++) {
ss >> part;
if (i)
fen += " ";
fen += part;
}
game = initBoard(fen, &transpositions);
}
// check for moves
while (ss >> token) {
if (token == "moves")
break;
}
while (ss >> token) {
Move move = UciToMove(token);
MakeMove(move, &game);
}
}
}
}
|