1
/***************************************************************************
4
begin : Thu Nov 29 2001
5
copyright : (C) 2001 by PALM Thomas , DINTILHAC Florian, HIVERT Anthony, PIOC Sebastien
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
24
/* Function: open a session
25
* Arguments: name and path of go program
31
/*openGtpSession(filename);*/
32
programProcess = NULL; // This to permit proper ending
39
programProcess->kill(); // not very clean (should use tryTerminate)
44
/* Function: get the last message from gnugo
47
* Returns: last message from GO
51
QString QGtp::getLastMessage()
56
int QGtp::openGtpSession(QString filename, int size, float komi, int handicap, int level)
60
programProcess = new QProcess();
62
programProcess->clearArguments();
63
programProcess->addArgument(filename);
64
programProcess->addArgument("--mode");
65
programProcess->addArgument("gtp");
66
programProcess->addArgument("--quiet");
68
connect(programProcess, SIGNAL(readyReadStdout()),
69
this, SLOT(slot_readFromStdout()) );
70
connect(programProcess, SIGNAL(processExited()),
71
this, SLOT(slot_processExited()) );
73
if (!programProcess->start())
75
_response="Could not start "+filename;
79
//?? never know ... otherwise, I get a segfault with sprint ...
80
if ((outFile = (char *)malloc (100)) == NULL)
82
_response="Yuck ! Could not allocate 100 bytes !!!" ;
86
if (protocolVersion()==OK)
88
if(getLastMessage().toInt() !=2)
90
qDebug("Protocol version problem???");
92
_response="Protocol version not supported";
95
if(setBoardsize(size)==FAIL)
99
if(clearBoard()==FAIL)
101
// removed by frosla -> protocol changes...
106
if(knownCommand("level")==FAIL)
110
else if (getLastMessage().contains("true"))
112
if (setLevel(level)==FAIL)
119
if(setKomi(komi)==FAIL)
123
if(fixedHandicap(handicap)==FAIL)
131
_response="Protocol version error";
139
void QGtp::slot_readFromStdout()
143
while (programProcess->canReadLineStdout())
145
buff=programProcess->readLineStdout();
146
buff=buff.stripWhiteSpace();
147
if (buff.length() != 0)
150
// qDebug(QString("** QGtp::slot_readFromStdout(): [%1]").arg(_response));
156
void QGtp::slot_processExited()
158
qDebug(QString("%1 quit").arg(_cpt));
159
sprintf (outFile, "%d quit\n", _cpt);
161
// return waitResponse();
165
/* Function: wait for a go response
173
QString buff = _response;
174
// QTextStream * inFile;
181
qApp->processEvents();
182
} while (_response.length() == 0 || _response == buff);
185
inFile=new QTextStream(programProcess->readStdout(),IO_ReadOnly);
190
buff=inFile->readLine();
191
} while(number !=_cpt);
194
// _response=buff.stripWhiteSpace();
195
qDebug(QString("** QGtp::waitResponse(): [%1]").arg(_response));
197
buff=programProcess->readLineStdout();
198
while(!buff.isEmpty())
200
_response=_response+"\n"+buff;
201
buff=programProcess->readLineStdout();
206
if ((pos = _response.find(' ')) < 1)
208
number = _response.mid(1,pos).toInt();
209
_response = _response.right(_response.length() - pos - 1);
211
if (buff == '?') //symbole=='?')
221
/****************************
222
* Program identity. *
223
****************************/
225
/* Function: Report the name of the program.
228
* Returns: program name
233
qDebug(QString("%1 name").arg(_cpt));
234
sprintf (outFile, "%d name\n", _cpt);
236
return waitResponse();
239
/* Function: Report protocol version.
242
* Returns: protocol version number
245
QGtp::protocolVersion ()
247
qDebug(QString("%1 protocol_version").arg(_cpt));
248
sprintf (outFile, "%d protocol_version\n", _cpt);
250
return waitResponse();
253
void QGtp::fflush(char * s)
256
int msglen = strlen(s);
258
QByteArray dat(msglen);
259
for (int j = 0; j < msglen; j++) {
262
programProcess->writeToStdin(dat);
266
qDebug(QString("flush -> %1").arg(s));
267
programProcess->writeToStdin(QString::QString(s));
272
/****************************
273
* Administrative commands. *
274
****************************/
284
sprintf (outFile, "%d quit\n", _cpt);
286
return waitResponse();
289
/* Function: Report the version number of the program.
292
* Returns: version number
297
sprintf (outFile, "%d version\n", _cpt);
299
return waitResponse();
303
/***************************
304
* Setting the board size. *
305
***************************/
307
/* Function: Set the board size to NxN.
309
* Fails: board size outside engine's limits
313
QGtp::setBoardsize (int size)
315
sprintf (outFile, "%d boardsize %d\n", _cpt, size);
317
return waitResponse();
321
/* Function: Find the current boardsize
324
* Returns: board_size
327
QGtp::queryBoardsize()
329
sprintf (outFile, "%d query_boardsize\n", _cpt);
331
return waitResponse();
334
/***********************
335
* Clearing the board. *
336
***********************/
338
/* Function: Clear the board.
347
sprintf (outFile, "%d clear_board\n", _cpt);
349
return waitResponse();
352
/***************************
354
***************************/
356
/* Function: Set the komi.
358
* Fails: incorrect argument
362
QGtp::setKomi(float f)
364
sprintf (outFile, "%d komi %.2f\n", _cpt,f);
366
return waitResponse();
369
/* Function: Set the playing level.
371
* Fails: incorrect argument
375
QGtp::setLevel (int level)
377
sprintf (outFile, "%d level %d\n", _cpt,level);
379
return waitResponse();
385
/* Function: Play a black stone at the given vertex.
387
* Fails: invalid vertex, illegal move
391
QGtp::playblack (char c , int i)
393
// sprintf (outFile, "%d play black %c%d\n", _cpt,c,i);
394
sprintf (outFile, "%d play black %c%d\n", _cpt,c,i);
396
return waitResponse();
399
/* Function: Black pass.
405
QGtp::playblackPass ()
407
// sprintf (outFile, "%d play black pass\n", _cpt);
408
sprintf (outFile, "%d play black pass\n", _cpt);
410
return waitResponse();
413
/* Function: Play a white stone at the given vertex.
415
* Fails: invalid vertex, illegal move
419
QGtp::playwhite (char c, int i)
421
// sprintf (outFile, "%d play white %c%d\n", _cpt,c,i);
422
sprintf (outFile, "%d play white %c%d\n", _cpt,c,i);
424
return waitResponse();
427
/* Function: White pass.
433
QGtp::playwhitePass ()
435
// sprintf (outFile, "%d play white pass\n", _cpt);
436
sprintf (outFile, "%d play white pass\n", _cpt);
438
return waitResponse();
441
/* Function: Set up fixed placement handicap stones.
442
* Arguments: number of handicap stones
443
* Fails: invalid number of stones for the current boardsize
444
* Returns: list of vertices with handicap stones
447
QGtp::fixedHandicap (int handicap)
452
sprintf (outFile, "%d fixed_handicap %d\n", _cpt,handicap);
454
return waitResponse();
457
/* Function: Load an sgf file, possibly up to a move number or the first
458
* occurence of a move.
459
* Arguments: filename + move number, vertex, or nothing
460
* Fails: missing filename or failure to open or parse file
461
* Returns: color to play
464
QGtp::loadsgf (QString filename,int movNumber,char c,int i)
466
//sprintf (outFile, "%d loadsgf %s %d %c%d\n", _cpt,(const char *) filename,movNumber,c,i);
467
sprintf (outFile, "%d loadsgf %s\n", _cpt,(const char *) filename);
469
return waitResponse();
476
/* Function: Return the color at a vertex.
478
* Fails: invalid vertex
479
* Returns: "black", "white", or "empty"
482
QGtp::whatColor (char c, int i)
484
sprintf (outFile, "%d color %c%d\n", _cpt,c,i);
486
return waitResponse();
489
/* Function: Count number of liberties for the string at a vertex.
491
* Fails: invalid vertex, empty vertex
492
* Returns: Number of liberties.
495
QGtp::countlib (char c, int i)
497
sprintf (outFile, "%d countlib %c%d\n", _cpt,c,i);
499
return waitResponse();
502
/* Function: Return the positions of the liberties for the string at a vertex.
504
* Fails: invalid vertex, empty vertex
505
* Returns: Sorted space separated list of vertices.
508
QGtp::findlib (char c, int i)
510
sprintf (outFile, "%d findlib %c%d\n", _cpt,c,i);
512
return waitResponse();
515
/* Function: Tell whether a move is legal.
517
* Fails: invalid move
518
* Returns: 1 if the move is legal, 0 if it is not.
521
QGtp::isLegal (QString color, char c, int i)
523
sprintf (outFile, "%d is_legal %s %c%d\n", _cpt,(const char *)color,c,i);
525
return waitResponse();
528
/* Function: List all legal moves for either color.
530
* Fails: invalid color
531
* Returns: Sorted space separated list of vertices.
534
QGtp::allLegal (QString color)
536
sprintf (outFile, "%d all_legal %s\n", _cpt,(const char *)color);
538
return waitResponse();
541
/* Function: List the number of captures taken by either color.
543
* Fails: invalid color
544
* Returns: Number of captures.
547
QGtp::captures (QString color)
549
sprintf (outFile, "%d captures %s\n", _cpt,(const char *)color);
551
return waitResponse();
554
/**********************
555
* Retractable moves. *
556
**********************/
558
/* Function: Play a stone of the given color at the given vertex.
559
* Arguments: move (color + vertex)
560
* Fails: invalid color, invalid vertex, illegal move
564
QGtp::trymove (QString color, char c, int i)
566
sprintf (outFile, "%d trymove %s %c%d\n", _cpt,(const char *)color,c,i);
568
return waitResponse();
571
/* Function: Undo a trymove.
579
sprintf (outFile, "%d popgo\n", _cpt);
581
return waitResponse();
584
/*********************
585
* Tactical reading. *
586
*********************/
588
/* Function: Try to attack a string.
590
* Fails: invalid vertex, empty vertex
591
* Returns: attack code followed by attack point if attack code nonzero.
594
QGtp::attack (char c, int i)
596
sprintf (outFile, "%d attack %c%d\n", _cpt,c,i);
598
return waitResponse();
601
/* Function: Try to defend a string.
603
* Fails: invalid vertex, empty vertex
604
* Returns: defense code followed by defense point if defense code nonzero.
607
QGtp::defend (char c, int i)
609
sprintf (outFile, "%d defend %c%d\n", _cpt,c,i);
611
return waitResponse();
614
/* Function: Increase depth values by one.
620
QGtp::increaseDepths ()
622
sprintf (outFile, "%d increase_depths\n", _cpt);
624
return waitResponse();
627
/* Function: Decrease depth values by one.
633
QGtp::decreaseDepths ()
635
sprintf (outFile, "%d decrease_depths\n", _cpt);
637
return waitResponse();
644
/* Function: Try to attack a dragon.
646
* Fails: invalid vertex, empty vertex
647
* Returns: attack code followed by attack point if attack code nonzero.
650
QGtp::owlAttack (char c, int i)
652
sprintf (outFile, "%d owl_attack %c%d\n", _cpt,c,i);
654
return waitResponse();
657
/* Function: Try to defend a dragon.
659
* Fails: invalid vertex, empty vertex
660
* Returns: defense code followed by defense point if defense code nonzero.
663
QGtp::owlDefend (char c, int i)
665
sprintf (outFile, "%d owl_defend %c%d\n", _cpt,c,i);
667
return waitResponse();
674
/* Function: Evaluate an eye space
676
* Fails: invalid vertex
677
* Returns: Minimum and maximum number of eyes. If these differ an
678
* attack and a defense point are additionally returned.
679
* If the vertex is not an eye space or not of unique color,
680
* a single -1 is returned.
684
QGtp::evalEye (char c, int i)
686
sprintf (outFile, "%d eval_eye %c%d\n", _cpt,c,i);
688
return waitResponse();
695
/* Function: Determine status of a dragon.
697
* Fails: invalid vertex, empty vertex
698
* Returns: status ("alive", "critical", "dead", or "unknown"),
699
* attack point, defense point. Points of attack and
700
* defense are only given if the status is critical and the
701
* owl code is enabled.
703
* FIXME: Should be able to distinguish between life in seki
704
* and life with territory. Should also be able to identify ko.
708
QGtp::dragonStatus (char c, int i)
710
sprintf (outFile, "%d dragon_status %c%d\n", _cpt,c,i);
712
return waitResponse();
715
/* Function: Determine whether two stones belong to the same dragon.
716
* Arguments: vertex, vertex
717
* Fails: invalid vertex, empty vertex
718
* Returns: 1 if the vertices belong to the same dragon, 0 otherwise
722
QGtp::sameDragon (char c1, int i1, char c2, int i2)
724
sprintf (outFile, "%d same_dragon %c%d %c%d\n", _cpt,c1,i1,c2,i2);
726
return waitResponse();
729
/* Function: Return the information in the dragon data structure.
732
* Returns: Dragon data formatted in the corresponding way to gtp_worm__
737
sprintf (outFile, "%d dragon_data \n", _cpt);
739
return waitResponse();
743
/* Function: Return the information in the dragon data structure.
744
* Arguments: intersection
745
* Fails: invalid coordinate
746
* Returns: Dragon data formatted in the corresponding way to gtp_worm__
749
QGtp::dragonData (char c,int i)
751
sprintf (outFile, "%d dragon_data %c%d\n", _cpt,c,i);
753
return waitResponse();
757
/***********************
758
* combination attacks *
759
***********************/
761
/* Function: Find a move by color capturing something through a
762
* combination attack.
764
* Fails: invalid color
765
* Returns: Recommended move, PASS if no move found
769
QGtp::combinationAttack (QString color)
771
sprintf (outFile, "%d combination_attack %s\n", _cpt,(const char *)color);
773
return waitResponse();
776
/********************
778
********************/
780
/* Function: Generate and play the supposedly best black move.
783
* Returns: a move coordinate (or "PASS")
786
QGtp::genmoveBlack ()
788
sprintf (outFile, "%d genmove black\n", _cpt);
790
return waitResponse();
793
/* Function: Generate and play the supposedly best white move.
796
* Returns: a move coordinate (or "PASS")
799
QGtp::genmoveWhite ()
801
sprintf (outFile, "%d genmove white\n", _cpt);
803
return waitResponse();
806
/* Function: Generate the supposedly best move for either color.
807
* Arguments: color to move, optionally a random seed
808
* Fails: invalid color
809
* Returns: a move coordinate (or "PASS")
812
QGtp::genmove (QString color,int seed)
814
sprintf (outFile, "%d gg_genmove %s %d\n", _cpt,(const char *)color,seed);
816
return waitResponse();
819
/* Function : Generate a list of the best moves for White with weights
822
* Returns : list of moves with weights
826
QGtp::topMovesWhite ()
828
sprintf (outFile, "%d top_moves_white\n", _cpt);
830
return waitResponse();
833
/* Function : Generate a list of the best moves for Black with weights
836
* Returns : list of moves with weights
840
QGtp::topMovesBlack ()
842
sprintf (outFile, "%d top_moves_black\n", _cpt);
844
return waitResponse();
847
/* Function: Undo last move
849
* Fails: If move pointer is 0
855
sprintf (outFile, "%d undo %d\n", _cpt,i);
857
return waitResponse();
860
/* Function: Report the final status of a vertex in a finished game.
861
* Arguments: Vertex, optional random seed
862
* Fails: invalid vertex
863
* Returns: Status in the form of one of the strings "alive", "dead",
864
* "seki", "white_territory", "black_territory", or "dame".
867
QGtp::finalStatus (char c, int i, int seed)
869
sprintf (outFile, "%d final_status %c%d %d\n", _cpt,c,i,seed);
871
return waitResponse();
875
/* Function: Report vertices with a specific final status in a finished game.
876
* Arguments: Status in the form of one of the strings "alive", "dead",
877
* "seki", "white_territory", "black_territory", or "dame".
878
* An optional random seed can be added.
879
* Fails: missing or invalid status string
880
* Returns: Vertices having the specified status. These are split with
881
* one string on each line if the vertices are nonempty (i.e.
882
* for "alive", "dead", and "seki").
885
QGtp::finalStatusList (QString status, int seed)
887
sprintf (outFile, "%d final_status_list %s %d\n", _cpt,(const char *)status,seed);
889
return waitResponse();
895
/* Function: Compute the score of a finished game.
896
* Arguments: Optional random seed
898
* Returns: Score in SGF format (RE property).
901
QGtp::finalScore (int seed)
903
sprintf (outFile, "%d final_score %d\n", _cpt,seed);
905
return waitResponse();
909
QGtp::estimateScore ()
911
sprintf (outFile, "%d estimate_score\n", _cpt);
913
return waitResponse();
919
sprintf (outFile, "%d new_score \n", _cpt);
921
return waitResponse();
928
/* Function: Reset the count of life nodes.
934
QGtp::resetLifeNodeCounter ()
936
sprintf (outFile, "%d reset_life_node_counter\n", _cpt);
938
return waitResponse();
941
/* Function: Retrieve the count of life nodes.
944
* Returns: number of life nodes
947
QGtp::getLifeNodeCounter ()
949
sprintf (outFile, "%d get_life_node_counter\n", _cpt);
951
return waitResponse();
954
/* Function: Reset the count of owl nodes.
960
QGtp::resetOwlNodeCounter ()
962
sprintf (outFile, "%d reset_owl_node_counter\n", _cpt);
964
return waitResponse();
967
/* Function: Retrieve the count of owl nodes.
970
* Returns: number of owl nodes
973
QGtp::getOwlNodeCounter ()
975
sprintf (outFile, "%d get_owl_node_counter\n", _cpt);
977
return waitResponse();
980
/* Function: Reset the count of reading nodes.
986
QGtp::resetReadingNodeCounter ()
988
sprintf (outFile, "%d reset_reading_node_counter\n", _cpt);
990
return waitResponse();
994
/* Function: Retrieve the count of reading nodes.
997
* Returns: number of reading nodes
1000
QGtp::getReadingNodeCounter ()
1002
sprintf (outFile, "%d get_reading_node_counter\n", _cpt);
1004
return waitResponse();
1007
/* Function: Reset the count of trymoves/trykos.
1013
QGtp::resetTrymoveCounter ()
1015
sprintf (outFile, "%d reset_trymove_counter\n", _cpt);
1017
return waitResponse();
1020
/* Function: Retrieve the count of trymoves/trykos.
1023
* Returns: number of trymoves/trykos
1026
QGtp::getTrymoveCounter ()
1028
sprintf (outFile, "%d get_trymove_counter\n", _cpt);
1030
return waitResponse();
1037
/* Function: Write the position to stderr.
1045
sprintf (outFile, "%d showboard\n", _cpt);
1047
return waitResponse();
1051
/* Function: Dump stack to stderr.
1059
sprintf (outFile, "%d dump_stack\n", _cpt);
1061
return waitResponse();
1064
/* Function: Write information about the influence function to stderr.
1065
* Arguments: color to move, optionally a list of what to show
1066
* Fails: invalid color
1070
QGtp::debugInfluence (QString color,QString list)
1072
sprintf (outFile, "%d debug_influence %s %s\n", _cpt,(const char *)color,(const char *)list);
1074
return waitResponse();
1077
/* Function: Write information about the influence function after making
1079
* Arguments: move, optionally a list of what to show
1084
QGtp::debugMoveInfluence (QString color, char c, int i,QString list)
1086
sprintf (outFile, "%d debug_move_influence %s %c%d %s\n", _cpt,(const char *)color,c,i,(const char *)list);
1088
return waitResponse();
1091
/* Function: Return information about the influence function.
1092
* Arguments: color to move
1094
* Returns: Influence data formatted
1097
QGtp::influence (QString color)
1099
sprintf (outFile, "%d influence %s\n", _cpt,(const char *)color);
1101
return waitResponse();
1104
/* Function: Return information about the influence function after a move
1107
* Returns: Influence data formatted in the same way as for gtp_influence.
1110
QGtp::moveInfluence (QString color, char c, int i)
1112
sprintf (outFile, "%d move_influence %s %c%d\n", _cpt,(const char *)color,c,i);
1114
return waitResponse();
1117
/* Function: Return the information in the worm data structure.
1120
* Returns: Worm data formatted
1125
sprintf (outFile, "%d worm_data\n", _cpt);
1127
return waitResponse();
1130
/* Function: Return the information in the worm data structure.
1133
* Returns: Worm data formatted
1136
QGtp::wormData (char c, int i)
1138
sprintf (outFile, "%d worm_data %c%d\n", _cpt,c,i);
1140
return waitResponse();
1144
/* Function: Return the cutstone field in the worm data structure.
1145
* Arguments: non-empty vertex
1150
QGtp::wormCutstone (char c, int i)
1152
sprintf (outFile, "%d worm_cutstone %c%d\n", _cpt,c,i);
1154
return waitResponse();
1157
/* Function: Tune the parameters for the move ordering in the tactical
1159
* Arguments: MOVE_ORDERING_PARAMETERS integers
1160
* Fails: incorrect arguments
1164
QGtp::tuneMoveOrdering (int MOVE_ORDERING_PARAMETERS)
1166
sprintf (outFile, "%d tune_move_ordering %d\n", _cpt,MOVE_ORDERING_PARAMETERS);
1168
return waitResponse();
1171
/* Function: Echo the parameter
1177
QGtp::echo (QString param)
1179
sprintf (outFile, "%d echo %s\n", _cpt,(const char *)param);
1181
return waitResponse();
1184
/* Function: List all known commands
1187
* Returns: list of known commands, one per line
1192
sprintf (outFile, "%d help\n", _cpt);
1194
return waitResponse();
1197
/* Function: evaluate wether a command is known
1198
* Arguments: command word
1200
* Returns: true or false
1203
QGtp::knownCommand (QString s)
1205
sprintf (outFile, "%d known_command %s\n", _cpt,(const char *)s);
1207
return waitResponse();
1211
/* Function: Turn uncertainty reports from owl_attack
1212
* and owl_defend on or off.
1213
* Arguments: "on" or "off"
1214
* Fails: invalid argument
1218
QGtp::reportUncertainty (QString s)
1220
sprintf (outFile, "%d report_uncertainty %s\n", _cpt,(const char *)s);
1222
return waitResponse();
1225
/* Function: List the stones of a worm
1226
* Arguments: the location
1227
* Fails: if called on an empty or off-board location
1228
* Returns: list of stones
1233
sprintf (outFile, "%d worm_stones\n", _cpt);
1235
return waitResponse();
1239
QGtp::shell(QString s)
1241
sprintf (outFile, "%d %s\n", _cpt, (const char *)s);
1243
return waitResponse();