-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
152 lines (145 loc) · 5.24 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
#include <cstdint>
#include <ctime>
#include <functional>
#include <iostream>
#include <time.h>
#include "chess.hpp"
#include "search.hpp"
#include "eval.hpp"
using namespace chess;
#define ENGINE_NAME "BasicChessEngine v1.0"
#define EXTRA_DELAY 50 //time to account for communication and panic delay (in ms)
int main()
{
init_tables();
if(!alloc_hash(16)) //create 16MB TT
{
std::cerr << "Cannot allocate 16MB hash table\n";
return 1;
}
Board board = Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
//UCI loop
while(true)
{
std::string input_string;
std::getline(std::cin, input_string);
std::stringstream input_stream(input_string);
std::string command;
input_stream >> command;
switch (command[0])
{
case 'u': //uci & ucinewgame
if (command.length() > 7) //for ucinewgame
{
clear_hash(); //time-consuming: don't do it for UCI
clear_small_tables();
}
else
{
std::cout << "id name " ENGINE_NAME "\nid author Enigma\n";
//options
std::cout << "option name Hash type spin default 16 min 1 max 1024\n";
//uciok
std::cout << "uciok\n";
}
break;
case 'i': //isready
std::cout << "readyok\n";
break;
case 'q': //quit
return 0;
case 's': //setoption
input_stream >> command; //"name"
input_stream >> command; //option name
if (command[0] == 'H')
{
input_stream >> command; //"value"
input_stream >> command; //size in mb
uint32_t size_mb = std::stoi(command);
if (!alloc_hash(size_mb)) //allocate
{
//allocation failed
std::cerr << "Cannot allocate " << size_mb << "MB hash table\n";
return 1;
}
}
break;
case 'p': //position
input_stream >> command; //get new word
if (command[0] == 's') //position startpos ...
board.setFen(constants::STARTPOS); //handy
else //position fen <FEN> ... (ONLY ACCEPTS FEN WITH COUNTERS! otherwise it freaks out)
{
std::string fen_string;
for (int i = 0; i < 6; i++) //fen string has 6 "words"
{
input_stream >> command;
fen_string.append(command);
if(i != 5) fen_string.append(" "); //spaces between words
}
board.setFen(fen_string);
}
//load moves after fen string (or startpos)
input_stream >> command;
if (command[0] != 'm') break; //no moves to load
input_stream >> command;
while (input_stream)
{
board.makeMove(uci::uciToMove(board, command)); //make move
input_stream >> command; //load next move in
}
//std::cout << board.getFen() << std::endl; //DEBUG
break;
case 'g': //go command
char engine_color = (board.sideToMove() == Color::WHITE) ? 'w' : 'b';
unsigned engtime = 0; //engine's think time
unsigned enginc = 0; //engine's increment
unsigned movestogo = 30; //default to 30
unsigned depth = MAX_DEPTH; //default depth = max
unsigned movetime = 0; //no set movetime = 0 (special value)
//load parameters
input_stream >> command;
while (input_stream)
{
if (command[0] == 'd') //depth
{
input_stream >> command;
depth = std::stoi(command);
}
else if (command[0] == engine_color) //wtime, btime, winc, binc
{
if (command[1] == 't') //wtime, btime
{
input_stream >> command;
engtime = std::stoi(command);
}
else //winc, binc
{
input_stream >> command;
enginc = std::stoi(command);
}
}
else if (command[4] == 't') //movetime
{
input_stream >> command;
movetime = std::stoi(command);
}
else //movestogo
{
input_stream >> command;
movestogo = std::stoi(command);
}
input_stream >> command;
}
int alloc_time = movetime ? //time management (all in milliseconds)
movetime :
(engtime / movestogo + enginc)
- EXTRA_DELAY; //account for communication delays
//pass it on to this function in search.cpp
Move best_move = search_root(board, alloc_time, depth);
//print best move
std::cout << "bestmove " << uci::moveToUci(best_move) << std::endl;
}
}
return 0;
}