-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgtp.cpp
More file actions
398 lines (371 loc) · 11.6 KB
/
gtp.cpp
File metadata and controls
398 lines (371 loc) · 11.6 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "gtp.h"
GTP::GTP(QObject *parent) : QObject(parent)
{
}
void GTP::setEngine(GTPEngineProcess &engine){
this->engine = &engine;
}
bool GTP::successful(QByteArray &reply){
if(reply[0]=='='){
reply.remove(0,2);
return true;
}else{
qDebug() << reply;
return false;
}
}
/*
* Reply is space separated string of verticies of each newline \n
* separated dragon.
*/
QVector<QString> GTP::dragon_stones(QString color){
QStringList verticies;
QByteArray reply = engine->write( QString("dragon_stones %1").arg(color));
if(successful( reply )){
QString tmp = QString(reply);
verticies = tmp.split("\n", QString::SkipEmptyParts);
}
return verticies.toVector();
}
QVector<QString> GTP::worm_stones(QString color){
QStringList verticies;
QByteArray reply = engine->write( QString("worm_stones %1").arg(color));
if(successful( reply )){
QString tmp = QString(reply);
verticies = tmp.split("\n", QString::SkipEmptyParts);
}
return verticies.toVector();
}
QVector<QString> GTP::final_status_list(QString type){
QStringList verticies;
QByteArray reply = engine->write( QString("final_status_list %1").arg(type));
if(successful( reply )){
QString tmp = QString(reply);
verticies = tmp.split("\n", QString::SkipEmptyParts);
}
return verticies.toVector();
}
/*
* read: "= C16 15.93 J14 9.73 H13 8.71 H14 8.29 B7 8.15 N3 8.01 C15 7.80 K14 7.42 G14 7.33 G13 7.25 \n\n"
showTopMoves: something went wrong i= 12 vertices.length()= 13
("c16", "15.93", "j14", "9.73", "h13", "8.71", "h14", "8.29", "b7", "3 8.01", "5 7.80", "33", "3 7.25")
QStringList GTP::top_moves(QString color){
bool ok;
QStringList verticies;
QByteArray reply = engine->write( QString("top_moves_%1").arg(color));
if(successful( reply )){
verticies = getVerticies(reply, ok);
if(!ok) {
qDebug() << "Error: top_moves_"<< color <<" "<<reply;
}else{
emit hints(color, verticies);
}
}
return verticies;
}
*/
QStringList GTP::top_moves(QString color){
bool ok;
QStringList verticies;
QByteArray reply = engine->write( QString("top_moves_%1").arg(color));
if(successful( reply )){
verticies = getVertexScores(reply, ok);
if(!ok) {
qDebug() << "Error: top_moves_"<< color <<" "<<reply;
}else{
emit hints(color, verticies);//not really verticies
}
}
return verticies;
}
int GTP::captures(QString color){
bool ok;
int captures=0;
QByteArray reply = engine->write( QString("captures %1").arg(color));
if(successful( reply )){
captures = getInt(reply, ok);
if(!ok) {
qDebug() << "Error: captures " << color << " "<< reply;
}else{
emit captureCount(color, captures);
}
}
return captures;
}
bool GTP::undo(int moves=1){
bool ret = false;
QByteArray reply;
if(moves == 1){
reply = engine->write( QString("undo").arg(moves));
}else{
reply = engine->write( QString("gg-undo %1").arg(moves));
}
if(successful( reply )){
ret = captures("black");
ret &= captures("white");
list_stones("black");
list_stones("white");
}
return ret;
}
bool GTP::pass(QString color){
bool ret = false;
QByteArray reply = engine->write( QString("play %1 pass").arg(color));
if(successful( reply )){
ret=true;
}
return ret;
}
/*
"0" (zero) for a draw (jigo)
"B+score" for a black win and
"W+score" for a white win, e.g. "B+2.5", "W+64" or "B+0.5"
"B+R"/"B+Resign" and "W+R"/"W+Resign" for a win by resignation.
You MUST NOT write "Black resigns" // assuming this means in file format?
*/
void GTP::final_score(){
QByteArray reply = engine->write( QString("final_score"));
QString white_score, black_score;
if(successful( reply )){
QRegularExpression re(commonREs.value("sgf_score"));
QRegularExpressionMatch match = re.match(reply);
QString color, score;
if (match.hasMatch()) {
color = match.captured("color");
score = match.captured("score");
if(score.contains("R", Qt::CaseInsensitive)){
//resigned
if(color.contains("b", Qt::CaseInsensitive)){
black_score = "Resigned";
white_score = "Wins";
}else{
white_score = "Resigned";
black_score = "Wins";
}
}else{
if(color.contains("b", Qt::CaseInsensitive)){
black_score=QString("Wins: %1").arg(score);
}else if(color.contains("w", Qt::CaseInsensitive)){
white_score = QString("Wins: %1").arg(score);
}else if(color.contains("0")){
black_score="Draw (jigo)";
white_score="Draw (jigo)";
}
}
}
}else{
black_score = tr("Unknown");
white_score = tr("Unknown");
}
emit blackScore(black_score);
emit whiteScore(white_score);
}
QStringList GTP::move_reasons(QString vertex){
QStringList reasons;
QByteArray reply = engine->write( QString("move_reasons %1").arg(vertex));
if(successful( reply )){
QString r = QString(reply);
reasons = r.split("\n");
}else{
qDebug() << "move_reasons error: "<< vertex <<": "<<reply;
}
return reasons;
}
QStringList GTP::list_stones(QString color){
bool ok;
QStringList verticies;
QByteArray reply = engine->write( QString("list_stones %1").arg(color));
if(successful( reply )){
verticies = getVerticies(reply, ok);
if(!ok){
qDebug() << "list_stones: "<< color <<" "<<reply;
}else{
emit stoneListing(color, verticies);
}
}
return verticies;
}
int GTP::getInt(QByteArray reply, bool &found){
QRegularExpression re(commonREs.value("int"));
QRegularExpressionMatch match = re.match(reply);
int the_int;
if (match.hasMatch()) {
the_int = match.captured("int").toInt();
found=true;
}else{
found=false;
}
return the_int;
}
QString GTP::getVertex(QByteArray reply, bool &found){
QRegularExpression re(commonREs.value("vertex"));
QRegularExpressionMatch match = re.match(reply);
QString vertex;
if (match.hasMatch()) {
vertex = match.captured("vertex");
vertex = vertex.toLower();
found=true;
}else{
found=false;
}
return vertex;
}
QStringList GTP::getVerticies(QByteArray reply, bool &found){
QStringList verticies;
QRegularExpression re(commonREs.value("verticies"));
QRegularExpressionMatchIterator i = re.globalMatch(reply);
while (i.hasNext()) {
found = true;
QRegularExpressionMatch match = i.next();
verticies.append( match.captured(1).toLower());
}
return verticies;
}
QStringList GTP::getVertexScores(QByteArray reply, bool &found){
QStringList vscores;
QRegularExpression re(commonREs.value("vertexscore"));
QRegularExpressionMatchIterator i = re.globalMatch(reply);
while (i.hasNext()) {
found = true;
QRegularExpressionMatch match = i.next();
vscores.append( match.captured(0));
}
return vscores;
}
QString GTP::genmove(QString color){
bool ok;
QString vertex;
QByteArray reply = engine->write(QString("genmove %1").arg(color));
if( successful( reply)){
vertex = getVertex(reply, ok);
if(!ok) {
QString something = QString(reply).toLower();
if(something.contains("pass", Qt::CaseInsensitive) ){
vertex = "pass";
}else if(something.contains("resign", Qt::CaseInsensitive)){
vertex = "resign";
}else{
qDebug() << "Error: genmove " << color << " " << reply;
}
}else{
emit move(color, vertex);
}
}
return vertex;
}
/*
QString GTP::genmove(QString color){
bool ret = false;
QByteArray reply = engine->write(QString("genmove %1").arg(color));
if( successful( reply)){
QString vertex = getVertex(reply, ret);
if(ret){
resetPass(color);
ui->gameBoard->placeStone(vertex, color);
ui->textHistory->appendPlainText(QString("genmove %1 %2").arg(color).arg(vertex));
captures(color);
}else{
QString something = QString(reply).toLower();
if(something.contains("pass") ){
ui->textHistory->appendPlainText(QString("genmove %1 passed").arg(color));
updatePass(color);
}else if(something.contains("resign")){
ui->textHistory->appendPlainText(QString("genmove %1 resigned").arg(color));
final_score();
}else{
ui->textHistory->appendPlainText(QString("genmove WTF %1 %2").arg(color).arg(something));
}
}
}
return ret;
}
*/
bool GTP::play(QString color, QString vertex){
QString cmd=QString("play %1 %2").arg(color).arg(vertex);
bool ret=false;
QByteArray reply = engine->write(cmd);
if( successful(reply)){
ret = true;
emit move(color, vertex);
}
return ret;
}
QString GTP::new_score(){
QString ret, white_score, black_score;
QByteArray reply = engine->write( QString("new_score"));
if(successful( reply )){
ret = QString(reply);
ret=ret.replace("upper bound:","U:");
ret=ret.replace("lower:","L:");
if(ret.contains("b", Qt::CaseInsensitive)){
black_score = ret;
}else{
white_score = ret;
}
emit blackScore(black_score);
emit whiteScore(white_score);
}
return ret;
}
QStringList GTP::fixed_handicap(int handicap){
bool ok;
QStringList verticies;
QByteArray reply = engine->write( QString("fixed_handicap %1").arg(handicap));
if(successful( reply )){
verticies = getVerticies(reply, ok);
if(!ok){
qDebug() << "fixed_handicap: "<< handicap <<" "<<reply;
}else{
emit stoneListing("black", verticies);
}
}
return verticies;
}
bool GTP::boardsize(int size){
bool ret = false;
QByteArray reply = engine->write( QString("boardsize %1").arg(size));
if(successful( reply )){
ret=true;
}else{
qDebug() << "Error boardsize: "<<size<< " "<< reply;
}
return ret;
}
bool GTP::komi(qreal komi){
bool ret = false;
QByteArray reply = engine->write( QString("komi %1").arg(komi));
if(successful( reply )){
ret=true;
}else{
qDebug() << "komi: "<<komi<< " "<< reply;
}
return ret;
}
bool GTP::printsgf(QString filename){
//oddly, command is like: printsgf filename.sgf
bool ret = false;
QByteArray reply = engine->write( QString("printsgf %1").arg(filename));
if(successful( reply )){
ret=true;
}else{
qDebug() << "printsgf: "<<filename<<" "<< reply;
}
return ret;
}
bool GTP::loadsgf(QString filename, QString &color){
//loadsgf ../build-GoGoGo-Qt_5_6_0-Debug/wtf.sgf
bool ret = false;
QByteArray reply = engine->write( QString("loadsgf %1").arg(filename));
if(successful( reply )){
QRegularExpression re(commonREs.value("word"));
QRegularExpressionMatch match = re.match(reply);
//IIRC, it actually returns a color, so probably should set proper turn
if (match.hasMatch()) {
color = match.captured("word");
ret=true;
}else{
qDebug() << "loadsgf: "<< reply;
}
}
return ret;
}