~ubuntu-branches/ubuntu/maverick/qgo/maverick

« back to all changes in this revision

Viewing changes to src/qgtp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin A. Godisch
  • Date: 2005-01-01 23:07:10 UTC
  • Revision ID: james.westby@ubuntu.com-20050101230710-fhng6yidm47xlb2i
Tags: upstream-1.0.0-r2
ImportĀ upstreamĀ versionĀ 1.0.0-r2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
qgtp.cpp  -  description
 
3
-------------------
 
4
begin                : Thu Nov 29 2001
 
5
copyright            : (C) 2001 by PALM Thomas , DINTILHAC Florian, HIVERT Anthony, PIOC Sebastien
 
6
email                : 
 
7
***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
*                                                                         *
 
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.                                   *
 
15
*                                                                         *
 
16
***************************************************************************/
 
17
 
 
18
 
 
19
//#include <unistd.h>
 
20
#include <stdlib.h>
 
21
#include <qprocess.h>
 
22
#include "qgtp.h"
 
23
 
 
24
/* Function:  open a session
 
25
* Arguments: name and path of go program
 
26
* Fails:     never
 
27
* Returns:   nothing
 
28
*/
 
29
QGtp::QGtp()
 
30
{
 
31
        /*openGtpSession(filename);*/
 
32
        programProcess = NULL; // This to permit proper ending
 
33
}
 
34
 
 
35
 
 
36
QGtp::~QGtp()
 
37
{
 
38
        if (programProcess)
 
39
                programProcess->kill();     // not very clean (should use tryTerminate)
 
40
}
 
41
 
 
42
/* Code */
 
43
 
 
44
/* Function:  get the last message from gnugo
 
45
* Arguments: none
 
46
* Fails:     never
 
47
* Returns:   last message from GO
 
48
*/
 
49
 
 
50
 
 
51
QString QGtp::getLastMessage()
 
52
{
 
53
        return _response;
 
54
}
 
55
 
 
56
int QGtp::openGtpSession(QString filename, int size, float komi, int handicap, int level)
 
57
{
 
58
        _cpt = 1;
 
59
        
 
60
    programProcess = new QProcess();
 
61
    
 
62
    programProcess->clearArguments();
 
63
    programProcess->addArgument(filename);
 
64
    programProcess->addArgument("--mode");
 
65
    programProcess->addArgument("gtp");
 
66
    programProcess->addArgument("--quiet");  
 
67
        
 
68
    connect(programProcess, SIGNAL(readyReadStdout()),
 
69
                this, SLOT(slot_readFromStdout()) );
 
70
    connect(programProcess, SIGNAL(processExited()),
 
71
                this, SLOT(slot_processExited()) );
 
72
        
 
73
        if (!programProcess->start())
 
74
    {
 
75
                  _response="Could not start "+filename;
 
76
                  return FAIL ;
 
77
    }
 
78
        
 
79
    //?? never know ... otherwise, I get a segfault with sprint ...
 
80
    if ((outFile = (char *)malloc (100)) == NULL)
 
81
    {
 
82
                  _response="Yuck ! Could not allocate 100 bytes !!!"  ;
 
83
                  return FAIL ;
 
84
    }   
 
85
        
 
86
        if (protocolVersion()==OK)
 
87
        {
 
88
                if(getLastMessage().toInt() !=2)
 
89
                {
 
90
                        qDebug("Protocol version problem???");
 
91
                        //                              quit();
 
92
                        _response="Protocol version not supported";
 
93
                        //                              return FAIL;
 
94
                }
 
95
                if(setBoardsize(size)==FAIL)
 
96
                {
 
97
                        return FAIL;
 
98
                }
 
99
                if(clearBoard()==FAIL)
 
100
                {
 
101
                        // removed by frosla -> protocol changes...
 
102
                        // return FAIL;
 
103
                }
 
104
 
 
105
 
 
106
                if(knownCommand("level")==FAIL)
 
107
        {
 
108
                          return FAIL;
 
109
                  }
 
110
    else if (getLastMessage().contains("true"))
 
111
        {
 
112
          if (setLevel(level)==FAIL)
 
113
                  {
 
114
                                return FAIL;
 
115
                        }
 
116
        }
 
117
 
 
118
      
 
119
                if(setKomi(komi)==FAIL)
 
120
                {
 
121
                        return FAIL;
 
122
                }
 
123
                if(fixedHandicap(handicap)==FAIL)
 
124
                {
 
125
                        return FAIL;
 
126
                }
 
127
        }
 
128
        else
 
129
        {
 
130
                quit();
 
131
                _response="Protocol version error";
 
132
                return FAIL;
 
133
        }
 
134
        //}
 
135
        return OK;
 
136
}
 
137
 
 
138
// Read from stdout
 
139
void QGtp::slot_readFromStdout()
 
140
{
 
141
        QString buff;
 
142
        
 
143
        while (programProcess->canReadLineStdout())
 
144
        {
 
145
                buff=programProcess->readLineStdout();
 
146
                buff=buff.stripWhiteSpace();
 
147
                if (buff.length() != 0)
 
148
                {
 
149
                        _response = buff;
 
150
//                      qDebug(QString("** QGtp::slot_readFromStdout(): [%1]").arg(_response));
 
151
                }
 
152
        }
 
153
}
 
