-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUCI.cpp
182 lines (160 loc) · 4.31 KB
/
UCI.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
//
// UCI.cpp for ChessIA in /home/wilmot_p/PROG/C++/ChessIA
//
// Made by WILMOT Pierre
// Login <wilmot@epitech.net>
//
// Started on Thu Jan 3 20:14:06 2013 WILMOT Pierre
// Last update Thu Dec 5 21:13:05 2013 WILMOT Pierre
//
#include <iostream>
#include <sstream>
#include "UCI.hpp"
#include "LogManager.hpp"
#include "ChessBoard.hpp"
UCI::UCI(std::string const & name, std::string const & author)
:m_name(name), m_author(author)
{
threadIt();
}
UCI::~UCI()
{
}
bool UCI::registerEngine() const
{
std::cout << "id name " << m_name << std::endl;
LogManager::getInstance()->log("Sent [id name "+m_name+"]", LogManager::UCI_OUT);
std::cout << "id author " << m_author << std::endl;
LogManager::getInstance()->log("Sent [id author "+m_author+"]", LogManager::UCI_OUT);
std::cout << "uciok" << std::endl;
LogManager::getInstance()->log("Sent [uciok]", LogManager::UCI_OUT);
return true;
}
void UCI::setDebug(std::string const &s)
{
std::stringstream ss(s);
std::string word;
ss >> word;
ss >> word;
if (word == "on")
{
LogManager::getInstance()->log("Set debug On", LogManager::UCI);
std::cout << "info Debug set on" << std::endl;
// TODO : Set debug on
}
if (word == "off")
{
LogManager::getInstance()->log("Set debug Off", LogManager::UCI);
// TODO : Set debug off
}
}
void UCI::isReady()
{
m_actionQueueMutex.lock();
m_actionQueue.push(new Action(Action::IsReady, "isReady"));
m_actionQueueMutex.unlock();
LogManager::getInstance()->log("Pushed a isready action to the queue", LogManager::UCI);
}
void UCI::position(std::string const &s)
{
std::string fen;
fen = s.substr(9, s.size() - 9);
if (fen == "startpos")
{
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
m_actionQueueMutex.lock();
m_actionQueue.push(new Action(Action::Position, fen));
m_actionQueueMutex.unlock();
LogManager::getInstance()->log("Pushing position "+fen, LogManager::UCI);
}
else
{
if (fen.compare(0, 15, "startpos moves ") == 0) // glchess position format
{
fen = generateFenFromStarposMoves(fen);
m_actionQueueMutex.lock();
m_actionQueue.push(new Action(Action::Position, fen));
m_actionQueueMutex.unlock();
LogManager::getInstance()->log("Pushing position "+fen, LogManager::UCI);
}
}
}
Action *UCI::getAction()
{
Action *a;
m_actionQueueMutex.lock();
if (m_actionQueue.empty())
{
m_actionQueueMutex.unlock();
m_varcond.wait();
}
else
{
a = m_actionQueue.front();
m_actionQueue.pop();
m_actionQueueMutex.unlock();
LogManager::getInstance()->log("Returning action "+a->getFen(), LogManager::UCI);
return a;
}
return (NULL);
}
void UCI::sendMove(Move const &m) const
{
LogManager::getInstance()->log("Sending Move "+m.getLAN(), LogManager::UCI_OUT);
std::cout << "bestmove " << m.getLAN() << std::endl;
}
void UCI::readyOK()
{
std::cout << "readyok" << std::endl;
}
void UCI::go(std::string const &s)
{
(void)s;
m_actionQueueMutex.lock();
m_actionQueue.push(new Action(Action::Go, s));
m_actionQueueMutex.unlock();
LogManager::getInstance()->log("Pushed a go action to the queue", LogManager::UCI);
}
std::string UCI::generateFenFromStarposMoves(std::string const &s) const
{
ChessBoard cb;
Move m(GameData(), 3, 3, 3, 3);
std::string move;
std::stringstream ss(s);
LogManager::getInstance()->log("Generating FEN for "+s, LogManager::UCI);
ss >> move;
ss >> move;
ss >> move;
while (move != "")
{
m.setFromLAN(move);
cb.applyMove(m);
move = "";
ss >> move;
}
LogManager::getInstance()->log("Result is "+cb.getFenString(), LogManager::UCI);
return (cb.getFenString());
}
int UCI::threadEntryPoint()
{
std::string guimsg;
while (!mustQuit())
{
std::getline(std::cin, guimsg);
LogManager::getInstance()->log("Got ["+guimsg+"] from GUI", LogManager::UCI_IN);
if (guimsg == "quit")
setMustQuit(true);
else if (guimsg == "uci")
registerEngine();
else if (guimsg.compare(0, 6, "debug ") == 0)
setDebug(guimsg);
else if (guimsg == "isready")
isReady();
else if (guimsg.compare(0, 9, "position ") == 0)
position(guimsg);
else if (guimsg.compare(0, 3, "go ") == 0)
go(guimsg);
m_varcond.signal();
}
return (0);
}