From 9875c9c861c97ebc3491ea9715a97d3f259a8433 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Wed, 30 Dec 2020 00:35:18 +0800 Subject: [PATCH] refactor: Move option.cpp functions to .h and rename RandomMove to Shuffling --- gamewindow.ui | 4 +- src/mills.cpp | 2 +- src/option.cpp | 92 --------------- src/option.h | 106 +++++++++++++++--- src/ucioption.cpp | 4 +- src/ui/flutter/lib/common/config.dart | 6 +- src/ui/flutter/lib/engine/native_engine.dart | 2 +- src/ui/flutter/lib/l10n/intl_en.arb | 4 +- src/ui/flutter/lib/l10n/intl_zh.arb | 2 +- src/ui/flutter/lib/widgets/settings_page.dart | 10 +- src/ui/qt/game.cpp | 4 +- src/ui/qt/game.h | 2 +- src/ui/qt/gamewindow.cpp | 4 +- 13 files changed, 111 insertions(+), 131 deletions(-) diff --git a/gamewindow.ui b/gamewindow.ui index db2f5d29..d30f71ce 100644 --- a/gamewindow.ui +++ b/gamewindow.ui @@ -295,7 +295,7 @@ - + @@ -1211,7 +1211,7 @@ 自动重新开局(&A) - + true diff --git a/src/mills.cpp b/src/mills.cpp index 291b0ceb..d50ab6b3 100644 --- a/src/mills.cpp +++ b/src/mills.cpp @@ -286,7 +286,7 @@ void move_priority_list_shuffle() movePriorityList3 = { SQ_24, SQ_26, SQ_28, SQ_30, SQ_8, SQ_10, SQ_12, SQ_14 }; } - if (gameOptions.getRandomMoveEnabled()) { + if (gameOptions.getShufflingEnabled()) { uint32_t seed = static_cast(now()); std::shuffle(movePriorityList0.begin(), movePriorityList0.end(), std::default_random_engine(seed)); diff --git a/src/option.cpp b/src/option.cpp index b1448fee..09f63d3b 100644 --- a/src/option.cpp +++ b/src/option.cpp @@ -19,95 +19,3 @@ #include "option.h" GameOptions gameOptions; - -void GameOptions::setAutoRestart(bool enabled) -{ - isAutoRestart = enabled; -} - -bool GameOptions::getAutoRestart() -{ - return isAutoRestart; -} - -void GameOptions::setAutoChangeFirstMove(bool enabled) -{ - isAutoChangeFirstMove = enabled; -} - -bool GameOptions::getAutoChangeFirstMove() -{ - return isAutoChangeFirstMove; -} - -void GameOptions::setResignIfMostLose(bool enabled) -{ - resignIfMostLose = enabled; -} - -bool GameOptions::getResignIfMostLose() -{ - return resignIfMostLose; -} - -void GameOptions::setRandomMoveEnabled(bool enabled) -{ - randomMoveEnabled = enabled; -} - -bool GameOptions::getRandomMoveEnabled() -{ - return randomMoveEnabled; -} - -void GameOptions::setLearnEndgameEnabled(bool enabled) -{ -#ifdef ENDGAME_LEARNING_FORCE - learnEndgame = true; -#else - learnEndgame = enabled; -#endif -} - -bool GameOptions::getLearnEndgameEnabled() -{ -#ifdef ENDGAME_LEARNING_FORCE - return true; -#else - return learnEndgame; -#endif -} - -void GameOptions::setIDSEnabled(bool enabled) -{ - IDSEnabled = enabled; -} - -bool GameOptions::getIDSEnabled() -{ - return IDSEnabled; -} - -// DepthExtension - -void GameOptions::setDepthExtension(bool enabled) -{ - depthExtension = enabled; -} - -bool GameOptions::getDepthExtension() -{ - return depthExtension; -} - -// OpeningBook - -void GameOptions::setOpeningBook(bool enabled) -{ - openingBook = enabled; -} - -bool GameOptions::getOpeningBook() -{ - return openingBook; -} diff --git a/src/option.h b/src/option.h index f76b453b..25b30e2e 100644 --- a/src/option.h +++ b/src/option.h @@ -24,31 +24,103 @@ class GameOptions { public: - void setAutoRestart(bool enabled); - bool getAutoRestart(); + void setAutoRestart(bool enabled) + { + isAutoRestart = enabled; + } - void setAutoChangeFirstMove(bool enabled); - bool getAutoChangeFirstMove(); + bool getAutoRestart() + { + return isAutoRestart; + } - void setResignIfMostLose(bool enabled); - bool getResignIfMostLose(); + void setAutoChangeFirstMove(bool enabled) + { + isAutoChangeFirstMove = enabled; + } - void setRandomMoveEnabled(bool enabled); - bool getRandomMoveEnabled(); + bool getAutoChangeFirstMove() + { + return isAutoChangeFirstMove; + } - void setLearnEndgameEnabled(bool enabled); - bool getLearnEndgameEnabled(); + void setResignIfMostLose(bool enabled) + { + resignIfMostLose = enabled; + } - void setIDSEnabled(bool enabled); - bool getIDSEnabled(); + bool getResignIfMostLose() + { + return resignIfMostLose; + } + + // Specify whether the successors of a given state should be shuffled if a + // re-evaluation is required so that the AI algorithm is not favoring one + // state if multiple ones have equal evaluations. This introduces some + // variation between different games against an opponent that tries to do the + // same sequence of moves. By default, shuffling is enabled. + + bool getShufflingEnabled() + { + return shufflingEnabled; + } + + void setShufflingEnabled(bool enabled) + { + shufflingEnabled = enabled; + } + + void setLearnEndgameEnabled(bool enabled) + { +#ifdef ENDGAME_LEARNING_FORCE + learnEndgame = true; +#else + learnEndgame = enabled; +#endif + } + + bool getLearnEndgameEnabled() + { +#ifdef ENDGAME_LEARNING_FORCE + return true; +#else + return learnEndgame; +#endif + } + + void setIDSEnabled(bool enabled) + { + IDSEnabled = enabled; + } + + bool getIDSEnabled() + { + return IDSEnabled; + } // DepthExtension - void setDepthExtension(bool enabled); - bool getDepthExtension(); + + void setDepthExtension(bool enabled) + { + depthExtension = enabled; + } + + bool getDepthExtension() + { + return depthExtension; + } // OpeningBook - void setOpeningBook(bool enabled); - bool getOpeningBook(); + + void setOpeningBook(bool enabled) + { + openingBook = enabled; + } + + bool getOpeningBook() + { + return openingBook; + } protected: @@ -56,7 +128,7 @@ private: bool isAutoRestart { false }; bool isAutoChangeFirstMove { false }; bool resignIfMostLose { false }; - bool randomMoveEnabled { true }; + bool shufflingEnabled { true }; #ifdef ENDGAME_LEARNING_FORCE bool learnEndgame { true }; #else diff --git a/src/ucioption.cpp b/src/ucioption.cpp index f5fabc5c..912cc1bf 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -62,7 +62,7 @@ void on_threads(const Option &o) void on_random_move(const Option &o) { - gameOptions.setRandomMoveEnabled((bool)o); + gameOptions.setShufflingEnabled((bool)o); } // Rules @@ -153,7 +153,7 @@ void init(OptionsMap &o) o["UCI_LimitStrength"] << Option(false); o["UCI_Elo"] << Option(1350, 1350, 2850); - o["RandomMove"] << Option(true, on_random_move); + o["Shuffling"] << Option(true, on_random_move); // Rules o["nTotalPiecesEachSide"] << Option(12, 6, 12, on_nTotalPiecesEachSide); diff --git a/src/ui/flutter/lib/common/config.dart b/src/ui/flutter/lib/common/config.dart index 85bc2aa5..8001643c 100644 --- a/src/ui/flutter/lib/common/config.dart +++ b/src/ui/flutter/lib/common/config.dart @@ -30,7 +30,7 @@ class Config { static bool isAutoRestart = false; static bool isAutoChangeFirstMove = false; static bool resignIfMostLose = false; - static bool randomMoveEnabled = true; + static bool shufflingEnabled = true; static bool learnEndgame = false; static bool idsEnabled = false; static bool depthExtension = true; @@ -60,7 +60,7 @@ class Config { Config.isAutoRestart = profile['isAutoRestart'] ?? false; Config.isAutoChangeFirstMove = profile['isAutoChangeFirstMove'] ?? false; Config.resignIfMostLose = profile['resignIfMostLose'] ?? false; - Config.randomMoveEnabled = profile['randomMoveEnabled'] ?? true; + Config.shufflingEnabled = profile['shufflingEnabled'] ?? true; Config.learnEndgame = profile['learnEndgame'] ?? false; Config.idsEnabled = profile['idsEnabled'] ?? false; Config.depthExtension = profile['depthExtension'] ?? false; @@ -107,7 +107,7 @@ class Config { profile['isAutoRestart'] = Config.isAutoRestart; profile['isAutoChangeFirstMove'] = Config.isAutoChangeFirstMove; profile['resignIfMostLose'] = Config.resignIfMostLose; - profile['randomMoveEnabled'] = Config.randomMoveEnabled; + profile['shufflingEnabled'] = Config.shufflingEnabled; profile['learnEndgame'] = Config.learnEndgame; profile['idsEnabled'] = Config.idsEnabled; profile['depthExtension'] = Config.depthExtension; diff --git a/src/ui/flutter/lib/engine/native_engine.dart b/src/ui/flutter/lib/engine/native_engine.dart index 4fec77d3..e83b523b 100644 --- a/src/ui/flutter/lib/engine/native_engine.dart +++ b/src/ui/flutter/lib/engine/native_engine.dart @@ -133,7 +133,7 @@ class NativeEngine extends AiEngine { } Future setOptions() async { - await send('setoption name RandomMove value ${Config.randomMoveEnabled}'); + await send('setoption name Shuffling value ${Config.shufflingEnabled}'); await send( 'setoption name nTotalPiecesEachSide value ${Config.nTotalPiecesEachSide}'); await send('setoption name nPiecesAtLeast value ${Config.nPiecesAtLeast}'); diff --git a/src/ui/flutter/lib/l10n/intl_en.arb b/src/ui/flutter/lib/l10n/intl_en.arb index a27ddc85..e5f29119 100644 --- a/src/ui/flutter/lib/l10n/intl_en.arb +++ b/src/ui/flutter/lib/l10n/intl_en.arb @@ -283,8 +283,8 @@ "@resignIfMostLose": { "description": "AI Resign if Most Lose" }, - "randomMoveEnabled": "AI Random Move", - "@randomMoveEnabled": { + "shufflingEnabled": "AI Random Move", + "@shufflingEnabled": { "description": "AI Random Move" }, "learnEndgame": "Learn Endgame", diff --git a/src/ui/flutter/lib/l10n/intl_zh.arb b/src/ui/flutter/lib/l10n/intl_zh.arb index 81a75d1e..7c3b437a 100644 --- a/src/ui/flutter/lib/l10n/intl_zh.arb +++ b/src/ui/flutter/lib/l10n/intl_zh.arb @@ -70,7 +70,7 @@ "isAutoRestart": "棋局结束时自动重新开局", "isAutoChangeFirstMove": "开局时自动交换先后手", "resignIfMostLose": "机器明显劣势时自动认输", - "randomMoveEnabled": "机器随机走子", + "shufflingEnabled": "机器随机走子", "learnEndgame": "残局库自学习", "idsEnabled": "迭代加深", "depthExtension": "深度扩展", diff --git a/src/ui/flutter/lib/widgets/settings_page.dart b/src/ui/flutter/lib/widgets/settings_page.dart index d1d7cf3a..1e7c91fe 100644 --- a/src/ui/flutter/lib/widgets/settings_page.dart +++ b/src/ui/flutter/lib/widgets/settings_page.dart @@ -132,9 +132,9 @@ class _SettingsPageState extends State { Config.save(); } - switchRandomMoveEnabled(bool value) async { + switchShufflingEnabled(bool value) async { setState(() { - Config.randomMoveEnabled = value; + Config.shufflingEnabled = value; }); Config.save(); @@ -667,10 +667,10 @@ class _SettingsPageState extends State { _buildDivider(), SwitchListTile( activeColor: UIColors.primaryColor, - value: Config.randomMoveEnabled, + value: Config.shufflingEnabled, title: - Text(S.of(context).randomMoveEnabled, style: itemStyle), - onChanged: switchRandomMoveEnabled, + Text(S.of(context).shufflingEnabled, style: itemStyle), + onChanged: switchShufflingEnabled, ), ], ), diff --git a/src/ui/qt/game.cpp b/src/ui/qt/game.cpp index db1ec81f..6a5d7ce9 100644 --- a/src/ui/qt/game.cpp +++ b/src/ui/qt/game.cpp @@ -549,9 +549,9 @@ void Game::setAutoChangeFirstMove(bool enabled) gameOptions.setAutoChangeFirstMove(enabled); } -void Game::setRandomMove(bool enabled) +void Game::setShuffling(bool enabled) { - gameOptions.setRandomMoveEnabled(enabled); + gameOptions.setShufflingEnabled(enabled); } void Game::setLearnEndgame(bool enabled) diff --git a/src/ui/qt/game.h b/src/ui/qt/game.h index 8c355c55..8bed464f 100644 --- a/src/ui/qt/game.h +++ b/src/ui/qt/game.h @@ -239,7 +239,7 @@ public slots: void setAutoChangeFirstMove(bool enabled = false); // AI 是否随机走子 - void setRandomMove(bool enabled); + void setShuffling(bool enabled); // AI 是否记录残局库 void setLearnEndgame(bool enabled); diff --git a/src/ui/qt/gamewindow.cpp b/src/ui/qt/gamewindow.cpp index d281638e..ee1ab8fe 100644 --- a/src/ui/qt/gamewindow.cpp +++ b/src/ui/qt/gamewindow.cpp @@ -235,8 +235,8 @@ void MillGameWindow::initialize() connect(ui.actionAutoChangeFirstMove_C, SIGNAL(toggled(bool)), game, SLOT(setAutoChangeFirstMove(bool))); - connect(ui.actionRandomMove_R, SIGNAL(toggled(bool)), - game, SLOT(setRandomMove(bool))); + connect(ui.actionShuffling_R, SIGNAL(toggled(bool)), + game, SLOT(setShuffling(bool))); connect(ui.actionLearnEndgame_E, SIGNAL(toggled(bool)), game, SLOT(setLearnEndgame(bool)));