154
 
 
155
// exit
 
156
void QGtp::slot_processExited()
 
157
{
 
158
        qDebug(QString("%1 quit").arg(_cpt));
 
159
        sprintf (outFile, "%d quit\n", _cpt);
 
160
        fflush(outFile);
 
161
        //      return waitResponse();
 
162
}
 
163
 
 
164
 
 
165
/* Function:  wait for a go response
 
166
* Arguments: none
 
167
* Fails:     never
 
168
* Returns:   nothing
 
169
*/
 
170
int
 
171
QGtp::waitResponse()
 
172
{
 
173
        QString buff = _response;
 
174
        //      QTextStream * inFile;
 
175
        //      char symbole;
 
176
        int number;
 
177
        int pos;
 
178
        
 
179
        do
 
180
        {
 
181
                qApp->processEvents();
 
182
        } while (_response.length() == 0 || _response == buff);
 
183
        
 
184
        /*
 
185
        inFile=new QTextStream(programProcess->readStdout(),IO_ReadOnly);
 
186
        do
 
187
        {
 
188
        * inFile>>symbole;
 
189
        * inFile>>number;
 
190
        buff=inFile->readLine();
 
191
        } while(number !=_cpt);
 
192
        */
 
193
        
 
194
        //      _response=buff.stripWhiteSpace();
 
195
        qDebug(QString("** QGtp::waitResponse(): [%1]").arg(_response));
 
196
        /*      
 
197
        buff=programProcess->readLineStdout();
 
198
        while(!buff.isEmpty())
 
199
        {
 
200
        _response=_response+"\n"+buff;
 
201
        buff=programProcess->readLineStdout();
 
202
        }
 
203
        */
 
204
 
 
205
        buff = _response[0];
 
206
        if ((pos = _response.find(' ')) < 1)
 
207
                pos = 1;
 
208
        number = _response.mid(1,pos).toInt();
 
209
        _response = _response.right(_response.length() - pos - 1);
 
210
 
 
211
        if (buff == '?') //symbole=='?')
 
212
        {
 
213
                return FAIL;
 
214
        }
 
215
        else
 
216
        {
 
217
                return OK;
 
218
        }
 
219
}
 
220
 
 
221
/****************************
 
222
* Program identity.        *
 
223
****************************/
 
224
 
 
225
/* Function:  Report the name of the program.
 
226
* Arguments: none
 
227
* Fails:     never
 
228
* Returns:   program name
 
229
*/
 
230
int
 
231
QGtp::name ()
 
232
{
 
233
        qDebug(QString("%1 name").arg(_cpt));
 
234
        sprintf (outFile, "%d name\n", _cpt);
 
235
        fflush(outFile);
 
236
        return waitResponse();
 
237
}
 
238
 
 
239
/* Function:  Report protocol version.
 
240
* Arguments: none
 
241
* Fails:     never
 
242
* Returns:   protocol version number
 
243
*/
 
244
int
 
245
QGtp::protocolVersion ()
 
246
{
 
247
        qDebug(QString("%1 protocol_version").arg(_cpt));
 
248
        sprintf (outFile, "%d protocol_version\n", _cpt);
 
249
        fflush(outFile);
 
250
        return waitResponse();
 
251
}
 
252
 
 
253
void QGtp::fflush(char * s)
 
254
{
 
255
/*
 
256
int msglen = strlen(s);
 
257
 
 
258
  QByteArray dat(msglen);
 
259
  for (int j = 0; j < msglen; j++) {
 
260
  dat[j] = s[j];
 
261
  }
 
262
  programProcess->writeToStdin(dat);
 
263
        */
 
264
        _cpt++;
 
265
 
 
266
        qDebug(QString("flush -> %1").arg(s));
 
267
    programProcess->writeToStdin(QString::QString(s));
 
268
        
 
269
        
 
270
}
 
271
 
 
272
/****************************
 
273
* Administrative commands. *
 
274
****************************/
 
275
 
 
276
/* Function:  Quit
 
277
* Arguments: none
 
278
* Fails:     never
 
279
* Returns:   nothing
 
280
*/
 
281
int
 
282
QGtp::quit ()
 
283
{
 
284
        sprintf (outFile, "%d quit\n", _cpt);
 
285
        fflush(outFile);
 
286
        return waitResponse();
 
287
}
 
288
 
 
289
/* Function:  Report the version number of the program.
 
290
* Arguments: none
 
291
* Fails:     never
 
292
* Returns:   version number
 
293
*/
 
294
int
 
295
QGtp::version ()
 
296
{
 
297
        sprintf (outFile, "%d version\n", _cpt);
 
298
        fflush(outFile);
 
299
        return waitResponse();
 
300
}
 
301
 
 
302
 
 
303
/***************************
 
304
* Setting the board size. *
 
305
***************************/
 
306
 
 
307
/* Function:  Set the board size to NxN.
 
308
* Arguments: integer
 
309
* Fails:     board size outside engine's limits
 
310
* Returns:   nothing
 
311
*/
 
