-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
228 lines (198 loc) · 5.79 KB
/
main.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
#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <format>
#include <fstream>
#include <unordered_map>
#include <vector>
#include <istream>
#include <cstdlib>
#include "bitboard.h"
#include "eval.h"
#include "maps.h"
#include "hash.h"
#include "move.h"
#include "movegen.h"
#include "perft.h"
#include "search.h"
#include "book.h"
#include "timeman.h"
#include "logger.h"
#define PONDER false
#define LOG_DIR "/home/jayden/Desktop/Programs/potato-chess/logs/"
const std::string START_POS = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
int main() {
#ifdef LOG_DIR
std::stringstream log_file;
log_file << LOG_DIR << "log" << time(0) << ".txt";
logger::init(log_file.str());
#else
logger::init();
#endif
srand(time(0));
maps::init();
eval::init();
hash::init();
book::book_open("/home/jayden/Desktop/Programs/potato-chess/potato-chess-uci/books/Book.bin");
bitboard::Position oldBoard(START_POS);
bitboard::Position board(START_POS);
std::unordered_map<std::string, int> options;
int totalHalfMoves = 0;
search::SearchResult lastResult = { -1, -1, -1, -1 };
while (true) {
skip:
std::string line;
getline(std::cin, line);
logger::log_input(line);
std::stringstream ss(line);
std::string token;
ss >> token;
if (token == "isready")
logger::log_output("readyok");
else if (token == "uci")
logger::log_output("id name Potato Chess\nid author Jayden Li\noption name Move Overhead type spin default 10 min 0 max 5000\nuciok");
else if (token == "position") {
totalHalfMoves = 0;
ss >> token;
if (token == "startpos")
board.decode(START_POS);
else if (token == "fen") {
std::string fen = "";
for (int i = 0; i < 6; i++) {
ss >> token;
fen += token + " ";
}
board.decode(fen);
}
bitboard::prevPositions.emplace(hash::get_hash(board));
if (!ss.eof()) {
ss >> token;
if (token == "moves") {
while (!ss.eof()) {
ss >> token;
int move = move::uci_to_move(board, token);
move::make_move(board, move);
bitboard::prevPositions.emplace(hash::get_hash(board));
totalHalfMoves++;
}
}
}
}
else if (token == "go") {
bool ponder = false;
int wtime = -1, btime = -1, winc = -1, binc = -1, movetime = -1, depth = -1;
while (ss >> token) {
if (token == "wtime")
ss >> wtime;
else if (token == "btime")
ss >> btime;
else if (token == "winc")
ss >> winc;
else if (token == "binc")
ss >> binc;
else if (token == "movetime")
ss >> movetime;
else if (token == "perft") {
int depth;
ss >> depth;
perft::PerftResult result = perft::test(board, depth);
logger::log_output(std::format("total nodes: {}", result.totalNodes));
logger::log_output(std::format("time: {}ms", result.time));
logger::log_output(std::format("nodes per second: {}", result.totalNodes / result.time * 1000));
goto skip;
}
else if (token == "depth")
ss >> depth;
else if (token == "ponder")
ponder = true;
}
search::SearchResult result;
if (ponder) {
int remainingTime = !board.turn ? btime : wtime;
int inc = !board.turn ? binc : winc;
if (inc == -1)
inc = 0;
oldBoard = board;
// move::make_move(board, lastResult.move);
// move::make_move(board, lastResult.ponder);
int time = timeman::calc_base_time(remainingTime, totalHalfMoves / 2) - options["Move Overhead"];
time = std::max(100, time);
result = search::ponder(board, time);
// *outputter << result.move << "\n";
if (result.move == -1) {
board = oldBoard;
logger::log_output(std::format("bestmove {} ponder {}",
move::to_string(lastResult.move), move::to_string(lastResult.ponder)));
goto skip;
}
}
else {
int bookMove = book::book_move(board);
if (bookMove != -1) {
logger::log_output(std::format("bestmove {}", move::to_string(bookMove)));
continue;
}
if (movetime != -1)
result = search::search_by_time(board, movetime, true);
else if (depth != -1)
result = search::search_by_depth(board, depth);
else if (wtime != -1 && btime != -1) {
int remainingTime = board.turn ? btime : wtime;
int inc = board.turn ? binc : winc;
if (inc == -1)
inc = 0;
int time = timeman::calc_base_time(remainingTime, totalHalfMoves / 2) - options["Move Overhead"];
time = std::max(100, time);
result = search::search_by_time(board, time + (inc * 0.5), false);
search::table_clear_move();
}
else
result = search::search_unlimited(board);
}
int bestMove = result.move, ponderMove = result.ponder;
lastResult = result;
if (ponderMove != -1 && ponderMove != 0 && PONDER)
logger::log_output(std::format("bestmove {} ponder {}", move::to_string(bestMove), move::to_string(ponderMove)));
else
logger::log_output(std::format("bestmove {}", move::to_string(bestMove)));
}
else if (token == "setoption") {
std::string optionName;
int value;
while (ss >> token) {
if (token == "name") {
bool first = true;
while (ss >> token) {
if (token == "value")
break;
if (!first)
optionName += " ";
first = false;
optionName += token;
}
}
ss >> value;
options[optionName] = value;
}
}
// polyglot (opening book system) key
else if (token == "pgkey")
logger::log_output(std::to_string(book::gen_polyglot_key(board)));
else if (token == "bookmove") {
int move = book::book_move(board);
if (move == -1)
logger::log_output("no book move found for this position");
else
logger::log_output(move::to_string(move));
}
else if (token == "print")
board.print();
else if (token == "quit")
break;
else if (token == "ucinewgame")
search::table_clear();
}
logger::close();
return 0;
}