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