312
int
 
313
QGtp::setBoardsize (int size)
 
314
{
 
315
        sprintf (outFile, "%d boardsize %d\n", _cpt, size);
 
316
        fflush(outFile);
 
317
        return waitResponse();
 
318
}
 
319
 
 
320
 
 
321
/* Function:  Find the current boardsize
 
322
* Arguments: none
 
323
* Fails:     never
 
324
* Returns:   board_size
 
325
*/
 
326
int
 
327
QGtp::queryBoardsize()
 
328
{
 
329
        sprintf (outFile, "%d query_boardsize\n", _cpt);
 
330
        fflush(outFile);
 
331
        return waitResponse();
 
332
}
 
333
 
 
334
/***********************
 
335
* Clearing the board. *
 
336
***********************/
 
337
 
 
338
/* Function:  Clear the board.
 
339
* Arguments: none
 
340
* Fails:     never
 
341
* Returns:   nothing
 
342
*/
 
343
 
 
344
int
 
345
QGtp::clearBoard ()
 
346
{
 
347
    sprintf (outFile, "%d clear_board\n", _cpt);
 
348
    fflush(outFile);
 
349
    return waitResponse();
 
350
}
 
351
 
 
352
/***************************
 
353
* Setting.           *
 
354
***************************/
 
355
 
 
356
/* Function:  Set the komi.
 
357
* Arguments: float
 
358
* Fails:     incorrect argument
 
359
* Returns:   nothing
 
360
*/
 
361
int
 
362
QGtp::setKomi(float f)
 
363
{
 
364
        sprintf (outFile, "%d komi %.2f\n", _cpt,f);
 
365
        fflush(outFile);
 
366
        return waitResponse();
 
367
}
 
368
 
 
369
/* Function:  Set the playing level.
 
370
* Arguments: int
 
371
* Fails:     incorrect argument
 
372
* Returns:   nothing
 
373
*/
 
374
int
 
375
QGtp::setLevel (int level)
 
376
{
 
377
        sprintf (outFile, "%d level %d\n", _cpt,level);
 
378
        fflush(outFile);
 
379
        return waitResponse();
 
380
}
 
381
/******************
 
382
* Playing moves. *
 
383
******************/
 
384
 
 
385
/* Function:  Play a black stone at the given vertex.
 
386
* Arguments: vertex
 
387
* Fails:     invalid vertex, illegal move
 
388
* Returns:   nothing
 
389
*/
 
390
int
 
391
QGtp::playblack (char c , int i)
 
392
{
 
393
        //  sprintf (outFile, "%d play black %c%d\n", _cpt,c,i);
 
394
        sprintf (outFile, "%d play black %c%d\n", _cpt,c,i);
 
395
        fflush(outFile);
 
396
        return waitResponse();
 
397
}
 
398
 
 
399
/* Function:  Black pass.
 
400
* Arguments: None
 
401
* Fails:     never
 
402
* Returns:   nothing
 
403
*/
 
404
int
 
405
QGtp::playblackPass ()
 
406
{
 
407
        //  sprintf (outFile, "%d play black pass\n", _cpt);
 
408
        sprintf (outFile, "%d play black pass\n", _cpt);
 
409
        fflush(outFile);
 
410
        return waitResponse();
 
411
}
 
412
 
 
413
/* Function:  Play a white stone at the given vertex.
 
414
* Arguments: vertex
 
415
* Fails:     invalid vertex, illegal move
 
416
* Returns:   nothing
 
417
*/
 
418
int
 
419
QGtp::playwhite (char c, int i)
 
420
{
 
421
        //  sprintf (outFile, "%d play white %c%d\n", _cpt,c,i);
 
422
        sprintf (outFile, "%d play white %c%d\n", _cpt,c,i);
 
423
        fflush(outFile);
 
424
        return waitResponse();
 
425
}
 
426
 
 
427
/* Function:  White pass.
 
428
* Arguments: none
 
429
* Fails:     never
 
430
* Returns:   nothing
 
431
*/
 
432
int
 
433
QGtp::playwhitePass ()
 
434
{
 
435
        //  sprintf (outFile, "%d play white pass\n", _cpt);
 
436
        sprintf (outFile, "%d play white pass\n", _cpt);
 
437
        fflush(outFile);
 
438
        return waitResponse();
 
439
}
 
440
 
 
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
 
445
*/
 
446
int
 
447
QGtp::fixedHandicap (int handicap)
 
448
{
 
449
        if (handicap < 2) 
 
450
                return OK;
 
451
        
 
452
        sprintf (outFile, "%d fixed_handicap %d\n", _cpt,handicap);
 
453
        fflush(outFile);
 
454
        return waitResponse();
 
455
}
 
456
 
 
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
 
462
*/
 
463
int
 
464
QGtp::loadsgf (QString filename,int movNumber,char c,int i)
 
465
{
 
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);
 
468
        fflush(outFile);
 
469
        return waitResponse();
 
470
}
 
471
 
 
472
/*****************
 
473
* Board status. *
 
474
*****************/
 
