From d65acac9a5ea82bf7e43e4b3618c9c2c7040f1df Mon Sep 17 00:00:00 2001 From: CalciteM Team Date: Fri, 20 Sep 2019 01:34:58 +0800 Subject: [PATCH] =?UTF-8?q?option:=20=E6=96=B0=E5=A2=9E=E6=AE=8B=E5=B1=80?= =?UTF-8?q?=E5=BA=93=E8=87=AA=E5=AD=A6=E4=B9=A0=E7=9A=84=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E5=B9=B6=E9=BB=98=E8=AE=A4=E6=89=93=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gamewindow.ui | 15 ++++++++++++++- src/ai/search.cpp | 5 +++-- src/game/option.cpp | 10 ++++++++++ src/game/option.h | 6 ++++++ src/game/position.cpp | 5 ++++- src/ui/qt/gamecontroller.cpp | 9 ++++++++- src/ui/qt/gamecontroller.h | 3 +++ src/ui/qt/gamewindow.cpp | 3 +++ 8 files changed, 51 insertions(+), 5 deletions(-) diff --git a/gamewindow.ui b/gamewindow.ui index f32ec512..fca55cb9 100644 --- a/gamewindow.ui +++ b/gamewindow.ui @@ -227,7 +227,7 @@ 0 0 491 - 23 + 22 @@ -294,6 +294,7 @@ + @@ -1125,6 +1126,18 @@ 必败时认输(&G) + + + true + + + true + + + 残局自学习 +(&E) + + diff --git a/src/ai/search.cpp b/src/ai/search.cpp index 63b4c4dc..e1e057f0 100644 --- a/src/ai/search.cpp +++ b/src/ai/search.cpp @@ -415,7 +415,8 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no // 检索残局库 Endgame endgame; - if (findEndgameHash(hash, endgame)) { + if (options.getLearnEndgameEnabled() && + findEndgameHash(hash, endgame)) { switch (endgame.type) { case ENDGAME_PLAYER_1_WIN: node->value = VALUE_WIN; @@ -693,7 +694,7 @@ const char* AIAlgorithm::bestMove() #ifdef ENDGAME_LEARNING // 检查是否明显劣势 - if (1) { // TODO: 替换为开关选项 + if (options.getLearnEndgameEnabled()) { bool isMostWeak = true; // 是否明显劣势 for (auto child : root->children) { diff --git a/src/game/option.cpp b/src/game/option.cpp index 18f97088..d3506ccd 100644 --- a/src/game/option.cpp +++ b/src/game/option.cpp @@ -52,3 +52,13 @@ bool Options::getRandomMoveEnabled() { return randomMoveEnabled; } + +void Options::setLearnEndgameEnabled(bool enabled) +{ + learnEndgame = enabled; +} + +bool Options::getLearnEndgameEnabled() +{ + return learnEndgame; +} diff --git a/src/game/option.h b/src/game/option.h index 2ec4635a..8239da9d 100644 --- a/src/game/option.h +++ b/src/game/option.h @@ -35,6 +35,9 @@ public: void setRandomMoveEnabled(bool enabled); bool getRandomMoveEnabled(); + + void setLearnEndgameEnabled(bool enabled); + bool getLearnEndgameEnabled(); protected: private: @@ -46,6 +49,9 @@ private: // AI 是否随机走子 bool randomMoveEnabled { true }; + + // AI 是否生成残局库 + bool learnEndgame { true }; }; extern Options options; diff --git a/src/game/position.cpp b/src/game/position.cpp index 358e6746..23d4932c 100644 --- a/src/game/position.cpp +++ b/src/game/position.cpp @@ -24,6 +24,7 @@ #include "search.h" #include "movegen.h" #include "player.h" +#include "option.h" #include "zobrist.h" Game::Game() @@ -36,7 +37,9 @@ Game::Game() #ifdef ENDGAME_LEARNING // TODO: 残局文件被加载了多次 - AIAlgorithm::loadEndgameFileToHashMap(); + if (options.getLearnEndgameEnabled()) { + AIAlgorithm::loadEndgameFileToHashMap(); + } #endif // 默认选择第1号规则,即“打三棋” diff --git a/src/ui/qt/gamecontroller.cpp b/src/ui/qt/gamecontroller.cpp index 074fc3e4..ae403dfd 100644 --- a/src/ui/qt/gamecontroller.cpp +++ b/src/ui/qt/gamecontroller.cpp @@ -97,7 +97,9 @@ GameController::~GameController() delete ai[2]; #ifdef ENDGAME_LEARNING - AIAlgorithm::recordEndgameHashMapToFile(); + if (options.getLearnEndgameEnabled()) { + AIAlgorithm::recordEndgameHashMapToFile(); + } #endif /* ENDGAME_LEARNING */ } @@ -380,6 +382,11 @@ void GameController::setRandomMove(bool enabled) options.setRandomMoveEnabled(enabled); } +void GameController::setLearnEndgame(bool enabled) +{ + options.setLearnEndgameEnabled(enabled); +} + // 上下翻转 void GameController::flip() { diff --git a/src/ui/qt/gamecontroller.h b/src/ui/qt/gamecontroller.h index 276c975a..1f159e2f 100644 --- a/src/ui/qt/gamecontroller.h +++ b/src/ui/qt/gamecontroller.h @@ -153,6 +153,9 @@ public slots: // AI 是否随机走子 void setRandomMove(bool enabled); + // AI 是否记录残局库 + void setLearnEndgame(bool enabled); + // 上下翻转 void flip(); diff --git a/src/ui/qt/gamewindow.cpp b/src/ui/qt/gamewindow.cpp index ad8f2c48..75ffa61a 100644 --- a/src/ui/qt/gamewindow.cpp +++ b/src/ui/qt/gamewindow.cpp @@ -214,6 +214,9 @@ void MillGameWindow::initialize() connect(ui.actionRandomMove_R, SIGNAL(toggled(bool)), game, SLOT(setRandomMove(bool))); + connect(ui.actionLearnEndgame_E, SIGNAL(toggled(bool)), + game, SLOT(setLearnEndgame(bool))); + // 视图上下翻转 connect(ui.actionFlip_F, &QAction::triggered, game, &GameController::flip);