| 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 | |
| 23 | #include "move.h" |
| 24 | #include "matrix.h" |
| 25 | #include "group.h" |
| 26 | |
| 27 | Move::Move(int board_size) |
| 28 | { |
| 29 | parent = son = brother = marker = NULL; |
| 30 | stoneColor = stoneNone; |
| 31 | x = y = -1; |
| 32 | gamePhase = phaseOngoing; |
| 33 | moveNum = 0; |
| 34 | terrMarked = false; |
| 35 | capturesBlack = capturesWhite = 0; |
| 36 | checked = true; |
| 37 | //fastLoadMarkDict = NULL; |
| 38 | scored = false; |
| 39 | scoreWhite = scoreBlack = 0; |
| 40 | PLinfo = false; |
| 41 | timeinfo = false; |
| 42 | handicapMove = false; |
| 43 | matrix = new Matrix(board_size); |
| 44 | } |
| 45 | |
| 46 | Move::Move(StoneColor c, int mx, int my, int n, GamePhase phase, const Matrix &mat, bool clearAllMarks, const QString &s) |
| 47 | : stoneColor(c), x(mx), y(my), moveNum(n), gamePhase(phase), comment(s) |
| 48 | { |
| 49 | parent = son = brother = marker = NULL; |
| 50 | capturesBlack = capturesWhite = 0; |
| 51 | terrMarked = false; |
| 52 | checked = true; |
| 53 | scored = false; |
| 54 | scoreWhite = scoreBlack = 0; |
| 55 | PLinfo = false; |
| 56 | timeinfo = false; |
| 57 | handicapMove = false; |
| 58 | matrix = new Matrix(mat, clearAllMarks); |
| 59 | } |
| 60 | |
| 61 | Move::Move(StoneColor c, int mx, int my, int n, GamePhase phase, const QString &s) |
| 62 | : stoneColor(c), x(mx), y(my), moveNum(n), gamePhase(phase), comment(s) |
| 63 | { |
| 64 | parent = son = brother = marker = NULL; |
| 65 | capturesBlack = capturesWhite = 0; |
| 66 | terrMarked = false; |
| 67 | checked = false; |
| 68 | matrix = NULL; |
| 69 | scored = false; |
| 70 | scoreWhite = scoreBlack = 0; |
| 71 | PLinfo = false; |
| 72 | timeinfo = false; |
| 73 | handicapMove = false; |
| 74 | } |
| 75 | |
| 76 | Move::Move(Move *_parent, StoneColor _c, int _x, int _y) |
| 77 | : stoneColor(_c), x(_x), y(_y) |
| 78 | { |
| 79 | son = brother = marker = NULL; |
| 80 | parent = _parent; |
| 81 | moveNum = parent->moveNum+1; |
| 82 | gamePhase = parent->gamePhase; |
| 83 | capturesBlack = parent->capturesBlack; |
| 84 | capturesWhite = parent->capturesWhite; |
| 85 | matrix = new Matrix(*(parent->matrix), true); |
| 86 | terrMarked = false; |
| 87 | checked = false; |
| 88 | scored = false; |
| 89 | scoreWhite = scoreBlack = 0; |
| 90 | PLinfo = false; |
| 91 | timeinfo = false; |
| 92 | handicapMove = false; |
| 93 | // current node has no son? |
| 94 | if (parent->son == NULL) |
| 95 | { |
| 96 | parent->son = this; |
| 97 | } |
| 98 | // A son found. Add the new node as farest right brother of that son |
| 99 | else |
| 100 | { |
| 101 | parent->son->addBrother(this); |
| 102 | } |
| 103 | |
| 104 | // Execute move |
| 105 | if (x != PASS_XY || y != PASS_XY) |
| 106 | { |
| 107 | int lastCaptures = matrix->makeMove(x, y, stoneColor); |
| 108 | |
| 109 | if (stoneColor == stoneBlack) |
| 110 | capturesBlack += lastCaptures; |
| 111 | else if (stoneColor == stoneWhite) |
| 112 | capturesWhite += lastCaptures; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | Move::~Move() |
| 117 | { |
| 118 | while (son) |
| 119 | delete son; |
| 120 | if (parent) |
| 121 | { |
| 122 | if (parent->marker == this) |
| 123 | parent->marker = NULL; |
| 124 | Move * tmp = getPrevBrother(); |
| 125 | if (tmp) |
| 126 | tmp->brother = brother; |
| 127 | else |
| 128 | parent->son = brother; |
| 129 | } |
| 130 | delete matrix; |
| 131 | } |
| 132 | |
| 133 | Move *Move::getLastMove(bool followMarker) |
| 134 | { |
| 135 | Move * tmp = this; |
| 136 | // Go down the current branch, use the marker if possible to remember a previously used path |
| 137 | while (tmp->son) |
| 138 | tmp = ( (followMarker && (tmp->marker != NULL)) ? tmp->marker : tmp->son); |
| 139 | return tmp; |
| 140 | } |
| 141 | |
| 142 | Move *Move::getPrevBrother() |
| 143 | { |
| 144 | if (parent == NULL || parent->son == this) |
| 145 | return NULL; |
| 146 | Move * tmp = parent->son; |
| 147 | while (tmp->brother != this) |
| 148 | tmp = tmp->brother; |
| 149 | return tmp; |
| 150 | } |
| 151 | |
| 152 | bool Move::isInMainBranch() const |
| 153 | { |
| 154 | const Move * m = this; |
| 155 | while(m->parent && m->parent->son == m) |
| 156 | m = m->parent; |
| 157 | return (m->parent == NULL); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * returns a string representing the move in SGF format |
| 162 | */ |
| 163 | const QString Move::saveMove(bool isRoot) |
| 164 | { |
| 165 | QString str; |
| 166 | |
| 167 | if (!isRoot && !handicapMove ) |
| 168 | str += ";"; //"\n;"; |
| 169 | |
| 170 | if (x != -1 && y != -1) |
| 171 | { |
| 172 | if(gamePhase == phaseEdit && matrix->getStoneAt(x,y) == stoneErase) {} |
| 173 | else |
| 174 | { |
| 175 | // Write something like 'B[aa]' |
| 176 | str += stoneColor == stoneBlack ? "B" : "W"; |
| 177 | str += "[" + Matrix::coordsToString(x-1, y-1) + "]"; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Save edited moves (including handicap) |
| 182 | str += matrix->saveEditedMoves(parent != NULL ? parent->getMatrix() : 0); |
| 183 | |
| 184 | // Save marks |
| 185 | str += matrix->saveMarks(); |
| 186 | |
| 187 | // Add nodename, if we have one |
| 188 | if (!nodeName.isNull() && !nodeName.isEmpty()) |
| 189 | { |
| 190 | // simpletext |
| 191 | str += "N["; |
| 192 | str += nodeName; |
| 193 | str += "]"; |
| 194 | } |
| 195 | |
| 196 | // Add next move's color |
| 197 | if (PLinfo) |
| 198 | { |
| 199 | if (PLnextMove == stoneBlack) |
| 200 | str += "PL[B]"; |
| 201 | else |
| 202 | str += "PL[W]"; |
| 203 | } |
| 204 | |
| 205 | // Add comment, if we have one |
| 206 | if (!comment.isNull() && !comment.isEmpty()) |
| 207 | { |
| 208 | // text |
| 209 | QString tmp = comment; |
| 210 | int pos = 0; |
| 211 | while (( (pos = tmp.indexOf("]", pos)) != -1) && (static_cast<signed int>(pos) < tmp.length())) |
| 212 | { |
| 213 | tmp.replace(pos, 1, "\\]"); |
| 214 | pos += 2; |
| 215 | } |
| 216 | str += "C["; |
| 217 | str += tmp; |
| 218 | str += "]"; |
| 219 | } |
| 220 | |
| 221 | // time info |
| 222 | if (timeinfo && !isRoot && (int) timeLeft) |
| 223 | { |
| 224 | if (stoneColor == stoneBlack) |
| 225 | str += "BL["; |
| 226 | else |
| 227 | str += "WL["; |
| 228 | str += QString::number(timeLeft); |
| 229 | str += "]"; |
| 230 | |
| 231 | // open moves info |
| 232 | if (openMoves > 0) |
| 233 | { |
| 234 | if (stoneColor == stoneBlack) |
| 235 | str += "OB["; |
| 236 | else |
| 237 | str += "OW["; |
| 238 | str += QString::number(openMoves); |
| 239 | str += "]"; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // Add unknown properties, if we have some |
| 244 | if (!unknownProperty.isNull() && !unknownProperty.isEmpty()) |
| 245 | { |
| 246 | // complete property |
| 247 | str += unknownProperty; |
| 248 | } |
| 249 | |
| 250 | return str; |
| 251 | } |
| 252 | |
| 253 | bool Move::isPassMove() |
| 254 | { |
| 255 | if ((x==PASS_XY) && (y==PASS_XY)) |
| 256 | return true; |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | int Move::getNumBrothers() |
| 262 | { |
| 263 | if (parent ==NULL) |
| 264 | return 0; |
| 265 | |
| 266 | Move *tmp = parent->son; |
| 267 | int counter = 0; |
| 268 | |
| 269 | while ((tmp = tmp->brother) != NULL) |
| 270 | counter ++; |
| 271 | |
| 272 | return counter; |
| 273 | } |
| 274 | |
| 275 | int Move::getNumSons() |
| 276 | { |
| 277 | Move *tmp = son; |
| 278 | int counter = 0; |
| 279 | |
| 280 | while (tmp) |
| 281 | { |
| 282 | tmp = tmp->brother; |
| 283 | counter ++; |
| 284 | } |
| 285 | |
| 286 | return counter; |
| 287 | } |
| 288 | |
| 289 | bool Move::hasParent() |
| 290 | { |
| 291 | return (parent != NULL); |
| 292 | } |
| 293 | |
| 294 | bool Move::hasPrevBrother() |
| 295 | { |
| 296 | if (parent == NULL) |
| 297 | return false; |
| 298 | else |
| 299 | return (this != parent->son); |
| 300 | } |
| 301 | |
| 302 | bool Move::hasNextBrother() |
| 303 | { |
| 304 | return (brother != NULL); |
| 305 | } |
| 306 | |
| 307 | void Move::addBrother(Move * b) |
| 308 | { |
| 309 | Move *tmp = this; |
| 310 | |
| 311 | // Find brother farest right |
| 312 | while (tmp->brother != NULL) |
| 313 | tmp = tmp->brother; |
| 314 | |
| 315 | tmp->brother = b; |
| 316 | b->parent = tmp->parent; |
| 317 | b->setTimeinfo(false); //whats this for FIXME? |
| 318 | } |
| 319 | |
| 320 | void Move::addStone(StoneColor c, int x, int y) |
| 321 | { |
| 322 | if ((x < 1) || (x > matrix->getSize()) || (y < 1) || (y > matrix->getSize())) |
| 323 | return; |
| 324 | |
| 325 | if (! parent) |
| 326 | { |
| 327 | setHandicapMove(true); |
| 328 | setMoveNumber(0); //TODO : make sure this is the proper way |
| 329 | setX(-1); |
| 330 | setY(-1); |
| 331 | setColor(stoneBlack); // So that it is white's turn |
| 332 | } |
| 333 | |
| 334 | matrix->insertStone(x, y, c, getGamePhase() == phaseEdit); |
| 335 | } |
| 336 | |
| 337 | bool Move::checkMoveIsValid(StoneColor c, int x, int y) |
| 338 | { |
| 339 | if (x == PASS_XY && y == PASS_XY) |
| 340 | return true; |
| 341 | |
| 342 | if (x < 1 || x > matrix->getSize() || y < 1 || y > matrix->getSize()) |
| 343 | { |
| 344 | qWarning("Invalid position: %d/%d", x, y); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | /* special case, we're erasing a stone */ |
| 349 | if(c == stoneErase) |
| 350 | { |
| 351 | return (matrix->getStoneAt(x,y) != stoneNone); |
| 352 | } |
| 353 | |
| 354 | // If there is already a stone at (x,y) do nothing |
| 355 | if ((matrix->getStoneAt(x, y) == stoneBlack) || |
| 356 | (matrix->getStoneAt(x, y) == stoneWhite)) |
| 357 | return false; |
| 358 | |
| 359 | // Check for ko |
| 360 | if (matrix->getMarkAt(x,y) & markKoMarker) |
| 361 | return false; |
| 362 | |
| 363 | std::vector<Group *> visited; |
| 364 | std::vector<Group *>::iterator it_visited; |
| 365 | int lastCaptures = matrix->checkStoneCaptures(c,x,y,visited); |
| 366 | // FIXME should not have to delete these groups here |
| 367 | for (it_visited = visited.begin(); it_visited < visited.end(); ++it_visited) |
| 368 | { |
| 369 | delete (*it_visited); |
| 370 | } |
| 371 | |
| 372 | return (lastCaptures >= 0); |
| 373 | } |
| 374 | |
| 375 | Move *Move::hasSon(StoneColor c, int x, int y) |
| 376 | { |
| 377 | Move *tmp = son; |
| 378 | while (tmp != NULL) { |
| 379 | if ((tmp->getColor() == c) && (tmp->getX() == x) && (tmp->getY() == y)) |
| 380 | break; |
| 381 | tmp = tmp->brother; |
| 382 | }; |
| 383 | return tmp; |
| 384 | } |
| 385 | |
| 386 | Move *Move::makeMove(StoneColor c, int x, int y, bool force) |
| 387 | { |
| 388 | if (force || checkMoveIsValid(c, x, y)) |
| 389 | return new Move(this, c, x, y); |
| 390 | else |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | Move *Move::makePass() |
| 395 | { |
| 396 | return makeMove(stoneColor == stoneBlack ? stoneWhite : stoneBlack, PASS_XY, PASS_XY, true); |
| 397 | } |
| 398 | |
| 399 | StoneColor Move::whoIsOnTurn() |
| 400 | { |
| 401 | if (getPLinfo()) |
| 402 | // color of next stone is same as current |
| 403 | return getPLnextMove(); |
| 404 | |
| 405 | return (stoneColor == stoneBlack ? stoneWhite : stoneBlack); |
| 406 | } |