475
 
 
476
/* Function:  Return the color at a vertex.
 
477
* Arguments: vertex
 
478
* Fails:     invalid vertex
 
479
* Returns:   "black", "white", or "empty"
 
480
*/
 
481
int
 
482
QGtp::whatColor (char c, int i)
 
483
{
 
484
        sprintf (outFile, "%d color %c%d\n", _cpt,c,i);
 
485
        fflush(outFile);
 
486
        return waitResponse();
 
487
}
 
488
 
 
489
/* Function:  Count number of liberties for the string at a vertex.
 
490
* Arguments: vertex
 
491
* Fails:     invalid vertex, empty vertex
 
492
* Returns:   Number of liberties.
 
493
*/
 
494
int
 
495
QGtp::countlib (char c, int i)
 
496
{
 
497
        sprintf (outFile, "%d countlib %c%d\n", _cpt,c,i);
 
498
        fflush(outFile);
 
499
        return waitResponse();
 
500
}
 
501
 
 
502
/* Function:  Return the positions of the liberties for the string at a vertex.
 
503
* Arguments: vertex
 
504
* Fails:     invalid vertex, empty vertex
 
505
* Returns:   Sorted space separated list of vertices.
 
506
*/
 
507
int
 
508
QGtp::findlib (char c, int i)
 
509
{
 
510
        sprintf (outFile, "%d findlib %c%d\n", _cpt,c,i);
 
511
        fflush(outFile);
 
512
        return waitResponse();
 
513
}
 
514
 
 
515
/* Function:  Tell whether a move is legal.
 
516
* Arguments: move
 
517
* Fails:     invalid move
 
518
* Returns:   1 if the move is legal, 0 if it is not.
 
519
*/
 
520
int
 
521
QGtp::isLegal (QString color, char c, int i)
 
522
{
 
523
        sprintf (outFile, "%d is_legal %s %c%d\n", _cpt,(const char *)color,c,i);
 
524
        fflush(outFile);
 
525
        return waitResponse();
 
526
}
 
527
 
 
528
/* Function:  List all legal moves for either color.
 
529
* Arguments: color
 
530
* Fails:     invalid color
 
531
* Returns:   Sorted space separated list of vertices.
 
532
*/
 
533
int
 
534
QGtp::allLegal (QString color)
 
535
{
 
536
        sprintf (outFile, "%d all_legal %s\n", _cpt,(const char *)color);
 
537
        fflush(outFile);
 
538
        return waitResponse();
 
539
}
 
540
 
 
541
/* Function:  List the number of captures taken by either color.
 
542
* Arguments: color
 
543
* Fails:     invalid color
 
544
* Returns:   Number of captures.
 
545
*/
 
546
int
 
547
QGtp::captures (QString color)
 
548
{
 
549
        sprintf (outFile, "%d captures %s\n", _cpt,(const char *)color);
 
550
        fflush(outFile);
 
551
        return waitResponse();
 
552
}
 
553
 
 
554
/**********************
 
555
* Retractable moves. *
 
556
**********************/
 
557
 
 
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
 
561
* Returns:   nothing
 
562
*/
 
563
int
 
564
QGtp::trymove (QString color, char c, int i)
 
565
{
 
566
        sprintf (outFile, "%d trymove %s %c%d\n", _cpt,(const char *)color,c,i);
 
567
        fflush(outFile);
 
568
        return waitResponse();
 
569
}
 
570
 
 
571
/* Function:  Undo a trymove.
 
572
* Arguments: none
 
573
* Fails:     stack empty
 
574
* Returns:   nothing
 
575
*/
 
576
int
 
577
QGtp::popgo ()
 
578
{
 
579
        sprintf (outFile, "%d popgo\n", _cpt);
 
580
        fflush(outFile);
 
581
        return waitResponse();
 
582
}
 
583
 
 
584
/*********************
 
585
* Tactical reading. *
 
586
*********************/
 
587
 
 
588
/* Function:  Try to attack a string.
 
589
* Arguments: vertex
 
590
* Fails:     invalid vertex, empty vertex
 
591
* Returns:   attack code followed by attack point if attack code nonzero.
 
592
*/
 
593
int
 
594
QGtp::attack (char c, int i)
 
595
{
 
596
        sprintf (outFile, "%d attack %c%d\n", _cpt,c,i);
 
597
        fflush(outFile);
 
598
        return waitResponse();
 
599
}
 
600
 
 
601
/* Function:  Try to defend a string.
 
602
* Arguments: vertex
 
603
* Fails:     invalid vertex, empty vertex
 
604
* Returns:   defense code followed by defense point if defense code nonzero.
 
605
*/
 
606
int
 
607
QGtp::defend (char c, int i)
 
608
{
 
609
        sprintf (outFile, "%d defend %c%d\n", _cpt,c,i);
 
610
        fflush(outFile);
 
611
        return waitResponse();
 
612
}
 
613
 
 
614
/* Function:  Increase depth values by one.
 
615
* Arguments: none
 
616
* Fails:     never
 
617
* Returns:   nothing
 
618
*/
 
619
int
 
620
QGtp::increaseDepths ()
 
