From 143dc12e158335cb56a4cce570c6ef521340b086 Mon Sep 17 00:00:00 2001 From: CalciteM Team Date: Sun, 15 Sep 2019 18:06:05 +0800 Subject: [PATCH] position_ -> game_ (2) --- src/base/thread.cpp | 12 +-- src/base/thread.h | 2 +- src/ui/qt/gamecontroller.cpp | 172 +++++++++++++++++------------------ src/ui/qt/gamecontroller.h | 2 +- 4 files changed, 94 insertions(+), 94 deletions(-) diff --git a/src/base/thread.cpp b/src/base/thread.cpp index a9707bb1..772adcf2 100644 --- a/src/base/thread.cpp +++ b/src/base/thread.cpp @@ -27,7 +27,7 @@ AiThread::AiThread(int id, QObject *parent) : QThread(parent), waiting_(false), - position_(nullptr), + game_(nullptr), aiDepth(2), aiTime(3600) { @@ -65,8 +65,8 @@ void AiThread::setAi(const Game &game) { mutex.lock(); - this->position_ = &game; - ai_ab.setGame(*(this->position_)); + this->game_ = &game; + ai_ab.setGame(*(this->game_)); #ifdef TRANSPOSITION_TABLE_ENABLE // 新下一盘前清除哈希表 (注意可能同时存在每步之前清除) @@ -81,7 +81,7 @@ void AiThread::setAi(const Game &game) void AiThread::setAi(const Game &game, depth_t depth, int time) { mutex.lock(); - this->position_ = &game; + this->game_ = &game; ai_ab.setGame(game); aiDepth = depth; aiTime = time; @@ -108,7 +108,7 @@ void AiThread::run() while (!isInterruptionRequested()) { mutex.lock(); - i = Player::toId(position_->context.turn); + i = Player::toId(game_->context.turn); if (i != id || waiting_) { pauseCondition.wait(&mutex); @@ -116,7 +116,7 @@ void AiThread::run() continue; } - ai_ab.setGame(*position_); + ai_ab.setGame(*game_); emit calcStarted(); mutex.unlock(); diff --git a/src/base/thread.h b/src/base/thread.h index c3f9abc0..aa890a0f 100644 --- a/src/base/thread.h +++ b/src/base/thread.h @@ -110,7 +110,7 @@ private: QWaitCondition pauseCondition; // 主线程棋对象的引用 - const Game *position_; + const Game *game_; // Alpha-Beta剪枝算法类 MillGameAi_ab ai_ab; diff --git a/src/ui/qt/gamecontroller.cpp b/src/ui/qt/gamecontroller.cpp index 9b855569..508dcda2 100644 --- a/src/ui/qt/gamecontroller.cpp +++ b/src/ui/qt/gamecontroller.cpp @@ -122,9 +122,9 @@ const QMap GameController::getActions() void GameController::gameStart() { - position_.configure(giveUpIfMostLose_, randomMove_); - position_.start(); - dummyGame = position_; + game_.configure(giveUpIfMostLose_, randomMove_); + game_.start(); + dummyGame = game_; // 每隔100毫秒调用一次定时器处理函数 if (timeID == 0) { @@ -142,15 +142,15 @@ void GameController::gameReset() timeID = 0; // 棋未下完,则算对手得分 - if (position_.getPhase() == PHASE_MOVING && - position_.whoWin() == PLAYER_NOBODY) { + if (game_.getPhase() == PHASE_MOVING && + game_.whoWin() == PLAYER_NOBODY) { giveUp(); } // 重置游戏 - position_.configure(giveUpIfMostLose_, randomMove_); - position_.reset(); - dummyGame = position_; + game_.configure(giveUpIfMostLose_, randomMove_); + game_.reset(); + dummyGame = game_; // 停掉线程 if (!isAutoRestart) { @@ -166,7 +166,7 @@ void GameController::gameReset() currentPiece = nullptr; // 重新绘制棋盘 - scene.setDiagonal(position_.getRule()->hasObliqueLines); + scene.setDiagonal(game_.getRule()->hasObliqueLines); // 绘制所有棋子,放在起始位置 // 0: 先手第1子; 1:后手第1子 @@ -175,7 +175,7 @@ void GameController::gameReset() PieceItem::Models md; PieceItem *newP; - for (int i = 0; i < position_.getRule()->nTotalPiecesEachSide; i++) { + for (int i = 0; i < game_.getRule()->nTotalPiecesEachSide; i++) { // 先手的棋子 md = isInverted ? PieceItem::whitePiece : PieceItem::blackPiece; newP = new PieceItem; @@ -184,7 +184,7 @@ void GameController::gameReset() newP->setNum(i + 1); // 如果重复三连不可用,则显示棋子序号,九连棋专用玩法 - if (!(position_.getRule()->allowRemovePiecesRepeatedly)) + if (!(game_.getRule()->allowRemovePiecesRepeatedly)) newP->setShowNum(true); pieceList.append(newP); @@ -198,7 +198,7 @@ void GameController::gameReset() newP->setNum(i + 1); // 如果重复三连不可用,则显示棋子序号,九连棋专用玩法 - if (!(position_.getRule()->allowRemovePiecesRepeatedly)) + if (!(game_.getRule()->allowRemovePiecesRepeatedly)) newP->setShowNum(true); pieceList.append(newP); @@ -206,7 +206,7 @@ void GameController::gameReset() } // 读取规则限时要求 - timeLimit = position_.getRule()->maxTimeLedToLose; + timeLimit = game_.getRule()->maxTimeLedToLose; // 如果规则不要求计时,则time1和time2表示已用时间 if (timeLimit <= 0) { @@ -220,7 +220,7 @@ void GameController::gameReset() // 更新棋谱 manualListModel.removeRows(0, manualListModel.rowCount()); manualListModel.insertRow(0); - manualListModel.setData(manualListModel.index(0), position_.getCmdLine()); + manualListModel.setData(manualListModel.index(0), game_.getCmdLine()); currentRow = 0; // 发出信号通知主窗口更新LCD显示 @@ -229,13 +229,13 @@ void GameController::gameReset() emit time2Changed(qtime.toString("hh:mm:ss")); // 发信号更新状态栏 - message = QString::fromStdString(position_.getTips()); + message = QString::fromStdString(game_.getTips()); emit statusBarChanged(message); // 更新比分 LCD 显示 - emit score1Changed(QString::number(position_.score[1], 10)); - emit score2Changed(QString::number(position_.score[2], 10)); - emit scoreDrawChanged(QString::number(position_.score_draw, 10)); + emit score1Changed(QString::number(game_.score[1], 10)); + emit score2Changed(QString::number(game_.score[2], 10)); + emit scoreDrawChanged(QString::number(game_.score_draw, 10)); // 播放音效 //playSound(":/sound/resources/sound/newgame.wav"); @@ -280,8 +280,8 @@ void GameController::setRule(int ruleNo, step_t stepLimited /*= -1*/, int timeLi } // 设置模型规则,重置游戏 - position_.setContext(&RULES[ruleNo], stepsLimit, timeLimit); - dummyGame = position_; + game_.setContext(&RULES[ruleNo], stepsLimit, timeLimit); + dummyGame = game_; // 重置游戏 gameReset(); @@ -289,12 +289,12 @@ void GameController::setRule(int ruleNo, step_t stepLimited /*= -1*/, int timeLi void GameController::setEngine(int id, bool arg) { - position_.configure(giveUpIfMostLose_, randomMove_); + game_.configure(giveUpIfMostLose_, randomMove_); isAiPlayer[id] = arg; if (arg) { - ai[id]->setAi(position_); + ai[id]->setAi(game_); if (ai[id]->isRunning()) ai[id]->resume(); else @@ -325,8 +325,8 @@ void GameController::setAiDepthTime(depth_t depth1, int time1, depth_t depth2, i ai[2]->wait(); } - ai[1]->setAi(position_, depth1, time1); - ai[2]->setAi(position_, depth2, time2); + ai[1]->setAi(game_, depth1, time1); + ai[2]->setAi(game_, depth2, time2); if (isAiPlayer[1]) { ai[1]->start(); @@ -398,13 +398,13 @@ void GameController::flip() ai[2]->wait(); } - position_.context.board.mirror(position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation); - position_.context.board.rotate(180, position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation); - dummyGame = position_; + game_.context.board.mirror(game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation); + game_.context.board.rotate(180, game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation); + dummyGame = game_; // 更新棋谱 int row = 0; - for (const auto &str : *(position_.getCmdList())) { + for (const auto &str : *(game_.getCmdList())) { manualListModel.setData(manualListModel.index(row++), str.c_str()); } @@ -414,8 +414,8 @@ void GameController::flip() else phaseChange(currentRow, true); - ai[1]->setAi(position_); - ai[2]->setAi(position_); + ai[1]->setAi(game_); + ai[2]->setAi(game_); if (isAiPlayer[1]) { ai[1]->start(); @@ -438,13 +438,13 @@ void GameController::mirror() ai[2]->wait(); } - position_.context.board.mirror(position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation); - dummyGame = position_; + game_.context.board.mirror(game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation); + dummyGame = game_; // 更新棋谱 int row = 0; - for (const auto &str : *(position_.getCmdList())) { + for (const auto &str : *(game_.getCmdList())) { manualListModel.setData(manualListModel.index(row++), str.c_str()); } @@ -456,8 +456,8 @@ void GameController::mirror() else phaseChange(currentRow, true); - ai[1]->setAi(position_); - ai[2]->setAi(position_); + ai[1]->setAi(game_); + ai[2]->setAi(game_); if (isAiPlayer[1]) { ai[1]->start(); @@ -480,13 +480,13 @@ void GameController::turnRight() ai[2]->wait(); } - position_.context.board.rotate(-90, position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation); - dummyGame = position_; + game_.context.board.rotate(-90, game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation); + dummyGame = game_; // 更新棋谱 int row = 0; - for (const auto &str : *(position_.getCmdList())) { + for (const auto &str : *(game_.getCmdList())) { manualListModel.setData(manualListModel.index(row++), str.c_str()); } @@ -496,8 +496,8 @@ void GameController::turnRight() else phaseChange(currentRow, true); - ai[1]->setAi(position_); - ai[2]->setAi(position_); + ai[1]->setAi(game_); + ai[2]->setAi(game_); if (isAiPlayer[1]) { ai[1]->start(); @@ -520,20 +520,20 @@ void GameController::turnLeft() ai[2]->wait(); } - position_.context.board.rotate(90, position_.cmdlist, position_.cmdline, position_.move_, position_.currentRule, position_.currentLocation); - dummyGame = position_; + game_.context.board.rotate(90, game_.cmdlist, game_.cmdline, game_.move_, game_.currentRule, game_.currentLocation); + dummyGame = game_; // 更新棋谱 int row = 0; - for (const auto &str : *(position_.getCmdList())) { + for (const auto &str : *(game_.getCmdList())) { manualListModel.setData(manualListModel.index(row++), str.c_str()); } // 刷新显示 updateScence(); - ai[1]->setAi(position_); - ai[2]->setAi(position_); + ai[1]->setAi(game_); + ai[2]->setAi(game_); if (isAiPlayer[1]) { ai[1]->start(); } @@ -548,7 +548,7 @@ void GameController::timerEvent(QTimerEvent *event) static QTime qt1, qt2; // 玩家的已用时间 - position_.getElapsedTime(remainingTime[1], remainingTime[2]); + game_.getElapsedTime(remainingTime[1], remainingTime[2]); // 如果规则要求计时,则time1和time2表示倒计时 if (timeLimit > 0) { @@ -564,7 +564,7 @@ void GameController::timerEvent(QTimerEvent *event) emit time2Changed(qt2.toString("hh:mm:ss")); // 如果胜负已分 - if (position_.whoWin() != PLAYER_NOBODY) { + if (game_.whoWin() != PLAYER_NOBODY) { // 停止计时 killTimer(timeID); @@ -572,7 +572,7 @@ void GameController::timerEvent(QTimerEvent *event) timeID = 0; // 发信号更新状态栏 - message = QString::fromStdString(position_.getTips()); + message = QString::fromStdString(game_.getTips()); emit statusBarChanged(message); // 弹框 @@ -609,7 +609,7 @@ void GameController::timerEvent(QTimerEvent *event) bool GameController::isAIsTurn() { - return isAiPlayer[position_.context.turnId]; + return isAiPlayer[game_.context.turnId]; } // 关键槽函数,根据QGraphicsScene的信号和状态来执行选子、落子或去子 @@ -642,17 +642,17 @@ bool GameController::actionPiece(QPointF pos) if (QMessageBox::Ok == msgBox.exec()) { #endif /* !MOBILE_APP_UI */ - position_ = dummyGame; + game_ = dummyGame; manualListModel.removeRows(currentRow + 1, manualListModel.rowCount() - currentRow - 1); // 如果再决出胜负后悔棋,则重新启动计时 - if (position_.whoWin() == PLAYER_NOBODY) { + if (game_.whoWin() == PLAYER_NOBODY) { // 重新启动计时 timeID = startTimer(100); // 发信号更新状态栏 - message = QString::fromStdString(position_.getTips()); + message = QString::fromStdString(game_.getTips()); emit statusBarChanged(message); #ifndef MOBILE_APP_UI } @@ -663,7 +663,7 @@ bool GameController::actionPiece(QPointF pos) } // 如果未开局则开局 - if (position_.getPhase() == PHASE_NOTSTARTED) + if (game_.getPhase() == PHASE_NOTSTARTED) gameStart(); // 判断执行选子、落子或去子 @@ -671,10 +671,10 @@ bool GameController::actionPiece(QPointF pos) PieceItem *piece = nullptr; QGraphicsItem *item = scene.itemAt(pos, QTransform()); - switch (position_.getAction()) { + switch (game_.getAction()) { case ACTION_PLACE: - if (position_._place(r, s)) { - if (position_.getAction() == ACTION_CAPTURE) { + if (game_._place(r, s)) { + if (game_.getAction() == ACTION_CAPTURE) { // 播放成三音效 playSound(":/sound/resources/sound/capture.wav"); } else { @@ -692,7 +692,7 @@ bool GameController::actionPiece(QPointF pos) piece = qgraphicsitem_cast(item); if (!piece) break; - if (position_.choose(r, s)) { + if (game_.choose(r, s)) { // 播放选子音效 playSound(":/sound/resources/sound/choose.wav"); result = true; @@ -703,7 +703,7 @@ bool GameController::actionPiece(QPointF pos) break; case ACTION_CAPTURE: - if (position_._capture(r, s)) { + if (game_._capture(r, s)) { // 播放音效 playSound(":/sound/resources/sound/remove.wav"); result = true; @@ -720,7 +720,7 @@ bool GameController::actionPiece(QPointF pos) if (result) { // 发信号更新状态栏 - message = QString::fromStdString(position_.getTips()); + message = QString::fromStdString(game_.getTips()); emit statusBarChanged(message); // 将新增的棋谱行插入到ListModel @@ -728,7 +728,7 @@ bool GameController::actionPiece(QPointF pos) int k = 0; // 输出命令行 - for (const auto & i : *(position_.getCmdList())) { + for (const auto & i : *(game_.getCmdList())) { // 跳过已添加的,因标准list容器没有下标 if (k++ <= currentRow) continue; @@ -738,16 +738,16 @@ bool GameController::actionPiece(QPointF pos) // 播放胜利或失败音效 #ifndef DONOT_PLAY_WIN_SOUND - if (position_.whoWin() != PLAYER_NOBODY && + if (game_.whoWin() != PLAYER_NOBODY && (manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over.")) playSound(":/sound/resources/sound/win.wav"); #endif // AI设置 - if (&position_ == &(this->position_)) { + if (&game_ == &(this->game_)) { // 如果还未决出胜负 - if (position_.whoWin() == PLAYER_NOBODY) { - if (position_.context.turn == PLAYER_1) { + if (game_.whoWin() == PLAYER_NOBODY) { + if (game_.context.turn == PLAYER_1) { if (isAiPlayer[1]) { ai[1]->resume(); } @@ -767,7 +767,7 @@ bool GameController::actionPiece(QPointF pos) ai[2]->stop(); // 弹框 - //message = QString::fromStdString(position_.getTips()); + //message = QString::fromStdString(game_.getTips()); //QMessageBox::about(NULL, "游戏结果", message); } } @@ -779,7 +779,7 @@ bool GameController::actionPiece(QPointF pos) bool GameController::giveUp() { - bool result = position_.giveup(position_.context.turn); + bool result = game_.giveup(game_.context.turn); if (!result) { return false; @@ -790,7 +790,7 @@ bool GameController::giveUp() int k = 0; // 输出命令行 - for (const auto & i : *(position_.getCmdList())) { + for (const auto & i : *(game_.getCmdList())) { // 跳过已添加的,因标准list容器没有下标 if (k++ <= currentRow) continue; @@ -798,7 +798,7 @@ bool GameController::giveUp() manualListModel.setData(manualListModel.index(currentRow), i.c_str()); } - if (position_.whoWin() != PLAYER_NOBODY) + if (game_.whoWin() != PLAYER_NOBODY) playSound(":/sound/resources/sound/loss.wav"); return result; @@ -819,7 +819,7 @@ bool GameController::command(const QString &cmd, bool update /* = true */) // 声音 QString sound; - switch (position_.getAction()) { + switch (game_.getAction()) { case ACTION_CHOOSE: case ACTION_PLACE: sound = ":/sound/resources/sound/drog.wav"; @@ -832,44 +832,44 @@ bool GameController::command(const QString &cmd, bool update /* = true */) } // 如果未开局则开局 - if (position_.getPhase() == PHASE_NOTSTARTED) { + if (game_.getPhase() == PHASE_NOTSTARTED) { gameStart(); } - if (!position_.command(cmd.toStdString().c_str())) + if (!game_.command(cmd.toStdString().c_str())) return false; - if (sound == ":/sound/resources/sound/drog.wav" && position_.getAction() == ACTION_CAPTURE) { + if (sound == ":/sound/resources/sound/drog.wav" && game_.getAction() == ACTION_CAPTURE) { sound = ":/sound/resources/sound/capture.wav"; } if (update) { playSound(sound); - updateScence(position_); + updateScence(game_); } // 发信号更新状态栏 - message = QString::fromStdString(position_.getTips()); + message = QString::fromStdString(game_.getTips()); emit statusBarChanged(message); // 对于新开局 - if (position_.getCmdList()->size() <= 1) { + if (game_.getCmdList()->size() <= 1) { manualListModel.removeRows(0, manualListModel.rowCount()); manualListModel.insertRow(0); - manualListModel.setData(manualListModel.index(0), position_.getCmdLine()); + manualListModel.setData(manualListModel.index(0), game_.getCmdLine()); currentRow = 0; } // 对于当前局 else { currentRow = manualListModel.rowCount() - 1; // 跳过已添加行,迭代器不支持+运算符,只能一个个++ - auto i = (position_.getCmdList()->begin()); - for (int r = 0; i != (position_.getCmdList())->end(); i++) { + auto i = (game_.getCmdList()->begin()); + for (int r = 0; i != (game_.getCmdList())->end(); i++) { if (r++ > currentRow) break; } // 将新增的棋谱行插入到ListModel - while (i != position_.getCmdList()->end()) { + while (i != game_.getCmdList()->end()) { manualListModel.insertRow(++currentRow); manualListModel.setData(manualListModel.index(currentRow), (*i++).c_str()); } @@ -877,17 +877,17 @@ bool GameController::command(const QString &cmd, bool update /* = true */) // 播放胜利或失败音效 #ifndef DONOT_PLAY_WIN_SOUND - if (position_.whoWin() != PLAYER_NOBODY && + if (game_.whoWin() != PLAYER_NOBODY && (manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over.")) { playSound(":/sound/resources/sound/win.wav"); } #endif // AI设置 - if (&position_ == &(this->position_)) { + if (&game_ == &(this->game_)) { // 如果还未决出胜负 - if (position_.whoWin() == PLAYER_NOBODY) { - if (position_.context.turn == PLAYER_1) { + if (game_.whoWin() == PLAYER_NOBODY) { + if (game_.context.turn == PLAYER_1) { if (isAiPlayer[1]) { ai[1]->resume(); } @@ -920,7 +920,7 @@ bool GameController::command(const QString &cmd, bool update /* = true */) #ifdef MESSAGEBOX_ENABLE // 弹框 - message = QString::fromStdString(position_.getTips()); + message = QString::fromStdString(game_.getTips()); QMessageBox::about(NULL, "游戏结果", message); #endif } @@ -957,7 +957,7 @@ bool GameController::phaseChange(int row, bool forceUpdate) } // 下面这步关键,会让悔棋者承担时间损失 - dummyGame.setStartTime(position_.getStartTimeb()); + dummyGame.setStartTime(game_.getStartTimeb()); // 刷新棋局场景 updateScence(dummyGame); @@ -967,7 +967,7 @@ bool GameController::phaseChange(int row, bool forceUpdate) bool GameController::updateScence() { - return updateScence(position_); + return updateScence(game_); } bool GameController::updateScence(Game &game) diff --git a/src/ui/qt/gamecontroller.h b/src/ui/qt/gamecontroller.h index 9a8274ee..379cbdbd 100644 --- a/src/ui/qt/gamecontroller.h +++ b/src/ui/qt/gamecontroller.h @@ -203,7 +203,7 @@ protected: private: // 棋对象的数据模型 - Game position_; + Game game_; // 棋对象的数据模型(临时) Game dummyGame;