-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuci.cpp
More file actions
188 lines (169 loc) · 5.68 KB
/
Copy pathuci.cpp
File metadata and controls
188 lines (169 loc) · 5.68 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
#include <iostream>
#include <string>
#include <bitset>
#include <sstream>
#include "Board.h"
#include <chrono>
#include "zobrist.h"
#include "move.h"
#include <cstring>
struct Board;
Board board;
extern int gameHistory[2][64][64];
extern std::vector<uint64_t> repHistory;
bool makeMove(Move move, Board &board);
Move findBestMove(const Board &board, int depth);
Move parseMove(const std::string &moveStr, const Board &board);
std::string moveToUCI(Move m);
void uciLoop()
{
std::string line;
board.whiteToMove = true;
while (std::getline(std::cin, line))
{
std::istringstream iss(line);
std::string token;
iss >> token;
if (token == "uci")
{
std::cout << "id name GrandMater\n";
std::cout << "id author ognjen-simic\n";
std::cout << "uciok\n";
}
else if (token == "isready")
{
std::cout << "readyok\n";
}
else if (token == "ucinewgame")
{
board.whitePawns = 0x000000000000FF00ULL;
board.whiteRooks = (1ULL << 0) | (1ULL << 7);
board.whiteKnights = (1ULL << 1) | (1ULL << 6);
board.whiteBishops = (1ULL << 2) | (1ULL << 5);
board.whiteQueen = (1ULL << 3);
board.whiteKing = (1ULL << 4);
board.blackPawns = 0x00FF000000000000ULL;
board.blackRooks = (1ULL << 56) | (1ULL << 63);
board.blackKnights = (1ULL << 57) | (1ULL << 62);
board.blackBishops = (1ULL << 58) | (1ULL << 61);
board.blackQueen = (1ULL << 59);
board.blackKing = (1ULL << 60);
board.whiteToMove = true;
board.whiteCanCastleKingside = true;
board.whiteCanCastleQueenside = true;
board.blackCanCastleKingside = true;
board.blackCanCastleQueenside = true;
board.whiteCastled = false;
board.blackCastled = false;
board.en_passant = -1;
board.hash = Zobrist::computeHash(board);
repHistory.clear();
repHistory.push_back(board.hash);
memset(gameHistory, 0, sizeof(gameHistory));
}
else if (token == "position")
{
std::string next;
iss >> next;
if (next == "startpos")
{
board.whitePawns = 0x000000000000FF00ULL;
board.whiteRooks = (1ULL << 0) | (1ULL << 7);
board.whiteKnights = (1ULL << 1) | (1ULL << 6);
board.whiteBishops = (1ULL << 2) | (1ULL << 5);
board.whiteQueen = (1ULL << 3);
board.whiteKing = (1ULL << 4);
board.blackPawns = 0x00FF000000000000ULL;
board.blackRooks = (1ULL << 56) | (1ULL << 63);
board.blackKnights = (1ULL << 57) | (1ULL << 62);
board.blackBishops = (1ULL << 58) | (1ULL << 61);
board.blackQueen = (1ULL << 59);
board.blackKing = (1ULL << 60);
board.whiteToMove = true;
board.whiteCanCastleKingside = true;
board.whiteCanCastleQueenside = true;
board.blackCanCastleKingside = true;
board.blackCanCastleQueenside = true;
board.whiteCastled = false;
board.blackCastled = false;
board.en_passant = -1;
board.hash = Zobrist::computeHash(board);
repHistory.clear();
repHistory.push_back(board.hash);
}
else if (next == "fen")
{
std::string fen, word;
fen = "";
while (iss >> word && word != "moves")
{
fen += word + " ";
}
fen.pop_back();
board.setFromFEN(fen);
if (word == "moves")
{
std::string move;
while (iss >> move)
{
Move m = parseMove(move, board);
if (m != 0)
makeMove(m, board);
}
}
}
std::string word;
if (iss >> word && word == "moves")
{
std::string move;
while (iss >> move)
{
Move m = parseMove(move, board);
if (m != 0)
{
makeMove(m, board);
repHistory.push_back(board.hash);
}
}
}
}
else if (token == "go")
{
int timeLeft = 5000;
int moveTime = -1;
std::string sub;
while (iss >> sub)
{
if (sub == "wtime" && board.whiteToMove)
{
iss >> timeLeft;
}
else if (sub == "btime" && !board.whiteToMove)
{
iss >> timeLeft;
}
else if (sub == "movetime")
{
iss >> moveTime;
}
}
int timeToMove;
if (moveTime > 0)
{
timeToMove = moveTime;
}
else
{
timeToMove = timeLeft / 30;
timeToMove = std::max(100, timeToMove);
}
Move bestMove = findBestMove(board, timeToMove);
std::cout << "bestmove " << moveToUCI(bestMove) << "\n"
<< std::flush;
}
else if (token == "quit")
{
break;
}
}
}