-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUCI.cpp
More file actions
179 lines (162 loc) · 5.82 KB
/
UCI.cpp
File metadata and controls
179 lines (162 loc) · 5.82 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
//
// Created by LociStar on 29.09.2020.
//
#include <sstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include "UCI.h"
bool startsWith(std::string mainStr, std::string toMatch) {
// std::string::find returns 0 if toMatch is found at starting
if (mainStr.find(toMatch) == 0)
return true;
else
return false;
}
void UCI::uciCommunication() {
std::string input;
while (true) {
std::getline(std::cin, input);
if ("uci" == input) {
inputUCI();
} else if (startsWith(input, "setoption")) {
inputSetOption(input);
} else if (input == "isready") {
inputIsReady();
} else if (input == "ucinewgame") {
inputUCINewGame();
} else if (startsWith(input, "position")) {
inputPosition(input);
} else if (startsWith(input, "go")) {
inputGo(input);
} else if (input == "print") {
inputPrint();
} else if (input == "quit") {
inputQuit(input);
break;
} else if (input == "stop") {
//MoveList legalMoves = MoveGenerator.generateLegalMoves(board);
}
}
}
void UCI::inputUCI() {
std::cout << "id name " << ENGINENAME << std::endl;
std::cout << "id author LociStar" << std::endl;
//options go here
std::cout << "uciok" << std::endl;
}
void UCI::inputSetOption(std::string input) {
//set options
}
void UCI::inputIsReady() {
std::cout << "readyok" << std::endl;
}
void UCI::inputUCINewGame() {
delete engine;
engine = new Engine();
initialise_all_databases();
zobrist::initialise_zobrist_keys();
//Position::set("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", engine->position);
newGame = true;
}
void UCI::inputQuit(std::string input) {
delete engine;
exit(0);
}
void UCI::inputPosition(std::string input) {
//std::fill_n(engine->position.history,256, UndoInfo());
//std::cout << engine->position << std::endl;
input.erase(0, 9);
if (startsWith(input, "startpos")) {
input.erase(0, 9);
if (newGame) {
Position::set("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", engine->position);
newGame = false;
}
} else if (startsWith(input, "fen")) {
input.erase(0, 4);
if (newGame) {
Position::set(input, engine->position);
newGame = false;
}
}
if (startsWith(input, "moves")) {
//std::cout << engine->position;
input.erase(0, 6);
//split string at whitespaces
std::vector<std::string> result;
std::istringstream iss(input);
for (std::string s; iss >> s;)
result.push_back(s);
Move *move = nullptr;
if (false) {
for (auto &item : result) {
//if (engine->position.history[engine->position.ply()].epsq == )
std::cout << "LOOP" << item << std::endl;
move = new Move(item);
//generate MoveList for validation
if (engine->position.turn() == 0) {
MoveList<WHITE> moveList1(engine->position);
for (Move legalMove : moveList1) {
//move validation check
if (legalMove.from() == move->from() && legalMove.to() == move->to()) {
std::cout << "Input White: " << move->from() << move->to() << std::endl;
engine->position.play<WHITE>(legalMove);
}
}
} else {
MoveList<BLACK> moveList2(engine->position);
for (Move legalMove : moveList2) {
std::cout << "Black: " << legalMove << std::endl;
//move validation check
if (legalMove.from() == move->from() && legalMove.to() == move->to()) {
std::cout << "Input Black: " << move->from() << move->to() << std::endl;
engine->position.play<BLACK>(legalMove);
}
}
}
move = nullptr;
}
newGame = true;
} else {
move = new Move(result[result.size() - 1]);
if (engine->position.turn() == 0) {
MoveList<WHITE> moveList3(engine->position);
for (Move legalMove: moveList3) {
//move validation check
if (legalMove.from() == move->from() && legalMove.to() == move->to()) {
//std::cout << legalMove;
engine->position.play<WHITE>(legalMove);
}
}
} else {
MoveList<BLACK> moveList4(engine->position);
for (Move legalMove: moveList4) {
//move validation check
if (legalMove.from() == move->from() && legalMove.to() == move->to()) {
engine->position.play<BLACK>(legalMove);
}
}
}
move = new Move(result.back());
}
delete move;
}
}
void UCI::inputGo(std::string input) {
engine->search();
std::cout << "\nbestmove " << engine->bestMove << std::endl;
}
void UCI::inputPrint() {
std::cout << engine->position << std::endl;
if (engine->position.turn() == 0) std::cout << engine->evaluation() << std::endl;
else std::cout << engine->evaluation() << std::endl;
std::cout << "Side: " << engine->position.turn() << std::endl;
std::cout << "Draw: " << engine->isDraw() << std::endl;
/*MoveList<WHITE> moveList3(engine->position);
for (Move legalMove: moveList3) {
std::cout << legalMove;
}
std::cout << std::endl;*/
}