qt: Save settings to settings.ini

Do not resetAiPlayers when autoRestart.
This commit is contained in:
Calcitem 2021-01-23 22:51:04 +08:00
parent c7a6a58c91
commit 8ae49592c7
5 changed files with 87 additions and 6 deletions

3
.gitignore vendored
View File

@ -342,6 +342,9 @@ plyInfo.dat
preCalculatedVars.dat preCalculatedVars.dat
shortKnotValue.dat shortKnotValue.dat
# Settings
settings.ini
# Executable files # Executable files
src/fishmill src/fishmill

View File

@ -70,6 +70,11 @@ public:
shufflingEnabled = enabled; shufflingEnabled = enabled;
} }
bool getLearnEndgameEnabled() const noexcept
{
return learnEndgame;
}
void setLearnEndgameEnabled(bool enabled) noexcept void setLearnEndgameEnabled(bool enabled) noexcept
{ {
#ifdef ENDGAME_LEARNING_FORCE #ifdef ENDGAME_LEARNING_FORCE

View File

@ -29,6 +29,7 @@
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QParallelAnimationGroup> #include <QParallelAnimationGroup>
#include <QDir> #include <QDir>
#include <QFileInfo>
#include <QThread> #include <QThread>
#include <iomanip> #include <iomanip>
@ -109,6 +110,35 @@ Game::Game(
#endif #endif
moveHistory.reserve(256); moveHistory.reserve(256);
loadSettings();
}
void Game::loadSettings()
{
bool empty = false;
QFileInfo file(SETTINGS_FILE);
if (!file.exists()) {
cout << SETTINGS_FILE.toStdString() << " is not exists, create it." << endl;
empty = true;
}
settings = new QSettings(SETTINGS_FILE, QSettings::IniFormat);
setEngineBlack(empty? false : settings->value("Options/blackIsAiPlayer").toBool());
setEngineWhite(empty ? true : settings->value("Options/whiteIsAiPlayer").toBool());
setSound(empty ? true : settings->value("Options/Sound").toBool());
setShuffling(empty ? true : settings->value("Options/Shuffling").toBool());
setRule(empty ? DEFAULT_RULE_NUMBER : settings->value("Options/ruleNo").toInt());
setResignIfMostLose(empty ? false : settings->value("Options/ResignIfMostLose").toBool());
setOpeningBook(empty ? false : settings->value("Options/OpeningBook").toBool());
setLearnEndgame(empty ? false : settings->value("Options/LearnEndgameEnabled").toBool());
setIDS(empty ? false : settings->value("Options/IDS").toBool());
setDepthExtension(empty ? true : settings->value("Options/DepthExtension").toBool());
setAutoRestart(empty ? false : settings->value("Options/AutoRestart").toBool());
setAutoChangeFirstMove(empty ? false : settings->value("Options/AutoChangeFirstMove").toBool());
setAnimation(empty ? true : settings->value("Options/Animation").toBool());
} }
Game::~Game() Game::~Game()
@ -126,6 +156,9 @@ Game::~Game()
#endif /* ENDGAME_LEARNING */ #endif /* ENDGAME_LEARNING */
moveHistory.clear(); moveHistory.clear();
delete settings;
settings = nullptr;
} }
const map<int, QStringList> Game::getActions() const map<int, QStringList> Game::getActions()
@ -205,7 +238,7 @@ void Game::gameReset()
// Stop threads // Stop threads
if (!gameOptions.getAutoRestart()) { if (!gameOptions.getAutoRestart()) {
pauseThreads(); pauseThreads();
resetAiPlayers(); //resetAiPlayers();
} }
// Clear pieces // Clear pieces
@ -353,6 +386,8 @@ void Game::setRule(int ruleNo, int stepLimited /*= -1*/, int timeLimited /*= -1*
// Reset game // Reset game
gameReset(); gameReset();
settings->setValue("Options/ruleNo", ruleNo);
} }
void Game::setEngine(Color color, bool enabled) void Game::setEngine(Color color, bool enabled)
@ -371,11 +406,13 @@ void Game::setEngine(Color color, bool enabled)
void Game::setEngineBlack(bool enabled) void Game::setEngineBlack(bool enabled)
{ {
setEngine(BLACK, enabled); setEngine(BLACK, enabled);
settings->setValue("Options/blackIsAiPlayer", enabled);
} }
void Game::setEngineWhite(bool enabled) void Game::setEngineWhite(bool enabled)
{ {
setEngine(WHITE, enabled); setEngine(WHITE, enabled);
settings->setValue("Options/whiteIsAiPlayer", enabled);
} }
void Game::setAiDepthTime(int time1, int time2) void Game::setAiDepthTime(int time1, int time2)
@ -403,11 +440,14 @@ void Game::setAnimation(bool arg) noexcept
durationTime = 500; durationTime = 500;
else else
durationTime = 0; durationTime = 0;
settings->setValue("Options/Animation", arg);
} }
void Game::setSound(bool arg) noexcept void Game::setSound(bool arg) noexcept
{ {
hasSound = arg; hasSound = arg;
settings->setValue("Options/Sound", arg);
} }
void Game::playSound(GameSound soundType, Color c) void Game::playSound(GameSound soundType, Color c)
@ -515,26 +555,31 @@ void Game::playSound(GameSound soundType, Color c)
void Game::setResignIfMostLose(bool enabled) void Game::setResignIfMostLose(bool enabled)
{ {
gameOptions.setResignIfMostLose(enabled); gameOptions.setResignIfMostLose(enabled);
settings->setValue("Options/ResignIfMostLose", enabled);
} }
void Game::setAutoRestart(bool enabled) void Game::setAutoRestart(bool enabled)
{ {
gameOptions.setAutoRestart(enabled); gameOptions.setAutoRestart(enabled);
settings->setValue("Options/AutoRestart", enabled);
} }
void Game::setAutoChangeFirstMove(bool enabled) void Game::setAutoChangeFirstMove(bool enabled)
{ {
gameOptions.setAutoChangeFirstMove(enabled); gameOptions.setAutoChangeFirstMove(enabled);
settings->setValue("Options/AutoChangeFirstMove", enabled);
} }
void Game::setShuffling(bool enabled) void Game::setShuffling(bool enabled)
{ {
gameOptions.setShufflingEnabled(enabled); gameOptions.setShufflingEnabled(enabled);
settings->setValue("Options/Shuffling", enabled);
} }
void Game::setLearnEndgame(bool enabled) void Game::setLearnEndgame(bool enabled)
{ {
gameOptions.setLearnEndgameEnabled(enabled); gameOptions.setLearnEndgameEnabled(enabled);
settings->setValue("Options/LearnEndgameEnabled", enabled);
#ifdef ENDGAME_LEARNING #ifdef ENDGAME_LEARNING
if (gameOptions.isEndgameLearningEnabled()) { if (gameOptions.isEndgameLearningEnabled()) {
@ -546,18 +591,21 @@ void Game::setLearnEndgame(bool enabled)
void Game::setIDS(bool enabled) void Game::setIDS(bool enabled)
{ {
gameOptions.setIDSEnabled(enabled); gameOptions.setIDSEnabled(enabled);
settings->setValue("Options/IDS", enabled);
} }
// DepthExtension // DepthExtension
void Game::setDepthExtension(bool enabled) void Game::setDepthExtension(bool enabled)
{ {
gameOptions.setDepthExtension(enabled); gameOptions.setDepthExtension(enabled);
settings->setValue("Options/DepthExtension", enabled);
} }
// OpeningBook // OpeningBook
void Game::setOpeningBook(bool enabled) void Game::setOpeningBook(bool enabled)
{ {
gameOptions.setOpeningBook(enabled); gameOptions.setOpeningBook(enabled);
settings->setValue("Options/OpeningBook", enabled);
} }
void Game::flip() void Game::flip()

View File

@ -35,6 +35,7 @@
#include <QTextStream> #include <QTextStream>
#include <QStringListModel> #include <QStringListModel>
#include <QModelIndex> #include <QModelIndex>
#include <QSettings>
#include "position.h" #include "position.h"
#include "gamescene.h" #include "gamescene.h"
@ -411,6 +412,20 @@ private:
bool isInverted; bool isInverted;
public: public:
const QString SETTINGS_FILE = "settings.ini";
QSettings *settings {nullptr};
void loadSettings();
bool soundEnabled()
{
return hasSound;
}
bool animationEnabled()
{
return hasAnimation;
}
// True when the computer takes the lead // True when the computer takes the lead
bool isAiPlayer[COLOR_NB]; bool isAiPlayer[COLOR_NB];

View File

@ -306,10 +306,8 @@ void MillGameWindow::initialize()
connect(game, SIGNAL(statusBarChanged(QString)), connect(game, SIGNAL(statusBarChanged(QString)),
statusBarlabel, SLOT(setText(QString))); statusBarlabel, SLOT(setText(QString)));
ruleNo = DEFAULT_RULE_NUMBER; ruleActionList[game->getRuleIndex()]->setChecked(true);
ruleActionList[ruleNo]->setChecked(true); game->setRule(game->getRuleIndex());
game->setRule(ruleNo);
// Update rule display // Update rule display
ruleInfo(); ruleInfo();
@ -391,7 +389,19 @@ void MillGameWindow::initialize()
connect(ui.pushButton_option, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ctxMenu(const QPoint &))); connect(ui.pushButton_option, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ctxMenu(const QPoint &)));
#endif /* MOBILE_APP_UI */ #endif /* MOBILE_APP_UI */
ui.actionEngine2_R->setChecked(true); ui.actionEngine1_T->setChecked(game->isAiPlayer[BLACK]);
ui.actionEngine2_R->setChecked(game->isAiPlayer[WHITE]);
ui.actionSound_S->setChecked(game->soundEnabled());
ui.actionAnimation_A->setChecked(game->animationEnabled());
ui.actionShuffling_R->setChecked(gameOptions.getShufflingEnabled());
ui.actionIDS_I->setChecked(gameOptions.getIDSEnabled());
ui.actionDepthExtension_D->setChecked(gameOptions.getDepthExtension());
ui.actionResignIfMostLose_G->setChecked(gameOptions.getResignIfMostLose());
ui.actionAutoRestart_A->setChecked(gameOptions.getAutoRestart());
ui.actionOpeningBook_O->setChecked(gameOptions.getOpeningBook());
ui.actionLearnEndgame_E->setChecked(gameOptions.getLearnEndgameEnabled());
} }
#ifdef MOBILE_APP_UI #ifdef MOBILE_APP_UI