Skip to content
Snippets Groups Projects
Select Git revision
  • 56ca9b3d2378cf333fae964eee46d26d46af12ac
  • main default protected
  • version_stable
  • quentin
4 results

game2048.cpp

Blame
  • game2048.cpp 1.82 KiB
    #include "game2048.h"
    #include <QRandomGenerator>
    
    Game2048::Game2048(QObject *parent)
        : QObject(parent), m_score(0) {
        resetGame();
    }
    
    void Game2048::resetGame() {
        m_score = 0;
    
        // Vider le plateau
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                m_board[i][j] = 0;
    
        // Ajouter deux tuiles initiales
        spawnTile();
        spawnTile();
    
        emit boardChanged();
        emit scoreChanged();
    }
    
    int Game2048::score() const {
        return m_score;
    }
    
    QVector<int> Game2048::board() const {
        QVector<int> flatBoard;
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                flatBoard.append(m_board[i][j]);
        return flatBoard;
    }
    
    void Game2048::spawnTile() {
        QVector<QPair<int,int>> emptyTiles;
        for (int row = 0; row < 4; row++) {
            for (int col = 0; col < 4; col++) {
                if (m_board[row][col] == 0)
                    flatBoard.append(row * 4 + col);
            }
        }
        if (!flatBoard.isEmpty()) {
            int randIndex = QRandomGenerator::global()->bounded(flatBoard.size());
            int index = flatBoard[randIndex];
            int row = index / 4;
            int col = index % 4;
            m_board[row][col] = (QRandomGenerator::global()->bounded(10) == 0) ? 4 : 2;
        }
    }
    
    bool Game2048::moveLeft() {
        bool moved = false;
        // Implémentation à venir
        return moved;
    }
    
    bool Game2048::moveRight() {
        bool moved = false;
        // Implémentation à venir
        return moved;
    }
    
    bool Game2048::moveUp() {
        bool moved = false;
        // Implémentation à venir
        return moved;
    }
    
    bool Game2048::moveDown() {
        bool moved = false;
        // Implémentation à venir
        return moved;
    }
    
    void Game2048::updateGameState() {
        if (!canMove()) {
            emit gameOver();
        }
    }
    
    bool Game2048::canMove() const {
        // Implémentation basique à compléter
        return true;
    }