621
{
 
622
        sprintf (outFile, "%d increase_depths\n", _cpt);
 
623
        fflush(outFile);
 
624
        return waitResponse();
 
625
}
 
626
 
 
627
/* Function:  Decrease depth values by one.
 
628
* Arguments: none
 
629
* Fails:     never
 
630
* Returns:   nothing
 
631
*/
 
632
int
 
633
QGtp::decreaseDepths ()
 
634
{
 
635
        sprintf (outFile, "%d decrease_depths\n", _cpt);
 
636
        fflush(outFile);
 
637
        return waitResponse();
 
638
}
 
639
 
 
640
/******************
 
641
* owl reading. *
 
642
******************/
 
643
 
 
644
/* Function:  Try to attack a dragon.
 
645
* Arguments: vertex
 
646
* Fails:     invalid vertex, empty vertex
 
647
* Returns:   attack code followed by attack point if attack code nonzero.
 
648
*/
 
649
int
 
650
QGtp::owlAttack (char c, int i)
 
651
{
 
652
        sprintf (outFile, "%d owl_attack %c%d\n", _cpt,c,i);
 
653
        fflush(outFile);
 
654
        return waitResponse();
 
655
}
 
656
 
 
657
/* Function:  Try to defend a dragon.
 
658
* Arguments: vertex
 
659
* Fails:     invalid vertex, empty vertex
 
660
* Returns:   defense code followed by defense point if defense code nonzero.
 
661
*/
 
662
int
 
663
QGtp::owlDefend (char c, int i)
 
664
{
 
665
        sprintf (outFile, "%d owl_defend %c%d\n", _cpt,c,i);
 
666
        fflush(outFile);
 
667
        return waitResponse();
 
668
}
 
669
 
 
670
/********
 
671
* eyes *
 
672
********/
 
673
 
 
674
/* Function:  Evaluate an eye space
 
675
* Arguments: vertex
 
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.
 
681
*/
 
682
 
 
683
int
 
684
QGtp::evalEye (char c, int i)
 
685
{
 
686
        sprintf (outFile, "%d eval_eye %c%d\n", _cpt,c,i);
 
687
        fflush(outFile);
 
688
        return waitResponse();
 
689
}
 
690
 
 
691
/*****************
 
692
* dragon status *
 
693
*****************/
 
694
 
 
695
/* Function:  Determine status of a dragon.
 
696
* Arguments: vertex
 
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.
 
702
*
 
703
* FIXME: Should be able to distinguish between life in seki
 
704
*        and life with territory. Should also be able to identify ko.
 
705
*/
 
706
 
 
707
int
 
708
QGtp::dragonStatus (char c, int i)
 
709
{
 
710
        sprintf (outFile, "%d dragon_status %c%d\n", _cpt,c,i);
 
711
        fflush(outFile);
 
712
        return waitResponse();
 
713
}
 
714
 
 
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
 
719
*/
 
720
 
 
721
int
 
722
QGtp::sameDragon (char c1, int i1, char c2, int i2)
 
723
{
 
724
        sprintf (outFile, "%d same_dragon %c%d %c%d\n", _cpt,c1,i1,c2,i2);
 
725
        fflush(outFile);
 
726
        return waitResponse();
 
727
}
 
728
 
 
729
/* Function:  Return the information in the dragon data structure.
 
730
* Arguments: nothing
 
731
* Fails:     never
 
732
* Returns:   Dragon data formatted in the corresponding way to gtp_worm__
 
733
*/
 
734
int
 
735
QGtp::dragonData ()
 
736
{
 
737
        sprintf (outFile, "%d dragon_data \n", _cpt);
 
738
        fflush(outFile);
 
739
        return waitResponse();
 
740
}
 
741
 
 
742
 
 
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__
 
747
*/
 
748
int
 
749
QGtp::dragonData (char c,int i)
 
750
{
 
751
        sprintf (outFile, "%d dragon_data %c%d\n", _cpt,c,i);
 
752
        fflush(outFile);
 
753
        return waitResponse();
 
754
}
 
755
 
 
756
 
 
757
/***********************
 
758
* combination attacks *
 
759
***********************/
 
760
 
 
761
/* Function:  Find a move by color capturing something through a
 
762
*            combination attack.
 
763
* Arguments: color
 
764
* Fails:     invalid color
 
765
* Returns:   Recommended move, PASS if no move found
 
766
*/
 
767
 
 
768
int
 
769
QGtp::combinationAttack (QString color)
 
770
{
 
771
        sprintf (outFile, "%d combination_attack %s\n", _cpt,(const char *)color);
 
772
        fflush(outFile);
 
773
        return waitResponse();
 
774
}
 
775
 
 
776
/********************
 
777
* generating moves *
 
778
********************/
 
779
 
 
780
/* Function:  Generate and play the supposedly best black move.
 
781
* Arguments: none
 
782
* Fails:     never
 
783
* Returns:   a move coordinate (or "PASS")
 
784
*/
 
785
int
 
786
QGtp::genmoveBlack ()
 
787
{
 
788
        sprintf (outFile, "%d genmove black\n", _cpt);
 
789
        fflush(outFile);
 
790
        return waitResponse();
 
791
}
 
792
 
 
793
/* Function:  Generate and play the supposedly best white move.
 
794
* Arguments: none
 
795
* Fails:     never
 
796
* Returns:   a move coordinate (or "PASS")
 
797
*/
 
798
int
 
799
QGtp::genmoveWhite ()
 
800
{
 
801
        sprintf (outFile, "%d genmove white\n", _cpt);
 
802
        fflush(outFile);
 
803
        return waitResponse();
 
804
}
 
805
 
 
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")
 
810
*/
 
811
int
 
812
QGtp::genmove (QString color,int seed)
 
813
{
 
814
        sprintf (outFile, "%d gg_genmove %s %d\n", _cpt,(const char *)color,seed);
 
815
        fflush(outFile);
 
816
        return waitResponse();
 
817
}
 
818
 
 
819
/* Function : Generate a list of the best moves for White with weights
 
820
* Arguments: none
 
821
* Fails:   : never
 
822
* Returns  : list of moves with weights
 
823
*/
 
824
 
 
825
int
 
826
QGtp::topMovesWhite ()
 
827
{
 
828
        sprintf (outFile, "%d top_moves_white\n", _cpt);
 
829
        fflush(outFile);
 
830
        return waitResponse();
 
831
}
 
832
 
 
833
/* Function : Generate a list of the best moves for Black with weights
 
834
* Arguments: none
 
835
* Fails:   : never
 
836
* Returns  : list of moves with weights
 
837
*/
 
838
 
 
839
int
 
840
QGtp::topMovesBlack ()
 
841
{
 
842
        sprintf (outFile, "%d top_moves_black\n", _cpt);
 
843
        fflush(outFile);
 
844
        return waitResponse();
 
845
}
 
846
 
 
847
/* Function:  Undo last move
 
848
* Arguments: int
 
849
* Fails:     If move pointer is 0
 
850
* Returns:   nothing
 
851
*/
 
852
int
 
853
QGtp::undo (int i)
 
854
{
 
855
        sprintf (outFile, "%d undo %d\n", _cpt,i);
 
856
        fflush(outFile);
 
857
        return waitResponse();
 
858
}
 
859
 
 
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".
 
865
*/
 
866
int
 
867
QGtp::finalStatus (char c, int i, int seed)     
 
868
{
 
869
        sprintf (outFile, "%d final_status %c%d %d\n", _cpt,c,i,seed);
 
870
        fflush(outFile);
 
871
        return waitResponse();
 
872
}
 
873
 
 
874
 
 
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").
 
883
*/
 
884
int
 
885
QGtp::finalStatusList (QString status, int seed)
 
886
{
 
887
        sprintf (outFile, "%d final_status_list %s %d\n", _cpt,(const char *)status,seed);
 
888
        fflush(outFile);
 
889
        return waitResponse();
 
890
}
 
891
/**************
 
892
* score *
 
893
**************/
 
894
 
 
895
/* Function:  Compute the score of a finished game.
 
896
* Arguments: Optional random seed
 
897
* Fails:     never
 
898
* Returns:   Score in SGF format (RE property).
 
899
*/
 
900
int
 
901
QGtp::finalScore (int seed)
 
902
{
 
903
        sprintf (outFile, "%d final_score %d\n", _cpt,seed);
 
904
        fflush(outFile);
 
905
        return waitResponse();
 
906
}
 
907
 
 
908
int
 
909
QGtp::estimateScore ()
 
910
{
 
911
        sprintf (outFile, "%d estimate_score\n", _cpt);
 
912
        fflush(outFile);
 
913
        return waitResponse();
 
914
}
 
915
 
 
916
int
 
917
QGtp::newScore ()
 
918
{
 
919
        sprintf (outFile, "%d new_score \n", _cpt);
 
920
        fflush(outFile);
 
921
        return waitResponse();
 
922
}
 
923
 
 
924
/**************
 
925
* statistics *
 
926
**************/
 
927
 
 
928
/* Function:  Reset the count of life nodes.
 
929
* Arguments: none
 
930
* Fails:     never
 
931
* Returns:   nothing
 
932
*/
 
933
int
 
934
QGtp::resetLifeNodeCounter ()
 
935
{
 
936
        sprintf (outFile, "%d reset_life_node_counter\n", _cpt);
 
937
        fflush(outFile);
 
938
        return waitResponse();
 
939
}
 
940
 
 
941
/* Function:  Retrieve the count of life nodes.
 
942
* Arguments: none
 
943
* Fails:     never
 
944
* Returns:   number of life nodes
 
945
*/
 
946
int
 
947
QGtp::getLifeNodeCounter ()
 
948
{
 
949
        sprintf (outFile, "%d get_life_node_counter\n", _cpt);
 
950
        fflush(outFile);
 
951
        return waitResponse();
 
952
}
 
953
 
 
954
/* Function:  Reset the count of owl nodes.
 
955
* Arguments: none
 
956
* Fails:     never
 
957
* Returns:   nothing
 
958
*/
 
959
int
 
960
QGtp::resetOwlNodeCounter ()
 
961
{
 
962
        sprintf (outFile, "%d reset_owl_node_counter\n", _cpt);
 
963
        fflush(outFile);
 
964
        return waitResponse();
 
965
}
 
966
 
 
967
/* Function:  Retrieve the count of owl nodes.
 
968
* Arguments: none
 
969
* Fails:     never
 
970
* Returns:   number of owl nodes
 
971
*/
 
972
int
 
973
QGtp::getOwlNodeCounter ()
 
974
{
 
975
        sprintf (outFile, "%d get_owl_node_counter\n", _cpt);
 
976
        fflush(outFile);
 
977
        return waitResponse();
 
978
}
 
979
 
 
980
/* Function:  Reset the count of reading nodes.
 
981
* Arguments: none
 
982
* Fails:     never
 
983
* Returns:   nothing
 
984
*/
 
985
int
 
986
QGtp::resetReadingNodeCounter ()
 
987
{
 
988
        sprintf (outFile, "%d reset_reading_node_counter\n", _cpt);
 
989
        fflush(outFile);
 
990
        return waitResponse();
 
991
}
 
992
 
 
993
 
 
994
/* Function:  Retrieve the count of reading nodes.
 
995
* Arguments: none
 
996
* Fails:     never
 
997
* Returns:   number of reading nodes
 
998
*/
 
999
int
 
1000
QGtp::getReadingNodeCounter ()
 
1001
{
 
1002
        sprintf (outFile, "%d get_reading_node_counter\n", _cpt);
 
1003
        fflush(outFile);
 
1004
        return waitResponse();
 
1005
}
 
1006
 
 
1007
/* Function:  Reset the count of trymoves/trykos.
 
1008
* Arguments: none
 
1009
* Fails:     never
 
1010
* Returns:   nothing
 
1011
*/
 
1012
int
 
1013
QGtp::resetTrymoveCounter ()
 
1014
{
 
1015
        sprintf (outFile, "%d reset_trymove_counter\n", _cpt);
 
1016
        fflush(outFile);
 
1017
        return waitResponse();
 
1018
}
 
1019
 
 
1020
/* Function:  Retrieve the count of trymoves/trykos.
 
1021
* Arguments: none
 
1022
* Fails:     never
 
1023
* Returns:   number of trymoves/trykos
 
1024
*/
 
1025
int
 
1026
QGtp::getTrymoveCounter ()
 
1027
{
 
1028
        sprintf (outFile, "%d get_trymove_counter\n", _cpt);
 
1029
        fflush(outFile);
 
1030
        return waitResponse();
 
1031
}
 
1032
 
 
1033
/*********
 
1034
* debug *
 
1035
*********/
 
1036
 
 
1037
/* Function:  Write the position to stderr.
 
1038
* Arguments: none
 
1039
* Fails:     never
 
1040
* Returns:   nothing
 
1041
*/
 
1042
int
 
1043
QGtp::showboard ()
 
1044
{
 
1045
        sprintf (outFile, "%d showboard\n", _cpt);
 
1046
        fflush(outFile);
 
1047
        return waitResponse();
 
1048
}
 
1049
 
 
1050
 
 
1051
/* Function:  Dump stack to stderr.
 
1052
* Arguments: none
 
1053
* Fails:     never
 
1054
* Returns:   nothing
 
1055
*/
 
1056
int
 
1057
QGtp::dumpStack ()
 
1058
{
 
1059
        sprintf (outFile, "%d dump_stack\n", _cpt);
 
1060
        fflush(outFile);
 
1061
        return waitResponse();
 
1062
}
 
1063
 
 
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
 
1067
* Returns:   nothing
 
1068
*/
 
1069
int
 
1070
QGtp::debugInfluence (QString color,QString list)
 
1071
{
 
1072
        sprintf (outFile, "%d debug_influence %s %s\n", _cpt,(const char *)color,(const char *)list);
 
1073
        fflush(outFile);
 
1074
        return waitResponse();
 
1075
}
 
1076
 
 
1077
/* Function:  Write information about the influence function after making
 
1078
*            a move to stderr.
 
1079
* Arguments: move, optionally a list of what to show
 
1080
* Fails:     never
 
1081
* Returns:   nothing
 
1082
*/
 
1083
int
 
1084
QGtp::debugMoveInfluence (QString color, char c, int i,QString list)
 
1085
{
 
1086
        sprintf (outFile, "%d debug_move_influence %s %c%d %s\n", _cpt,(const char *)color,c,i,(const char *)list);
 
1087
        fflush(outFile);
 
1088
        return waitResponse();
 
1089
}
 
1090
 
 
1091
/* Function:  Return information about the influence function.
 
1092
* Arguments: color to move
 
1093
* Fails:     never
 
1094
* Returns:   Influence data formatted
 
1095
*/
 
1096
int
 
1097
QGtp::influence (QString color)
 
1098
{
 
1099
        sprintf (outFile, "%d influence %s\n", _cpt,(const char *)color);
 
1100
        fflush(outFile);
 
1101
        return waitResponse();
 
1102
}
 
1103
 
 
1104
/* Function:  Return information about the influence function after a move
 
1105
* Arguments: move
 
1106
* Fails:     never
 
1107
* Returns:   Influence data formatted in the same way as for gtp_influence.
 
1108
*/
 
