diff --git a/applicationQT2048/gamemanager.cpp b/applicationQT2048/gamemanager.cpp index be6a6aaf828402e45102b0ba2744ae91badd4846..810fd89d94781d8ca71af41fb8c4b1f1b579bdce 100644 --- a/applicationQT2048/gamemanager.cpp +++ b/applicationQT2048/gamemanager.cpp @@ -47,6 +47,7 @@ void GameManager::moveLeft() { //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } void GameManager::moveRight() { @@ -88,6 +89,7 @@ void GameManager::moveRight() { //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } @@ -136,6 +138,7 @@ void GameManager::moveUp() { //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } void GameManager::moveDown() { @@ -181,6 +184,7 @@ void GameManager::moveDown() { //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } @@ -219,6 +223,7 @@ void GameManager::restartGame() { //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } @@ -242,7 +247,7 @@ void GameManager::addRandomElement() { int row = emptyCells[randomIndex].first; int col = emptyCells[randomIndex].second; - // Placer un '2' dans cette case + // Placer un '2'ou un '4' dans cette case int randValue = std::rand() % 100; if (randValue < 75) { grid[row][col] = 2; @@ -314,6 +319,7 @@ void GameManager::undo() { } emit gridChanged(); // Notifiez que la grille a changé + emit calculscore(); } } @@ -382,4 +388,15 @@ void GameManager::chargerPartie(QString partieName){ file.close(); } +void GameManager::calculscore(){ + // Parcourir la grille pour additionner toute les cases + int score=0; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + score=grid[i][j]+score; + + } + } + +} diff --git a/applicationQT2048/gamemanager.h b/applicationQT2048/gamemanager.h index 445ffa2e3b7992049275727147459b1724eaa4af..8e2fd68de7092f134d37e90da2b5b67f22e5d11d 100644 --- a/applicationQT2048/gamemanager.h +++ b/applicationQT2048/gamemanager.h @@ -36,6 +36,7 @@ private: Q_INVOKABLE void addRandomElement(); Q_INVOKABLE void enregistrerPartie(QString partieName); + Q_INVOKABLE void calculscore(); QJsonArray gridToJsonArray();