diff --git a/applicationQT2048/GamePage.qml b/applicationQT2048/GamePage.qml index ddd257bc80512d19b6efd315265b1580136195a1..c884000f4640e0ff61659050a11516f1a35ca4cd 100644 --- a/applicationQT2048/GamePage.qml +++ b/applicationQT2048/GamePage.qml @@ -118,7 +118,7 @@ FocusScope { // Permet de capter le focus pour les raccourcis clavier Text{ id: score - text:"0" + text:gameManager.score color:"white" font.bold: true font.pixelSize: 25 diff --git a/applicationQT2048/gamemanager.cpp b/applicationQT2048/gamemanager.cpp index ba90f22f8cf22bb6e5021b2a4c266692babcd0f0..317947f729203f693c9d8dfa82718fd647ec3963 100644 --- a/applicationQT2048/gamemanager.cpp +++ b/applicationQT2048/gamemanager.cpp @@ -3,11 +3,14 @@ #include <QFileInfo> #include <QDebug> -GameManager::GameManager(QObject *parent) : QObject(parent), grid(4, std::vector<int>(4, 0)) { +GameManager::GameManager(QObject *parent) : QObject(parent), grid(4, std::vector<int>(4, 0)),m_score(0), m_gameOver(false) { } void GameManager::moveLeft() { + if (m_gameOver) { // Si la partie est terminée, ne fais rien + return; + } for (int i = 0; i < 4; i++) { std::vector<int> newRow(4, 0); // Créer une nouvelle ligne vide int newPos = 0; // Position pour insérer un élément non nul (commence par le début) @@ -42,13 +45,17 @@ void GameManager::moveLeft() { grid[i] = newRow; } emit addRandomElement(); - + m_gameOver = isGameOver(); //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } void GameManager::moveRight() { + if (m_gameOver) { // Si la partie est terminée, ne fais rien + return; + } for (int i = 0; i < 4; i++) { std::vector<int> newRow(4, 0); // Créer une nouvelle ligne vide int newPos = 3; // Position pour insérer un élément non nul (commence par la fin) @@ -83,16 +90,21 @@ void GameManager::moveRight() { grid[i] = newRow; } emit addRandomElement(); + m_gameOver = isGameOver(); //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } void GameManager::moveUp() { + if (m_gameOver) { // Si la partie est terminée, ne fais rien + return; + } // Implémentation du mouvement vers le haut for (int j = 0; j < 4; j++) { std::vector<int> newCol(4, 0); // Créer une nouvelle ligne vide @@ -131,13 +143,18 @@ void GameManager::moveUp() { } emit addRandomElement(); + m_gameOver = isGameOver(); //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } void GameManager::moveDown() { + if (m_gameOver) { // Si la partie est terminée, ne fais rien + return; + } // Implémentation du mouvement vers le bas for (int j = 0; j < 4; j++) { std::vector<int> newCol(4, 0); // Créer une nouvelle ligne vide @@ -176,10 +193,12 @@ void GameManager::moveDown() { } emit addRandomElement(); + m_gameOver = isGameOver(); //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } @@ -187,10 +206,15 @@ void GameManager::restartGame(QString partieName) { //demander pour sauvergarder la partie précédente +<<<<<<< HEAD if(partieName=="false"){ }else{ enregistrerPartie(partieName); }; +======= + enregistrerPartie("partie1"); + m_gameOver = false; +>>>>>>> e22daca7a3c09bd0a515d12093b0cbf7e3e4e152 // Réinitialisation des variables @@ -221,6 +245,7 @@ void GameManager::restartGame(QString partieName) { //Mettre a jour l'état et l'enregistrer historyArray.append(gridToJsonArray()); emit gridChanged(); + emit calculscore(); } @@ -244,7 +269,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; @@ -316,6 +341,7 @@ void GameManager::undo() { } emit gridChanged(); // Notifiez que la grille a changé + emit calculscore(); } } @@ -384,6 +410,42 @@ void GameManager::chargerPartie(QString partieName){ file.close(); } +int GameManager::score() const { + return m_score; // Retourne le score actuel +} +<<<<<<< HEAD +======= +void GameManager::calculscore(){ + if (m_gameOver) { // Si la partie est terminée, ne calcule pas le score + return; + } + m_score = 0; + // Parcourir la grille pour additionner toute les cases + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + m_score+=grid[i][j]; + + } + } + emit scoreChanged(); +} + +bool GameManager::isGameOver() { + + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + if (grid[i][j] == 0) { // Une case vide est trouvée, donc le jeu n'est pas fini + return false; + } + // Vérifier les possibilités de fusion (horizontale et verticale) + if ((i < 3 && grid[i][j] == grid[i + 1][j]) || (j < 3 && grid[i][j] == grid[i][j + 1])) { + return false; + } + } + } + return true; // Si la grille est pleine et aucune fusion n'est possible, la partie est terminée +} +>>>>>>> e22daca7a3c09bd0a515d12093b0cbf7e3e4e152 diff --git a/applicationQT2048/gamemanager.h b/applicationQT2048/gamemanager.h index c89791b34aa28266b26f5c86b70c3d81848ba5b5..e1fc8d20b2fed75e5fe458ab6575f0e38874ff43 100644 --- a/applicationQT2048/gamemanager.h +++ b/applicationQT2048/gamemanager.h @@ -16,6 +16,7 @@ class GameManager : public QObject Q_PROPERTY(QVector<int> gridValues READ getGridValues NOTIFY gridChanged) Q_PROPERTY(QStringList partieHistorique READ getPartieHistorique NOTIFY historiqueChanged) + Q_PROPERTY(int score READ score NOTIFY scoreChanged) public: explicit GameManager(QObject *parent = nullptr); @@ -26,24 +27,31 @@ public: Q_INVOKABLE void restartGame(QString partieName); Q_INVOKABLE void undo(); Q_INVOKABLE void chargerPartie(QString partieName); + bool isGameOver(); QVector<int> getGridValues() const; QStringList getPartieHistorique(); + int score() const; private: std::vector<std::vector<int>> grid; QJsonArray historyArray; + int m_score; Q_INVOKABLE void addRandomElement(); Q_INVOKABLE void enregistrerPartie(QString partieName); + Q_INVOKABLE void calculscore(); QJsonArray gridToJsonArray(); + bool m_gameOver; + signals: void gridChanged(); void historiqueChanged(); + void scoreChanged(); };