-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuci.cpp
More file actions
108 lines (99 loc) · 2.97 KB
/
uci.cpp
File metadata and controls
108 lines (99 loc) · 2.97 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
#include "uci.h"
#include "board.h"
#include "engine.h"
#include "move_gen.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void uciLoop() {
Board board;
string line, cmd;
while (getline(cin, line)) {
stringstream ss(line);
ss >> cmd;
if (cmd == "uci") {
cout << "id name Raven" << endl;
cout << "id author Tanay" << endl;
cout << "uciok" << endl;
} else if (cmd == "isready") {
cout << "readyok" << endl;
} else if (cmd == "ucinewgame") {
clearTT();
loadFEN(board,
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
} else if (cmd == "position") {
string type;
ss >> type;
if (type == "startpos") {
loadFEN(board,
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
} else if (type == "fen") {
string fen;
string word;
for (int i = 0; i < 6; i++) {
if (ss >> word) {
fen += word + " ";
}
}
loadFEN(board, fen);
}
string movesCmd;
if (ss >> movesCmd && movesCmd == "moves") {
string moveStr;
while (ss >> moveStr) {
Move m = parseMove(board, moveStr, board.whiteToMove);
if (m.from != 0) {
makeMove(board, m, board.whiteToMove);
}
}
}
} else if (cmd == "go") {
string sub;
int depth = 50;
int wtime = -1, btime = -1, winc = 0, binc = 0, movestogo = 40;
bool useTime = false;
bool depthSpecified = false;
while (ss >> sub) {
if (sub == "depth") {
ss >> depth;
depthSpecified = true;
} else if (sub == "wtime") {
ss >> wtime;
useTime = true;
} else if (sub == "btime") {
ss >> btime;
useTime = true;
} else if (sub == "winc") {
ss >> winc;
} else if (sub == "binc") {
ss >> binc;
} else if (sub == "movestogo") {
ss >> movestogo;
}
}
// wtime - how much time i have left
// winc - how much time i get per move
// movestogo - how many moves left
string bestMove;
if (useTime && !depthSpecified) {
int myTime = board.whiteToMove ? wtime : btime;
int myInc = board.whiteToMove ? winc : binc;
// timeLimitMs = (time left / moves left) + (increment / 2)
// this is a rough estimate of how much time we should spend on each
// move if timeLimitMs is less than 0ms, we will spend 50ms on each move
int timeLimitMs = (myTime / movestogo) + (myInc / 2);
if (timeLimitMs <= 0)
timeLimitMs = 50;
bestMove = iterativeDeepening(board, timeLimitMs, board.whiteToMove);
} else {
bestMove =
findBestMove(board, depthSpecified ? depth : 6, board.whiteToMove);
}
cout << "bestmove " << bestMove << endl;
} else if (cmd == "quit") {
break;
}
}
}