1109
int
 
1110
QGtp::moveInfluence (QString color, char c, int i)
 
1111
{
 
1112
        sprintf (outFile, "%d move_influence %s %c%d\n", _cpt,(const char *)color,c,i);
 
1113
        fflush(outFile);
 
1114
        return waitResponse();
 
1115
}
 
1116
 
 
1117
/* Function:  Return the information in the worm data structure.
 
1118
* Arguments: none
 
1119
* Fails:     never
 
1120
* Returns:   Worm data formatted
 
1121
*/
 
1122
int
 
1123
QGtp::wormData ()
 
1124
{
 
1125
        sprintf (outFile, "%d worm_data\n", _cpt);
 
1126
        fflush(outFile);
 
1127
        return waitResponse();
 
1128
}
 
1129
 
 
1130
/* Function:  Return the information in the worm data structure.
 
1131
* Arguments: vertex
 
1132
* Fails:     never
 
1133
* Returns:   Worm data formatted
 
1134
*/
 
1135
int
 
1136
QGtp::wormData (char c, int i)
 
1137
{
 
1138
        sprintf (outFile, "%d worm_data %c%d\n", _cpt,c,i);
 
1139
        fflush(outFile);
 
1140
        return waitResponse();
 
1141
}
 
1142
 
 
1143
 
 
1144
/* Function:  Return the cutstone field in the worm data structure.
 
1145
* Arguments: non-empty vertex
 
1146
* Fails:     never
 
1147
* Returns:   cutstone
 
1148
*/
 
1149
int
 
1150
QGtp::wormCutstone (char c, int i)
 
1151
{
 
1152
        sprintf (outFile, "%d worm_cutstone %c%d\n", _cpt,c,i);
 
1153
        fflush(outFile);
 
1154
        return waitResponse();
 
1155
}
 
1156
 
 
1157
/* Function:  Tune the parameters for the move ordering in the tactical
 
1158
*            reading.
 
1159
* Arguments: MOVE_ORDERING_PARAMETERS integers
 
1160
* Fails:     incorrect arguments
 
1161
* Returns:   nothing
 
1162
*/
 
1163
int
 
1164
QGtp::tuneMoveOrdering (int MOVE_ORDERING_PARAMETERS)
 
1165
{
 
1166
        sprintf (outFile, "%d tune_move_ordering %d\n", _cpt,MOVE_ORDERING_PARAMETERS);
 
1167
        fflush(outFile);
 
1168
        return waitResponse();
 
1169
}
 
1170
 
 
1171
/* Function:  Echo the parameter
 
1172
* Arguments: string
 
1173
* Fails:     never
 
1174
* Returns:   nothing
 
1175
*/
 
1176
int
 
1177
QGtp::echo (QString param)
 
1178
{
 
1179
        sprintf (outFile, "%d echo %s\n", _cpt,(const char *)param);
 
1180
        fflush(outFile);
 
1181
        return waitResponse();
 
1182
}
 
1183
 
 
1184
/* Function:  List all known commands
 
1185
* Arguments: none
 
1186
* Fails:     never
 
1187
* Returns:   list of known commands, one per line
 
1188
*/
 
1189
int
 
1190
QGtp::help ()
 
1191
{
 
1192
        sprintf (outFile, "%d help\n", _cpt);
 
1193
        fflush(outFile);
 
1194
        return waitResponse();
 
1195
}
 
1196
 
 
1197
/* Function:  evaluate wether a command is known
 
1198
* Arguments: command word
 
1199
* Fails:     never
 
1200
* Returns:   true or false
 
1201
*/
 
1202
int
 
1203
QGtp::knownCommand (QString s)
 
1204
{
 
1205
        sprintf (outFile, "%d known_command %s\n", _cpt,(const char *)s);
 
1206
        fflush(outFile);
 
1207
        return waitResponse();
 
1208
}
 
1209
 
 
1210
 
 
1211
/* Function:  Turn uncertainty reports from owl_attack
 
1212
*            and owl_defend on or off.
 
1213
* Arguments: "on" or "off"
 
1214
* Fails:     invalid argument
 
1215
* Returns:   nothing
 
1216
*/
 
1217
int
 
1218
QGtp::reportUncertainty (QString s)
 
1219
{
 
1220
        sprintf (outFile, "%d report_uncertainty %s\n", _cpt,(const char *)s);
 
1221
        fflush(outFile);
 
1222
        return waitResponse();
 
1223
}
 
1224
 
 
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
 
1229
*/
 
1230
int
 
1231
QGtp::wormStones()
 
1232
{
 
1233
        sprintf (outFile, "%d worm_stones\n", _cpt);
 
1234
        fflush(outFile);
 
1235
        return waitResponse();
 
1236
}
 
1237
 
 
1238
int
 
1239
QGtp::shell(QString s)
 
1240
{
 
1241
        sprintf (outFile, "%d %s\n", _cpt, (const char *)s);
 
1242
        fflush(outFile);
 
1243
        return waitResponse();
 
1244
}
 
1245