| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by The qGo Project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | #include "qgoboardlocalinterface.h" |
| 23 | |
| 24 | #include "qgtp.h" |
| 25 | #include "tree.h" |
| 26 | #include "move.h" |
| 27 | #include "../network/messages.h" |
| 28 | #include "gamedata.h" |
| 29 | #include "boardwindow.h" |
| 30 | #include "qgtp.h" |
| 31 | #include "board.h" |
| 32 | #include "matrix.h" |
| 33 | #include "audio.h" |
| 34 | |
| 35 | #include <QMessageBox> |
| 36 | |
| 37 | qGoBoardLocalInterface::qGoBoardLocalInterface(BoardWindow *bw, Tree * t, GameData *gd) |
| 38 | : qGoBoard(bw, t, gd) |
| 39 | { |
| 40 | this->setObjectName("qGoBoardLocalInterface"); |
| 41 | boardwindow->clearData(); |
| 42 | |
| 43 | // If we have handicap, but not from a loaded file, we have to set the handicap move |
| 44 | if (gameData->handicap && gameData->fileName.isEmpty()) |
| 45 | setHandicap(gameData->handicap); |
| 46 | |
| 47 | QSettings settings; |
| 48 | // value 1 = no sound, 0 all games, 2 my games |
| 49 | playSound = (settings.value("SOUND") != 1); |
| 50 | |
| 51 | // Set up computer interface. |
| 52 | gtp = new QGtp(); |
| 53 | currentEngine = NULL; |
| 54 | |
| 55 | connect (gtp, &QGtp::signal_computerPlayed, this, &qGoBoardLocalInterface::slot_playComputer); |
| 56 | connect (gtp, &QGtp::computerResigned, this, &qGoBoardLocalInterface::slot_resignComputer); |
| 57 | connect (gtp, &QGtp::computerPassed, this, &qGoBoardLocalInterface::slot_passComputer); |
| 58 | |
| 59 | |
| 60 | settings.beginReadArray("ENGINES"); |
| 61 | settings.setArrayIndex(settings.value("DEFAULT_ENGINE").toInt()); |
| 62 | QString path = settings.value("path").toString(); |
| 63 | QString args = settings.value("args").toString(); |
| 64 | settings.endArray(); |
| 65 | if (gtp->openGtpSession(path,args, |
| 66 | gameData->board_size, |
| 67 | gameData->komi, |
| 68 | gameData->handicap)==FAIL) |
| 69 | { |
| 70 | QMessageBox::warning(0,QObject::tr("Error"), |
| 71 | QObject::tr("Error opening program: %1").arg(gtp->getLastMessage())); |
| 72 | delete gtp; |
| 73 | gtp = NULL; |
| 74 | } |
| 75 | |
| 76 | tree->setCurrent(tree->findLastMoveInMainBranch()); |
| 77 | |
| 78 | insertStoneFlag = false; |
| 79 | feedPositionThroughGtp(); |
| 80 | } |
| 81 | |
| 82 | qGoBoardLocalInterface::~qGoBoardLocalInterface() |
| 83 | { |
| 84 | qDebug("Deconstructing computer interface"); |
| 85 | delete gtp; |
| 86 | } |
| 87 | |
| 88 | void qGoBoardLocalInterface::localMoveRequest(StoneColor c, int x, int y) |
| 89 | { |
| 90 | if (insertStoneFlag) |
| 91 | { |
| 92 | tree->getCurrent()->getMatrix()->insertStone(x,y,c,true); |
| 93 | return; |
| 94 | } |
| 95 | bool engineUpToDate = (tree->getCurrent() == currentEngine); |
| 96 | Move *result = tree->getCurrent()->hasSon(c,x,y); |
| 97 | if (!result) |
| 98 | result = tree->getCurrent()->makeMove(c,x,y); |
| 99 | if (!result) |
| 100 | return; |
| 101 | |
| 102 | tree->setCurrent(result); |
| 103 | setModified(true); |
| 104 | boardwindow->updateMove(tree->getCurrent()); |
| 105 | |
| 106 | /* Non trivial here. We don't want to play a sound as we get all |
| 107 | * the moves from an observed game. But there's no clean way |
| 108 | * to tell when the board has stopped loading, particularly for IGS. |
| 109 | * so we only play a sound every 250 msecs... |
| 110 | * Also, maybe it should play even if we aren't looking at last move, yeah not sure on that FIXME */ |
| 111 | if(boardwindow->getGamePhase() == phaseOngoing && playSound) |
| 112 | { |
| 113 | clickSound->play(); |
| 114 | } |
| 115 | |
| 116 | if (engineUpToDate) |
| 117 | { |
| 118 | currentEngine = result; |
| 119 | sendMoveToInterface(c,x,y); |
| 120 | } |
| 121 | else |
| 122 | feedPositionThroughGtp(); |
| 123 | |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | void qGoBoardLocalInterface::slotDonePressed() |
| 129 | { |
| 130 | /* FIXME Since there's no server to tell us the result, we have |
| 131 | * to calculate it or whatever, print out a winner a score, etc.. |
| 132 | * I'm really surprised there's no existing code to do this |
| 133 | * actually. Also feels like the kind of thing that could |
| 134 | * be in qGoBoard, some generic win reporting thing, score |
| 135 | * reporting, etc.. and then the set_result functions could |
| 136 | * call that qGoBoard thing with the specifics.*/ |
| 137 | qGoBoard::slotDonePressed(); |
| 138 | qDebug("Done Pressed\n"); |
| 139 | } |
| 140 | |
| 141 | void qGoBoardLocalInterface::slotUndoPressed() |
| 142 | { |
| 143 | if(boardwindow->getGamePhase() == phaseScore) |
| 144 | leaveScoreMode(); |
| 145 | else |
| 146 | { |
| 147 | if (gtp) |
| 148 | gtp->undo(0); |
| 149 | qGoBoard::slotUndoPressed(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * This sends the move to the computer through GTP |
| 155 | */ |
| 156 | void qGoBoardLocalInterface::sendMoveToInterface(StoneColor c,int x, int y) |
| 157 | { |
| 158 | if (!gtp) |
| 159 | return; |
| 160 | switch (c) |
| 161 | { |
| 162 | case stoneWhite : |
| 163 | if (gtp->playwhite(x ,y)) |
| 164 | { |
| 165 | QMessageBox::warning(boardwindow, PACKAGE, tr("Failed to play the stone within program \n") + gtp->getLastMessage()); |
| 166 | return; |
| 167 | } |
| 168 | break; |
| 169 | |
| 170 | case stoneBlack : |
| 171 | if (gtp->playblack(x ,y)) |
| 172 | { |
| 173 | QMessageBox::warning(boardwindow, PACKAGE, tr("Failed to play the stone within program \n") + gtp->getLastMessage()); |
| 174 | return; |
| 175 | } |
| 176 | break; |
| 177 | |
| 178 | default : |
| 179 | ; |
| 180 | } |
| 181 | |
| 182 | // Check whether the computer should reply |
| 183 | checkComputersTurn(); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * This slot is triggered by the signal emitted by 'gtp' when getting a move |
| 188 | */ |
| 189 | void qGoBoardLocalInterface::slot_playComputer(int x, int y) |
| 190 | { |
| 191 | bool atCurrent = (tree->getCurrent() == currentEngine); |
| 192 | Move *result = currentEngine->makeMove(currentEngine->whoIsOnTurn(), x, y); |
| 193 | if (!result) |
| 194 | { |
| 195 | QMessageBox::warning(boardwindow, tr("Invalid Move"), tr("The incoming move (%1, %2) seems to be invalid").arg(x).arg(y)); |
| 196 | return; |
| 197 | } |
| 198 | currentEngine = result; |
| 199 | |
| 200 | if (atCurrent) |
| 201 | tree->setCurrent(result); |
| 202 | checkComputersTurn(); |
| 203 | } |
| 204 | |
| 205 | void qGoBoardLocalInterface::slot_resignComputer() |
| 206 | { |
| 207 | // This hack should be removed by making doMove() and related functions relative to a move |
| 208 | Move *remember = tree->getCurrent(); |
| 209 | bool atCurrent = (remember == currentEngine); |
| 210 | tree->setCurrent(currentEngine); |
| 211 | |
| 212 | bool b = getBlackTurn(); |
| 213 | GameResult g((!b ? stoneBlack : stoneWhite), GameResult::RESIGN); |
| 214 | setResult(g); |
| 215 | currentEngine = NULL; |
| 216 | |
| 217 | if (!atCurrent) |
| 218 | tree->setCurrent(remember); |
| 219 | } |
| 220 | |
| 221 | void qGoBoardLocalInterface::slot_passComputer() |
| 222 | { |
| 223 | bool atCurrent = (tree->getCurrent() == currentEngine); |
| 224 | Move *result = currentEngine->makePass(); |
| 225 | if (!result) |
| 226 | return; |
| 227 | currentEngine = result; |
| 228 | |
| 229 | if (result->parent->isPassMove()) |
| 230 | { |
| 231 | tree->setCurrent(result); |
| 232 | enterScoreMode(); |
| 233 | } else { |
| 234 | if (atCurrent) |
| 235 | tree->setCurrent(result); |
| 236 | checkComputersTurn(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * sends a "pass" move to GTP |
| 242 | */ |
| 243 | void qGoBoardLocalInterface::passRequest() |
| 244 | { |
| 245 | if (getBlackTurn() && boardwindow->getMyColorIsBlack()) |
| 246 | { |
| 247 | doPass(); |
| 248 | if (gtp) |
| 249 | gtp->playblackPass(); |
| 250 | } else if (!getBlackTurn() && boardwindow->getMyColorIsWhite()) |
| 251 | { |
| 252 | doPass(); |
| 253 | if (gtp) |
| 254 | gtp->playwhitePass(); |
| 255 | } else |
| 256 | return; // It is not our move |
| 257 | |
| 258 | currentEngine = tree->getCurrent(); |
| 259 | if (tree->getCurrent()->parent->isPassMove()) |
| 260 | enterScoreMode(); |
| 261 | else |
| 262 | checkComputersTurn(); |
| 263 | } |
| 264 | |
| 265 | void qGoBoardLocalInterface::feedPositionThroughGtp() |
| 266 | { |
| 267 | if (!gtp) |
| 268 | return; |
| 269 | gtp->clearBoard(); |
| 270 | |
| 271 | QStack<Move*> stack; |
| 272 | Move *m = tree->getCurrent(); |
| 273 | currentEngine = m; |
| 274 | while (m->parent != NULL) |
| 275 | { |
| 276 | stack.push(m); |
| 277 | m = m->parent; |
| 278 | } |
| 279 | // Do not push the root move. |
| 280 | // It only holds the handicap (if any). |
| 281 | if (tree->getRoot()->isHandicapMove()) |
| 282 | { |
| 283 | Matrix * m = tree->getRoot()->getMatrix(); |
| 284 | int size = m->getSize(); |
| 285 | QList<Point> handicap_stones; |
| 286 | for (int x=1; x<=size; x++) |
| 287 | for (int y=1; y<=size; y++) |
| 288 | { |
| 289 | if (m->getStoneAt(x,y) == stoneBlack) |
| 290 | handicap_stones.append(Point(x,y)); |
| 291 | } |
| 292 | gtp->set_free_handicap(handicap_stones); |
| 293 | } |
| 294 | |
| 295 | while (!stack.isEmpty()) |
| 296 | { |
| 297 | m = stack.pop(); |
| 298 | if (m->isPassMove()) |
| 299 | { |
| 300 | if (m->getColor() == stoneBlack) |
| 301 | gtp->playblackPass(); |
| 302 | else if (m->getColor() == stoneWhite) |
| 303 | gtp->playwhitePass(); |
| 304 | } else { |
| 305 | if (m->getColor() == stoneWhite) |
| 306 | gtp->playwhite(m->getX(), m->getY()); |
| 307 | else if (m->getColor() == stoneBlack) |
| 308 | gtp->playblack(m->getX(), m->getY()); |
| 309 | } |
| 310 | } |
| 311 | // Request a move if the computer is on turn |
| 312 | checkComputersTurn(); |
| 313 | } |
| 314 | |
| 315 | void qGoBoardLocalInterface::checkComputersTurn(bool force) |
| 316 | { |
| 317 | if ((!gtp) || (gtp->isBusy()) || (!currentEngine)) |
| 318 | return; |
| 319 | |
| 320 | StoneColor toPlay = currentEngine->whoIsOnTurn(); |
| 321 | int result = OK; |
| 322 | if ((toPlay == stoneBlack) && (force || !(boardwindow->getMyColorIsBlack()))) |
| 323 | result= gtp->genmoveBlack(); |
| 324 | else if ((toPlay == stoneWhite) && (force || !(boardwindow->getMyColorIsWhite()))) |
| 325 | result = gtp->genmoveWhite(); |
| 326 | |
| 327 | if (result == FAIL) |
| 328 | QMessageBox::warning(boardwindow, PACKAGE, tr("Move request from engine failed\n") + gtp->getLastMessage()); |
| 329 | } |
| 330 | |
| 331 | void qGoBoardLocalInterface::slotToggleInsertStones(bool val) |
| 332 | { |
| 333 | insertStoneFlag = val; |
| 334 | } |