| 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 "matrix.h" |
| 24 | #include "group.h" |
| 25 | |
| 26 | Matrix::Matrix(int s) |
| 27 | : size(s) |
| 28 | { |
| 29 | Q_ASSERT(size > 0 && size <= 36); |
| 30 | |
| 31 | matrix = new unsigned short[size*size]; |
| 32 | Q_CHECK_PTR(matrix); |
| 33 | |
| 34 | for (int i=0; i<size*size; i++) |
| 35 | matrix[i] = stoneNone; |
| 36 | } |
| 37 | |
| 38 | Matrix::Matrix(const Matrix &m, bool cleanup) |
| 39 | : size(m.size) |
| 40 | { |
| 41 | Q_ASSERT(size > 0 && size <= 36); |
| 42 | |
| 43 | matrix = new unsigned short[size*size]; |
| 44 | Q_CHECK_PTR(matrix); |
| 45 | |
| 46 | unsigned short mask = stoneWhite | stoneBlack; |
| 47 | if (!cleanup) |
| 48 | mask |= markAll; |
| 49 | mask &= (~markKoMarker); |
| 50 | for (int i=0; i<size*size; ++i) |
| 51 | matrix[i] = m.matrix[i] & mask; |
| 52 | } |
| 53 | |
| 54 | Matrix::~Matrix() |
| 55 | { |
| 56 | delete[] matrix; |
| 57 | } |
| 58 | |
| 59 | void Matrix::insertStone(int key, StoneColor c, bool fEdit) |
| 60 | { |
| 61 | matrix[key] &= (~stoneErase); |
| 62 | matrix[key] |= c; |
| 63 | if(fEdit) |
| 64 | matrix[key] |= MX_STONEEDIT; |
| 65 | } |
| 66 | |
| 67 | void Matrix::insertStone(int x, int y, StoneColor c, bool fEdit) |
| 68 | { |
| 69 | insertStone(coordsToKey(x,y),c,fEdit); |
| 70 | } |
| 71 | |
| 72 | StoneColor Matrix::getStoneAt(int key) const |
| 73 | { |
| 74 | return (StoneColor) (matrix[key] & 0x3); |
| 75 | } |
| 76 | |
| 77 | StoneColor Matrix::getStoneAt(int x, int y) |
| 78 | { |
| 79 | return getStoneAt(coordsToKey(x,y)); |
| 80 | } |
| 81 | |
| 82 | bool Matrix::isStoneDead(int x, int y) |
| 83 | { |
| 84 | return (matrix[coordsToKey(x,y)] & MX_STONEDEAD); |
| 85 | } |
| 86 | |
| 87 | MarkType Matrix::getMarkAt(int key) const |
| 88 | { |
| 89 | return (MarkType) (matrix[key] & markAll); |
| 90 | } |
| 91 | |
| 92 | MarkType Matrix::getMarkAt(int x, int y) |
| 93 | { |
| 94 | return getMarkAt(coordsToKey(x,y)); |
| 95 | } |
| 96 | |
| 97 | void Matrix::insertMark(int x, int y, MarkType t) |
| 98 | { |
| 99 | matrix[coordsToKey(x,y)] &= (~markAll); |
| 100 | matrix[coordsToKey(x,y)] |= t; |
| 101 | } |
| 102 | |
| 103 | void Matrix::removeMark(int x, int y) |
| 104 | { |
| 105 | Q_ASSERT(x > 0 && x <= size && |
| 106 | y > 0 && y <= size); |
| 107 | |
| 108 | matrix[coordsToKey(x,y)] &= (~markAll); |
| 109 | markTexts.remove(coordsToKey(x,y)); |
| 110 | } |
| 111 | |
| 112 | void Matrix::clearAllMarks() |
| 113 | { |
| 114 | for (int i=0; i<size*size; ++i) |
| 115 | { |
| 116 | matrix[i] &= (~markAll); |
| 117 | } |
| 118 | markTexts.clear(); |
| 119 | } |
| 120 | |
| 121 | void Matrix::clearTerritoryMarks() |
| 122 | { |
| 123 | for (int i=0; i<size*size; ++i) |
| 124 | matrix[i] &= (~markTerrDame); |
| 125 | } |
| 126 | |
| 127 | // Called when leaving score mode |
| 128 | void Matrix::absMatrix() |
| 129 | { |
| 130 | for (int i=0; i<size*size; ++i) |
| 131 | { |
| 132 | matrix[i] &= ~(MX_STONEDEAD | MX_STONEEDIT); |
| 133 | if (getStoneAt(i) == stoneErase) |
| 134 | insertStone(i, stoneNone); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void Matrix::setMarkText(int x, int y, const QString &txt) |
| 139 | { |
| 140 | markTexts.insert(coordsToKey(x,y),txt); |
| 141 | } |
| 142 | |
| 143 | const QString Matrix::getMarkText(int x, int y) |
| 144 | { |
| 145 | return markTexts.value(coordsToKey(x,y)); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * returns a string representing the marks in the matrix, in SGF format |
| 150 | */ |
| 151 | const QString Matrix::saveMarks() |
| 152 | { |
| 153 | QString txt, sSQ = "", sCR = "", sTR = "", sMA = "", sLB = "", sTB = "", sTW = ""; |
| 154 | int i, j, colw = 0, colb = 0; |
| 155 | |
| 156 | for (i=0; i<size; i++) |
| 157 | { |
| 158 | for (j=0; j<size; j++) |
| 159 | { |
| 160 | switch (getMarkAt(i + 1, j + 1)) |
| 161 | { |
| 162 | case markSquare: |
| 163 | if (sSQ.isEmpty()) |
| 164 | sSQ += "SQ"; |
| 165 | sSQ += "[" + coordsToString(i, j) + "]"; |
| 166 | break; |
| 167 | case markCircle: |
| 168 | if (sCR.isEmpty()) |
| 169 | sCR += "CR"; |
| 170 | sCR += "[" + coordsToString(i, j) + "]"; |
| 171 | break; |
| 172 | case markTriangle: |
| 173 | if (sTR.isEmpty()) |
| 174 | sTR += "TR"; |
| 175 | sTR += "[" + coordsToString(i, j) + "]"; |
| 176 | break; |
| 177 | case markCross: |
| 178 | if (sMA.isEmpty()) |
| 179 | sMA += "MA"; |
| 180 | sMA += "[" + coordsToString(i, j) + "]"; |
| 181 | break; |
| 182 | case markText: |
| 183 | case markNumber: |
| 184 | if (sLB.isEmpty()) |
| 185 | sLB += "LB"; |
| 186 | sLB += "[" + coordsToString(i, j); |
| 187 | sLB += ":"; |
| 188 | txt = getMarkText(i+1, j+1); |
| 189 | if (txt.isNull() || txt.isEmpty()) |
| 190 | sLB += "?"; // Whoops |
| 191 | else |
| 192 | sLB += txt; |
| 193 | sLB += "]"; |
| 194 | break; |
| 195 | case markTerrBlack: |
| 196 | if (sTB.isEmpty()) |
| 197 | { |
| 198 | sTB += "\nTB"; |
| 199 | colb = 0; |
| 200 | } |
| 201 | sTB += "[" + coordsToString(i, j) + "]"; |
| 202 | if (++colb % 15 == 0) |
| 203 | sTB += "\n "; |
| 204 | break; |
| 205 | case markTerrWhite: |
| 206 | if (sTW.isEmpty()) |
| 207 | { |
| 208 | sTW += "\nTW"; |
| 209 | colw = 0; |
| 210 | } |
| 211 | sTW += "[" + coordsToString(i, j) + "]"; |
| 212 | if (++colw % 15 == 0) |
| 213 | sTW += "\n "; |
| 214 | break; |
| 215 | default: continue; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return sSQ + sCR + sTR + sMA + sLB + sTB + sTW; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * returns a string representing the edited stones in the matrix, in SGF format |
| 225 | */ |
| 226 | const QString Matrix::saveEditedMoves(Matrix *parent) |
| 227 | { |
| 228 | QString sAB="", sAW="", sAE=""; |
| 229 | int i, j; |
| 230 | int z; |
| 231 | for (i=0; i<size; i++) |
| 232 | { |
| 233 | for (j=0; j<size; j++) |
| 234 | { |
| 235 | z = getStoneAt(i + 1, j + 1); |
| 236 | if (z != 0) |
| 237 | z= 0; |
| 238 | if(!(matrix[internalCoordsToKey(i,j)] & MX_STONEEDIT)) |
| 239 | continue; |
| 240 | switch (getStoneAt(i + 1, j + 1)) |
| 241 | { |
| 242 | case stoneBlack: |
| 243 | if (parent != NULL && |
| 244 | parent->getStoneAt(i + 1, j + 1) == stoneBlack) |
| 245 | break; |
| 246 | if (sAB.isEmpty()) |
| 247 | sAB += "AB"; |
| 248 | sAB += "[" + coordsToString(i, j) + "]"; |
| 249 | break; |
| 250 | |
| 251 | case stoneWhite: |
| 252 | if (parent != NULL && |
| 253 | parent->getStoneAt(i + 1, j + 1) == stoneWhite) |
| 254 | break; |
| 255 | if (sAW.isEmpty()) |
| 256 | sAW += "AW"; |
| 257 | sAW += "[" + coordsToString(i, j) + "]"; |
| 258 | break; |
| 259 | |
| 260 | case stoneErase: |
| 261 | if (parent != NULL && |
| 262 | (parent->getStoneAt(i + 1, j + 1) == stoneNone || |
| 263 | parent->getStoneAt(i + 1, j + 1) == stoneErase)) |
| 264 | break; |
| 265 | if (sAE.isEmpty()) |
| 266 | sAE += "AE"; |
| 267 | sAE += "[" + coordsToString(i, j) + "]"; |
| 268 | break; |
| 269 | default: |
| 270 | case stoneNone: |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return sAB + sAW + sAE; |
| 277 | } |
| 278 | |
| 279 | const QString Matrix::printMe(ASCII_Import *charset) |
| 280 | { |
| 281 | int i, j; |
| 282 | QString str; |
| 283 | |
| 284 | str = "\n "; |
| 285 | for (i=0; i<size; i++) |
| 286 | str += QString(QChar(static_cast<const char>('A' + (i<8?i:i+1)))) + " "; |
| 287 | str += "\n +"; |
| 288 | for (i=0; i<size-1; i++) |
| 289 | str += "--"; |
| 290 | str += "-+\n"; |
| 291 | |
| 292 | for (i=0; i<size; i++) |
| 293 | { |
| 294 | if (size-i < 10) str += " "; |
| 295 | str += QString::number(size-i) + " |"; |
| 296 | for (j=0; j<size; j++) |
| 297 | { |
| 298 | switch (getStoneAt(j + 1, i + 1)) |
| 299 | { |
| 300 | case stoneBlack: str += QChar(charset->blackStone); str += " "; break; |
| 301 | case stoneWhite: str += QChar(charset->whiteStone); str += " "; break; |
| 302 | default: |
| 303 | // Check for starpoints |
| 304 | if (size > 9) // 10x10 or larger |
| 305 | { |
| 306 | if ((i == 3 && j == 3) || |
| 307 | (i == size-4 && j == 3) || |
| 308 | (i == 3 && j == size-4) || |
| 309 | (i == size-4 && j == size-4) || |
| 310 | (i == (size+1)/2 - 1 && j == 3) || |
| 311 | (i == (size+1)/2 - 1 && j == size-4) || |
| 312 | (i == 3 && j == (size+1)/2 - 1) || |
| 313 | (i == size-4 && j == (size+1)/2 - 1) || |
| 314 | (i == (size+1)/2 - 1 && j == (size+1)/2 - 1)) |
| 315 | { |
| 316 | str += QChar(charset->starPoint); |
| 317 | str += " "; |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | else // 9x9 or smaller |
| 322 | { |
| 323 | if ((i == 2 && j == 2) || |
| 324 | (i == 2 && j == size-3) || |
| 325 | (i == size-3 && j == 2) || |
| 326 | (i == size-3 && j == size-3)) |
| 327 | { |
| 328 | str += QChar(charset->starPoint); |
| 329 | str += " "; |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | str += QChar(charset->emptyPoint); |
| 335 | str += " "; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | str = str.left(str.length()-1); |
| 340 | str += "| "; |
| 341 | if (size-i < 10) str += " "; |
| 342 | str += QString::number(size-i) + "\n"; |
| 343 | } |
| 344 | |
| 345 | str += " +"; |
| 346 | for (i=0; i<size-1; i++) |
| 347 | str += "--"; |
| 348 | str += "-+\n "; |
| 349 | for (i=0; i<size; i++) |
| 350 | str += QString(QChar(static_cast<const char>('A' + (i<8?i:i+1)))) + " "; |
| 351 | str += "\n"; |
| 352 | |
| 353 | return str; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * checks wether the positions x,y holds a stone 'color'. |
| 358 | * if yes, appends the stone and returns the group |
| 359 | */ |
| 360 | void Matrix::checkNeighbour(int key, StoneColor color, Group *group, std::vector<int> *libertyList) const |
| 361 | { |
| 362 | StoneColor c = getStoneAt(key); |
| 363 | if(c != color) |
| 364 | { |
| 365 | if((libertyList) && (c == stoneNone) |
| 366 | && (std::find(libertyList->begin(), libertyList->end(), key) == libertyList->end())) |
| 367 | { |
| 368 | libertyList->push_back(key); |
| 369 | group->liberties++; |
| 370 | } |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | if (!(group->contains(key))) |
| 375 | group->append(key); |
| 376 | } |
| 377 | |
| 378 | /* Counts and returns the number of liberties of a group. |
| 379 | * What does not count as a liberty is determined by "mask". |
| 380 | * By default "mask" is set to black and white stones, |
| 381 | * so any unoocupied point counts as a liberty. |
| 382 | * One can put opponent's territory into mask to exclude dame points |
| 383 | * from liberties. This is useful for detecting false eyes. */ |
| 384 | int Matrix::countLiberties(Group *group, unsigned short mask) |
| 385 | { |
| 386 | QVector<int> libCounted; |
| 387 | |
| 388 | // Walk through the horizontal and vertial directions, counting the |
| 389 | // liberties of this group. |
| 390 | for (int i=0; i<group->count(); i++) |
| 391 | { |
| 392 | std::vector<int> neighbors = getNeighbors(group->at(i)); |
| 393 | std::vector<int>::iterator it_neighbor; |
| 394 | for (it_neighbor = neighbors.begin(); it_neighbor < neighbors.end(); ++it_neighbor) |
| 395 | { |
| 396 | if ( !(matrix[*it_neighbor] & mask) && |
| 397 | !libCounted.contains(*it_neighbor)) |
| 398 | { |
| 399 | libCounted.append(*it_neighbor); |
| 400 | } |
| 401 | |
| 402 | } |
| 403 | } |
| 404 | return libCounted.size(); |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Returns the group (QList) of matrix stones connected with the stone "key" |
| 409 | */ |
| 410 | Group* Matrix::assembleGroup(int key, StoneColor c) const |
| 411 | { |
| 412 | Group *group = new Group(c); |
| 413 | Q_CHECK_PTR(group); |
| 414 | group->append(key); |
| 415 | std::vector<int> libertyList; |
| 416 | int mark = 0; |
| 417 | |
| 418 | // Walk through the horizontal and vertical directions and assemble the |
| 419 | // attached stones to this group. |
| 420 | while (mark < group->count()) |
| 421 | { |
| 422 | if (getStoneAt(group->at(mark)) != stoneNone ) //FIXME how can this be stoneNone? |
| 423 | { |
| 424 | std::vector<int> neighbors = getNeighbors(group->at(mark)); |
| 425 | std::vector<int>::iterator it_neighbor; |
| 426 | for (it_neighbor = neighbors.begin(); it_neighbor < neighbors.end(); ++it_neighbor) |
| 427 | checkNeighbour(*it_neighbor,c,group, &libertyList); |
| 428 | } |
| 429 | mark ++; |
| 430 | } |
| 431 | |
| 432 | return group; |
| 433 | } |
| 434 | |
| 435 | /* This puts groups that the stone touches into "visited" and counts the |
| 436 | * number of captured stones (positive if enemy stones are captured, |
| 437 | * negative for suicide). This number can be used to check if the |
| 438 | * move is legal before playing it. |
| 439 | * This function does not alter the matrix */ |
| 440 | int Matrix::checkStoneCaptures(StoneColor ourColor, int x, int y, std::vector<Group *> &visited) const |
| 441 | { |
| 442 | StoneColor theirColor = (ourColor == stoneWhite ? stoneBlack : stoneWhite); |
| 443 | StoneColor c; |
| 444 | Group * g = NULL; |
| 445 | bool emptyNeighbor = false; |
| 446 | |
| 447 | // Find adjacent groups and put them into "visited" |
| 448 | std::vector<int> neighbors = getNeighbors(coordsToKey(x,y)); |
| 449 | std::vector<int>::iterator it_neighbor; |
| 450 | std::vector<Group *>::iterator it_visited; |
| 451 | for (it_neighbor = neighbors.begin(); it_neighbor < neighbors.end(); ++it_neighbor) |
| 452 | { |
| 453 | c = getStoneAt(*it_neighbor); |
| 454 | if ((c == stoneBlack) || (c == stoneWhite)) |
| 455 | { |
| 456 | g = NULL; |
| 457 | for (it_visited = visited.begin(); it_visited < visited.end(); ++it_visited) |
| 458 | { |
| 459 | if ((*it_visited)->contains(*it_neighbor)) |
| 460 | { |
| 461 | g = *it_visited; |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | if (g == NULL) |
| 466 | { |
| 467 | g = assembleGroup(*it_neighbor, c); |
| 468 | visited.push_back(g); |
| 469 | } |
| 470 | } else |
| 471 | emptyNeighbor = true; |
| 472 | } |
| 473 | |
| 474 | // Count the number of captured stones |
| 475 | int captures = 0; |
| 476 | // Check if some enemy groups are captured |
| 477 | for (it_visited = visited.begin(); it_visited < visited.end(); ++it_visited) |
| 478 | { |
| 479 | if (((*it_visited)->c == theirColor) && ((*it_visited)->liberties == 1)) |
| 480 | captures += (*it_visited)->size(); |
| 481 | } |
| 482 | // If enemy captured, return number of captures |
| 483 | if (captures > 0) |
| 484 | return captures; |
| 485 | // If no enemies captured, proceed |
| 486 | // If some neighboring square is empty, this is not a suicide |
| 487 | if (emptyNeighbor) |
| 488 | return 0; |
| 489 | // Count the number of stones in our adjacent groups |
| 490 | emptyNeighbor = false; |
| 491 | for (it_visited = visited.begin(); it_visited < visited.end(); ++it_visited) |
| 492 | { |
| 493 | if ((*it_visited)->c == ourColor) |
| 494 | { |
| 495 | captures -= (*it_visited)->size(); |
| 496 | if ((*it_visited)->liberties > 1) |
| 497 | emptyNeighbor = true; |
| 498 | } |
| 499 | } |
| 500 | // If some group has >1 liberty, this is not a suicide |
| 501 | if (emptyNeighbor) |
| 502 | return 0; |
| 503 | // Else this is a suicide, return the (negative) number of dying stones |
| 504 | return captures-1; |
| 505 | } |
| 506 | |
| 507 | /* This function executes a move without checking its validity. |
| 508 | * This function returns the number of captured stones. |
| 509 | * If the return value is negative, this means that the requested move |
| 510 | * was a suicide. */ |
| 511 | int Matrix::makeMove(int x, int y, StoneColor c) |
| 512 | { |
| 513 | std::vector<Group *> visited; |
| 514 | int capturedStones = checkStoneCaptures(c, x, y, visited); |
| 515 | |
| 516 | if (capturedStones >= 0) |
| 517 | insertStone(x, y, c); |
| 518 | |
| 519 | std::vector<Group *>::iterator it_visited; |
| 520 | for (it_visited = visited.begin(); it_visited < visited.end(); ++it_visited) |
| 521 | { |
| 522 | if (((*it_visited)->c != c) && ((*it_visited)->liberties == 1)) // Enemy group with one liberty |
| 523 | { |
| 524 | removeGroup((*it_visited)); |
| 525 | } else if (((*it_visited)->c == c) && (capturedStones < 0)) // suicide |
| 526 | { |
| 527 | removeGroup((*it_visited)); |
| 528 | } else |
| 529 | { |
| 530 | delete (*it_visited); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // Add Ko mark |
| 535 | if (capturedStones == 1) |
| 536 | { |
| 537 | int koStoneX = -1, koStoneY = -1; |
| 538 | StoneColor opp = (c == stoneBlack ? stoneWhite : stoneBlack); |
| 539 | StoneColor testcolor; |
| 540 | int sides = 4; |
| 541 | if(x < size) |
| 542 | { |
| 543 | testcolor = getStoneAt(x + 1, y); |
| 544 | if(testcolor == opp) |
| 545 | sides--; |
| 546 | else if(testcolor == stoneNone) |
| 547 | { |
| 548 | koStoneX = x + 1; |
| 549 | koStoneY = y; |
| 550 | } |
| 551 | } |
| 552 | else |
| 553 | sides--; |
| 554 | |
| 555 | if(x > 1) |
| 556 | { |
| 557 | testcolor = getStoneAt(x - 1, y); |
| 558 | if(testcolor == opp) |
| 559 | sides--; |
| 560 | else if(testcolor == stoneNone) |
| 561 | { |
| 562 | koStoneX = x - 1; |
| 563 | koStoneY = y; |
| 564 | } |
| 565 | } |
| 566 | else |
| 567 | sides--; |
| 568 | if(y < size) |
| 569 | { |
| 570 | testcolor = getStoneAt(x, y + 1); |
| 571 | if(testcolor == opp) |
| 572 | sides--; |
| 573 | else if(testcolor == stoneNone) |
| 574 | { |
| 575 | koStoneX = x; |
| 576 | koStoneY = y + 1; |
| 577 | } |
| 578 | } |
| 579 | else |
| 580 | sides--; |
| 581 | |
| 582 | if(y > 1) |
| 583 | { |
| 584 | testcolor = getStoneAt(x, y - 1); |
| 585 | if(testcolor == opp) |
| 586 | sides--; |
| 587 | else if(testcolor == stoneNone) |
| 588 | { |
| 589 | koStoneX = x; |
| 590 | koStoneY = y - 1; |
| 591 | } |
| 592 | } |
| 593 | else |
| 594 | sides--; |
| 595 | |
| 596 | if(sides == 1) |
| 597 | insertMark(koStoneX, koStoneY, markKoMarker); |
| 598 | } |
| 599 | |
| 600 | return capturedStones; |
| 601 | } |
| 602 | |
| 603 | bool Matrix::addHandicapStones(int handicap) |
| 604 | { |
| 605 | if (size > 25 || size < 7) |
| 606 | { |
| 607 | qWarning("Matrix::addHandicapStones() : Can't set handicap for this board size"); |
| 608 | return false; |
| 609 | } |
| 610 | const int edge_dist = (size > 12 ? 4 : 3); |
| 611 | const int low = edge_dist; |
| 612 | const int middle = (size + 1) / 2; |
| 613 | const int high = size + 1 - edge_dist; |
| 614 | // extra: |
| 615 | if (size == 19 && handicap > 9) |
| 616 | switch (handicap) |
| 617 | { |
| 618 | case 13: // Hehe, this is nuts... :) |
| 619 | insertStone(17, 17, stoneBlack, true); |
| 620 | case 12: |
| 621 | insertStone(3, 3, stoneBlack, true); |
| 622 | case 11: |
| 623 | insertStone(3, 17, stoneBlack, true); |
| 624 | case 10: |
| 625 | insertStone(17, 3, stoneBlack, true); |
| 626 | |
| 627 | default: |
| 628 | handicap = 9; |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | switch (size % 2) |
| 633 | { |
| 634 | // odd board size |
| 635 | case 1: |
| 636 | switch (handicap) |
| 637 | { |
| 638 | case 9: |
| 639 | insertStone(middle, middle, stoneBlack, true); |
| 640 | case 8: |
| 641 | case 7: |
| 642 | if (handicap >= 8) |
| 643 | { |
| 644 | insertStone(middle, low, stoneBlack, true); |
| 645 | insertStone(middle, high, stoneBlack, true); |
| 646 | } |
| 647 | else |
| 648 | insertStone(middle, middle, stoneBlack, true); |
| 649 | case 6: |
| 650 | case 5: |
| 651 | if (handicap >= 6) |
| 652 | { |
| 653 | insertStone(low, middle, stoneBlack, true); |
| 654 | insertStone(high, middle, stoneBlack, true); |
| 655 | } |
| 656 | else |
| 657 | insertStone(middle, middle, stoneBlack, true); |
| 658 | case 4: |
| 659 | insertStone(high, high, stoneBlack, true); |
| 660 | case 3: |
| 661 | insertStone(low, low, stoneBlack, true); |
| 662 | case 2: |
| 663 | insertStone(high, low, stoneBlack, true); |
| 664 | case 1: // Handicap 1? Whatever floats your boat. |
| 665 | insertStone(low, high, stoneBlack, true); |
| 666 | break; |
| 667 | |
| 668 | default: |
| 669 | qWarning("*** BoardHandler::setHandicap() - Invalid handicap given: %d", handicap); |
| 670 | } |
| 671 | break; |
| 672 | |
| 673 | // even board size |
| 674 | case 0: |
| 675 | switch (handicap) |
| 676 | { |
| 677 | case 4: |
| 678 | insertStone(high, high, stoneBlack); |
| 679 | case 3: |
| 680 | insertStone(low, low, stoneBlack); |
| 681 | case 2: |
| 682 | insertStone(high, low, stoneBlack); |
| 683 | case 1: // Handicap 1? Whatever floats your boat. |
| 684 | insertStone(low, high, stoneBlack); |
| 685 | break; |
| 686 | |
| 687 | default: |
| 688 | qWarning("*** Matrix::addHandicapStones() - Invalid handicap given: %d", handicap); |
| 689 | } |
| 690 | break; |
| 691 | |
| 692 | default: |
| 693 | qWarning("*** Matrix::addHandicapStones() - can't set handicap for this board size"); |
| 694 | } |
| 695 | return true; |
| 696 | } |
| 697 | |
| 698 | /* Note that checkStoneWithGroups would be called before this meaning that |
| 699 | * their could be no adjacent stones not in groups */ |
| 700 | void Matrix::removeGroup(Group * g) |
| 701 | { |
| 702 | /* For every stone in g we go through the neighboring groups. |
| 703 | * If the neighboring group is distinct from g, then it has different color. |
| 704 | * Every such group gets its number of liberties increased by 1. |
| 705 | * We take care not to count such groups multiple times. */ |
| 706 | for(int n = 0; n < g->count(); ++n) |
| 707 | { |
| 708 | insertStone(g->at(n), stoneNone); |
| 709 | } |
| 710 | delete g; |
| 711 | } |
| 712 | |
| 713 | /* This is kind of ugly but I'm trying to use the existing matrix |
| 714 | * code for something weird |
| 715 | * Could there be a potential miscalc if called on empty vertex? |
| 716 | * Would it be*/ |
| 717 | Group* Matrix::assembleAreaGroups(int key, StoneColor c) |
| 718 | { |
| 719 | Group *group = new Group(c); |
| 720 | Q_CHECK_PTR(group); |
| 721 | StoneColor oppColor = (c == stoneWhite ? stoneBlack : stoneWhite); |
| 722 | |
| 723 | group->append(key); |
| 724 | |
| 725 | int mark = 0; |
| 726 | // Walk through the horizontal and vertical directions and assemble the |
| 727 | // attached stones to this group. |
| 728 | while (mark < group->count()) |
| 729 | { |
| 730 | if (getStoneAt(group->at(mark)) != oppColor ) |
| 731 | { |
| 732 | std::vector<int> neighbors = getNeighbors(group->at(mark)); |
| 733 | std::vector<int>::iterator it_neighbor; |
| 734 | for (it_neighbor = neighbors.begin(); it_neighbor < neighbors.end(); ++it_neighbor) |
| 735 | { |
| 736 | checkNeighbour(*it_neighbor,c,group); |
| 737 | checkNeighbour(*it_neighbor,stoneNone,group); |
| 738 | } |
| 739 | } |
| 740 | mark ++; |
| 741 | } |
| 742 | |
| 743 | return group; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * This function marks all the stones of a group as dead (or alive if they were dead) |
| 748 | */ |
| 749 | void Matrix::toggleGroupAt( int x, int y) |
| 750 | { |
| 751 | StoneColor col = getStoneAt(x, y); |
| 752 | |
| 753 | if ( col != stoneWhite && col != stoneBlack ) |
| 754 | return ; |
| 755 | |
| 756 | Group *g = assembleGroup(coordsToKey(x,y),col); //FIXME use the view |
| 757 | |
| 758 | for (int i=0; i<g->count(); i++) |
| 759 | toggleStoneAt(g->at(i)); |
| 760 | } |
| 761 | |
| 762 | /* These are all kind of redundant, some aren't used, etc.. |
| 763 | * the other thing that bothers me is that the stone.cpp class |
| 764 | * should probably hold "dirty" and then we've considered before |
| 765 | * that matrix could be removed for something that takes up |
| 766 | * less space, but if this works for now, that's fine. FIXME */ |
| 767 | |
| 768 | void Matrix::toggleStoneAt(int key) |
| 769 | { |
| 770 | matrix[key] ^= MX_STONEDEAD; |
| 771 | } |
| 772 | |
| 773 | void Matrix::toggleStoneAt(int x, int y) |
| 774 | { |
| 775 | toggleStoneAt(coordsToKey(x,y)); |
| 776 | } |
| 777 | |
| 778 | |
| 779 | void Matrix::markStoneDead(int x, int y) |
| 780 | { |
| 781 | if(isStoneDead(x, y)) |
| 782 | return; |
| 783 | toggleStoneAt(x, y); |
| 784 | } |
| 785 | |
| 786 | void Matrix::markGroupDead(int x, int y) |
| 787 | { |
| 788 | if(isStoneDead(x, y)) |
| 789 | return; |
| 790 | toggleGroupAt(x, y); |
| 791 | } |
| 792 | |
| 793 | void Matrix::markGroupAlive(int x, int y) |
| 794 | { |
| 795 | if(!isStoneDead(x, y)) |
| 796 | return; |
| 797 | toggleGroupAt(x, y); |
| 798 | } |
| 799 | |
| 800 | /* Any stones connected to this xy stone by empty |
| 801 | * vertices are to be marked dead */ |
| 802 | void Matrix::toggleAreaAt( int x, int y) |
| 803 | { |
| 804 | StoneColor col = getStoneAt(x, y); |
| 805 | |
| 806 | if ( col != stoneWhite && col != stoneBlack ) |
| 807 | return ; |
| 808 | |
| 809 | Group *g = assembleAreaGroups(coordsToKey(x,y),col); |
| 810 | |
| 811 | for (int i=0; i<g->count(); i++) |
| 812 | toggleStoneAt(g->at(i)); |
| 813 | |
| 814 | delete g; |
| 815 | } |
| 816 | |
| 817 | void Matrix::markAreaDead(int x, int y) |
| 818 | { |
| 819 | if(isStoneDead(x, y)) |
| 820 | return; |
| 821 | toggleAreaAt(x, y); |
| 822 | } |
| 823 | |
| 824 | void Matrix::markAreaAlive(int x, int y) |
| 825 | { |
| 826 | if(!isStoneDead(x, y)) |
| 827 | return; |
| 828 | toggleAreaAt(x, y); |
| 829 | } |
| 830 | |
| 831 | void Matrix::floodTerritory(unsigned short mark, int key) |
| 832 | { |
| 833 | std::vector<int> neighbors = getNeighbors(key); |
| 834 | std::vector<int>::iterator it_neighbor; |
| 835 | for (it_neighbor = neighbors.begin(); it_neighbor < neighbors.end(); ++it_neighbor) |
| 836 | { |
| 837 | // If the neighbor point does not hold a live stone and has not been flooded yet |
| 838 | if (((getStoneAt(*it_neighbor) == stoneNone) || (matrix[*it_neighbor] & MX_STONEDEAD)) && (!(matrix[*it_neighbor] & mark))) |
| 839 | { |
| 840 | matrix[*it_neighbor] |= mark; |
| 841 | floodTerritory(mark, *it_neighbor); |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | void Matrix::markTerritory() |
| 847 | { |
| 848 | clearTerritoryMarks(); |
| 849 | |
| 850 | for (int i=0; i<size*size; ++i) |
| 851 | { |
| 852 | // If there is a live stone at i |
| 853 | if ((getStoneAt(i) != stoneNone) && !(matrix[i] & MX_STONEDEAD)) |
| 854 | // flood surrounding territory with its color |
| 855 | floodTerritory(getStoneAt(i) == stoneBlack ? markTerrBlack : markTerrWhite, i); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | void Matrix::count(int &terrBlack, int &terrWhite, int &deadBlack, int &deadWhite) |
| 860 | { |
| 861 | terrWhite = 0; |
| 862 | terrBlack = 0; |
| 863 | deadBlack = 0; |
| 864 | deadWhite = 0; |
| 865 | // Count territory |
| 866 | for (int i=0; i<size*size; ++i) |
| 867 | { |
| 868 | if (matrix[i] & markTerrWhite) |
| 869 | { |
| 870 | if (!(matrix[i] & markTerrBlack)) |
| 871 | ++terrWhite; |
| 872 | if (getStoneAt(i) == stoneBlack) |
| 873 | ++deadBlack; |
| 874 | } |
| 875 | if (matrix[i] & markTerrBlack) |
| 876 | { |
| 877 | if (!(matrix[i] & markTerrWhite)) |
| 878 | ++terrBlack; |
| 879 | if (getStoneAt(i) == stoneWhite) |
| 880 | ++deadWhite; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * This function returns the first letter or number available in the letter marks |
| 887 | */ |
| 888 | QString Matrix::getFirstTextAvailable(MarkType t) |
| 889 | { |
| 890 | QString mark; |
| 891 | |
| 892 | switch (t) |
| 893 | { |
| 894 | case markText : |
| 895 | for (int n = 0 ; n < 51 ; n++) |
| 896 | { |
| 897 | mark = QString(QChar(static_cast<const char>('A' + (n>=26 ? n+6 : n)))); |
| 898 | if (markTexts.key(mark,-1) == -1) |
| 899 | break; |
| 900 | } |
| 901 | break; |
| 902 | case markNumber : |
| 903 | for (int n = 1 ; n < 400 ; n++) |
| 904 | { |
| 905 | mark = QString::number(n); |
| 906 | if (markTexts.key(mark,-1) == -1) |
| 907 | break; |
| 908 | } |
| 909 | break; |
| 910 | //should not happen |
| 911 | default: |
| 912 | break; |
| 913 | } |
| 914 | |
| 915 | return mark; |
| 916 | } |
| 917 | |
| 918 | std::vector<int> Matrix::getNeighbors(int key) const |
| 919 | { |
| 920 | std::vector<int> result; |
| 921 | if (key >= size) // north |
| 922 | result.push_back(key-size); |
| 923 | if ((key % size) > 0) // west |
| 924 | result.push_back(key-1); |
| 925 | if ((key % size) < size-1) // east |
| 926 | result.push_back(key+1); |
| 927 | if ((key)<size*size-size) // south |
| 928 | result.push_back(key+size); |
| 929 | return result; |
| 930 | } |