diff --git a/qml/2048.js b/qml/2048.js index d137aaf..88e32cc 100644 --- a/qml/2048.js +++ b/qml/2048.js @@ -45,7 +45,7 @@ var labelFunc = { function startupFunction() { // Initialize variables - score = 0; + score = Number(settings.value("currentScore", 0)); checkTargetFlag = true; var i; var j; @@ -65,9 +65,29 @@ function startupFunction() { tileItems[i] = null; } - updateAvailableCells(); - createNewTileItems(true); - updateScore(0); + if (settings.contains("currentCellValues")) { + console.log("Reading saved cell values"); + var cells = settings.list("currentCellValues"); + for(i = 0; i < gridSize; i++) { + for(j = 0; j < gridSize; j++) { + cellValues[i][j] = Number(cells[i*gridSize + j]); + if(cellValues[i][j] !== 0) { + tileItems[i*gridSize + j] = createTileObject(i*gridSize + j, cellValues[i][j], true); + } + if(cellValues[i][j] >= targetLevel) + { + checkTargetFlag = false; + } + } + } + updateAvailableCells(); + } else { + console.log("No board saved, starting empty"); + updateAvailableCells(); + createNewTileItems(true); + } + updateScore(score); + addScoreText.parent = scoreBoard.itemAt(0); // Save the currently achieved best score @@ -462,12 +482,37 @@ function moveMergeTilesUpDown(i, v, v2, indices, up) { } } -function cleanUpAndQuit() { +function clearCurrentCellValuesSetting() { + console.log("Removing cells values setting..."); + settings.remove("currentCellValues"); + settings.remove("currentScore"); +} + +function saveSettings() { + if (isDead()) { + clearCurrentCellsSetting(); + } else { + console.log("Saving cell values..."); + var cells = []; + for(x = 0; x < gridSize; ++x) { + for(y = 0; y < gridSize; ++y) { + cells[x*gridSize + y] = cellValues[x][y]; + } + } + settings.setList("currentCellValues", cells); + settings.setValue("currentScore", score); + } + if (bestScore > settings.value("bestScore", 0)) { console.log("Updating new high score..."); settings.setValue("bestScore", bestScore); } if (label !== settings.value("label", "2048")) settings.setValue("label", label); +} + +function cleanUpAndQuit() { + console.log("Quitting..."); + saveSettings(); Qt.quit(); } diff --git a/qml/main.qml b/qml/main.qml index 6bbd8a6..ef1db98 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -25,7 +25,10 @@ ApplicationWindow { MenuItem { text: qsTr("New Game") shortcut: "Ctrl+N" - onTriggered: MyScript.startupFunction(); + onTriggered: { + MyScript.clearCurrentCellValuesSetting() + MyScript.startupFunction(); + } } MenuItem { text: qsTr("Exit") @@ -318,7 +321,10 @@ ApplicationWindow { } } } - onClicked: MyScript.startupFunction() + onClicked: { + MyScript.clearCurrentCellValuesSetting() + MyScript.startupFunction() + } } Rectangle { @@ -366,6 +372,7 @@ ApplicationWindow { text: qsTr("Game Over!") standardButtons: StandardButton.Retry | StandardButton.Abort onAccepted: { + MyScript.clearCurrentCellValuesSetting() MyScript.startupFunction(); } onRejected: MyScript.cleanUpAndQuit(); @@ -390,4 +397,5 @@ ApplicationWindow { } Component.onCompleted: MyScript.startupFunction(); + Component.onDestruction: MyScript.saveSettings(); } diff --git a/src/settings.cpp b/src/settings.cpp index 9d556aa..a7b1b61 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -12,6 +12,10 @@ bool Settings::contains(const QString & key) const { return settings_->contains(key); } +void Settings::remove(const QString & key) { + settings_->remove(key); +} + void Settings::setValue(const QString &key, const QVariant &value) { settings_->setValue(key, value); } @@ -20,6 +24,16 @@ QVariant Settings::value(const QString &key, const QVariant &defaultValue) const return settings_->value(key, defaultValue); } +void Settings::setList(const QString &key, const QVariantList &board) +{ + settings_->setValue(key, board); +} + +QVariantList Settings::list(const QString &key) const +{ + return settings_->value(key).toList(); +} + void Settings::setVersion(QString version) { appVersion = version; } diff --git a/src/settings.h b/src/settings.h index 7a8ef02..6accd0b 100644 --- a/src/settings.h +++ b/src/settings.h @@ -13,10 +13,14 @@ class Settings : public QObject Q_INVOKABLE void setValue(const QString &key, const QVariant &value); Q_INVOKABLE QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const; - Q_INVOKABLE QString getVersion(); + Q_INVOKABLE void setList(const QString &key, const QVariantList &board); + Q_INVOKABLE QVariantList list(const QString &key) const; void setVersion(const QString version); - bool contains(const QString & key) const; + Q_INVOKABLE QString getVersion(); + + Q_INVOKABLE bool contains(const QString & key) const; + Q_INVOKABLE void remove(const QString & key); signals: