📄 stonehandler.cpp
字号:
for (unsigned int i=0; i<group->count(); i++) { Stone *tmp = group->at(i); CHECK_PTR(tmp); int x = tmp->posX(), y = tmp->posY(); // North checkNeighbourLibertyOnMatrix(x, y-1, libCounted, liberties, m); // West checkNeighbourLibertyOnMatrix(x-1, y, libCounted, liberties, m); // South checkNeighbourLibertyOnMatrix(x, y+1, libCounted, liberties, m); // East checkNeighbourLibertyOnMatrix(x+1, y, libCounted, liberties, m); } return liberties;}void StoneHandler::checkNeighbourLibertyOnMatrix(int x, int y, QValueList<int> &libCounted, int &liberties, Matrix *m){ if (!x || !y) return; // Stone *s; if (x <= boardHandler->board->getBoardSize() && y <= boardHandler->board->getBoardSize() && x >= 0 && y >= 0 && !libCounted.contains(100*x + y) && (m->at(x - 1, y - 1) == MARK_TERRITORY_DONE_BLACK || m->at(x - 1, y - 1) == MARK_TERRITORY_DONE_WHITE)) { libCounted.append(100*x + y); liberties ++; }}bool StoneHandler::updateAll(Matrix *m, bool toDraw){ // qDebug("StoneHandler::updateAll(Matrix *m) - toDraw = %d", toDraw); CHECK_PTR(m); // m->debug(); Stone *stone; bool modified = false, fake = false; short data; /* * Synchronize the matrix with the stonehandler data and * update the canvas. * This is usually called when navigating through the tree. */ for (int y=1; y<=boardHandler->board->getBoardSize(); y++) { for (int x=1; x<=boardHandler->board->getBoardSize(); x++) { // Extract the data for the stone from the matrix data = abs(m->at(x-1, y-1) % 10); switch (data) { case stoneBlack: if ((stone = stones->find(Matrix::coordsToKey(x, y))) == NULL) { addStone(boardHandler->board->addStoneSprite(stoneBlack, x, y, fake), true, false); modified = true; break; } else if (!stone->visible()) { stone->show(); modified = true; } if (stone->getColor() == stoneWhite) { stone->setColor(stoneBlack); modified = true; } break; case stoneWhite: if ((stone = stones->find(Matrix::coordsToKey(x, y))) == NULL) { addStone(boardHandler->board->addStoneSprite(stoneWhite, x, y, fake), true, false); modified = true; break; } else if (!stone->visible()) { stone->show(); modified = true; } if (stone->getColor() == stoneBlack) { stone->setColor(stoneWhite); modified = true; } break; case stoneNone: case stoneErase: if ((stone = stones->find(Matrix::coordsToKey(x, y))) != NULL && stone->visible()) { stone->hide(); modified = true; } break; default: qWarning("Bad matrix data <%d> at %d/%d in StoneHandler::updateAll(Matrix *m) !", data, x, y); } // Skip mark drawing when reading sgf if (!toDraw) continue; // Extract the mark data from the matrix data = abs(m->at(x-1, y-1) / 10); switch (data) { case markSquare: modified = true; boardHandler->board->setMark(x, y, markSquare, false); break; case markCircle: modified = true; boardHandler->board->setMark(x, y, markCircle, false); break; case markTriangle: modified = true; boardHandler->board->setMark(x, y, markTriangle, false); break; case markCross: modified = true; boardHandler->board->setMark(x, y, markCross, false); break; case markText: modified = true; boardHandler->board->setMark(x, y, markText, false, m->getMarkText(x, y)); break; case markNumber: modified = true; boardHandler->board->setMark(x, y, markNumber, false, m->getMarkText(x, y)); break; case markTerrBlack: modified = true; boardHandler->board->setMark(x, y, markTerrBlack, false); break; case markTerrWhite: modified = true; boardHandler->board->setMark(x, y, markTerrWhite, false); break; case markNone: if (boardHandler->board->hasMark(x, y)) { modified = true; boardHandler->board->removeMark(x, y, false); } } } } return modified;}void StoneHandler::checkAllPositions(){ // Traverse all stones and check their positions. // Yuck, I suppose this is expensive... // Called when jumping through the variations. groups->clear(); QIntDictIterator<Stone> it(*stones); Stone *s; // Unmark stones as checked while (it.current()) { it.current()->checked = false; ++it; } it.toFirst(); while (it.current()) { s = it.current(); if (!s->checked) checkPosition(s,NULL); //SL added eb 8 ++it; }}#ifndef NO_DEBUGvoid StoneHandler::debug(){ qDebug("StoneHandler::debug()"); #if 0 QIntDictIterator<Stone> its(*stones); Stone *s; while (its.current()) { s = its.current(); qDebug("%d -> %s", its.currentKey(), s->getColor() == stoneBlack ? "Black" : "White"); ++its; }#endif QListIterator<Group> it(*groups); for (; it.current(); ++it) { Group *g = it.current(); g->debug(); }}#endifbool StoneHandler::removeDeadGroup(int x, int y, int &caps, StoneColor &col, bool &dead){ if (hasStone(x, y) != 1) return false; Stone *s = getStoneAt(x, y); CHECK_PTR(s); col = s->getColor(); if (!s->isDead()) dead = true; Group *g = assembleGroup(s, NULL); //SL added eb 8 CHECK_PTR(g); caps = g->count(); // Mark stones of this group as dead or alive again QListIterator<Stone> it(*g); for (; it.current(); ++it) { s = it.current(); CHECK_PTR(s); s->setDead(dead); if (dead) { s->setSequence(boardHandler->board->getImageHandler()->getGhostPixmaps()); s->shadow->hide(); } else { s->setSequence(boardHandler->board->getImageHandler()->getStonePixmaps()); s->shadow->show(); } } delete g; return true;}bool StoneHandler::markSekiGroup(int x, int y, int &caps, StoneColor &col, bool &seki){ if (hasStone(x, y) != 1) return false; Stone *s = getStoneAt(x, y); CHECK_PTR(s); col = s->getColor(); if (!s->isSeki()) seki = true; Group *g = assembleGroup(s, NULL); //SL added eb 8 CHECK_PTR(g); // Mark stones of this group as seki QListIterator<Stone> it(*g); for (; it.current(); ++it) { s = it.current(); CHECK_PTR(s); if (seki && s->isDead()) caps ++; s->setSeki(seki); if (seki) { s->setSequence(boardHandler->board->getImageHandler()->getGhostPixmaps()); s->shadow->hide(); } else { s->setSequence(boardHandler->board->getImageHandler()->getStonePixmaps()); s->shadow->show(); } } delete g; return true;}void StoneHandler::removeDeadMarks(){ QIntDictIterator<Stone> it(*stones); Stone *s; while (it.current()) { s = it.current(); CHECK_PTR(s); if (s->isDead() || s->isSeki()) { s->setDead(false); s->setSeki(false); s->setSequence(boardHandler->board->getImageHandler()->getStonePixmaps()); s->shadow->show(); } ++it; }}void StoneHandler::updateDeadMarks(int &black, int &white){ QIntDictIterator<Stone> it(*stones); Stone *s; while (it.current()) { s = it.current(); CHECK_PTR(s); if (s->isDead()) { if (s->getColor() == stoneBlack) white ++; else black ++; } ++it; } }bool StoneHandler:: checkFalseEye(Matrix *m, int x, int y, int col){ int bsize = m->getSize(); // Stone to the North? if (y - 1 >= 0 && m->at(x, y - 1) == col) if (countLibertiesOnMatrix(assembleGroup(getStoneAt(x + 1, y),NULL), m) == 1) //SL added eb 8 return true; // Stone to the west? if (x - 1 >= 0 && m->at(x - 1, y) == col) //SL added eb 8 if (countLibertiesOnMatrix(assembleGroup(getStoneAt(x, y + 1),NULL), m) == 1) return true; // Stone to the south? if (y + 1 < bsize && m->at(x, y + 1) == col) if (countLibertiesOnMatrix(assembleGroup(getStoneAt(x + 1, y + 2),NULL), m) == 1) //SL added eb 8 return true; // Stone to the east? if (x + 1 < bsize && m->at(x + 1, y) == col) if (countLibertiesOnMatrix(assembleGroup(getStoneAt(x + 2, y + 1),NULL), m) == 1) //SL added eb 8 return true; return false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -