diff --git a/Jeu_2048/CMakeLists.txt b/Jeu_2048/CMakeLists.txt index 5fba64af03e53978d5cb49fea52478268cd1c439..f38abf72d119d635416cfd53c95338ad09ea5c0b 100644 --- a/Jeu_2048/CMakeLists.txt +++ b/Jeu_2048/CMakeLists.txt @@ -18,9 +18,10 @@ qt_add_qml_module(appJeu_2048 QML_FILES Main.qml SOURCES bouton.h bouton.cpp - SOURCES case.h case.cpp + SOURCES case2048.h case2048.cpp QML_FILES Partie.qml QML_FILES Menu_de_Controle.qml + SOURCES game2048.h game2048.cpp ) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. diff --git a/Jeu_2048/Main.qml b/Jeu_2048/Main.qml index 41424f88973df650d0672dfdc62f670e86708de2..3b28f4f6d611f9e493bc5eb27b83616a9c70d689 100644 --- a/Jeu_2048/Main.qml +++ b/Jeu_2048/Main.qml @@ -1,8 +1,14 @@ import QtQuick Window { - width: 640 - height: 480 visible: true - title: qsTr("Hello World") + width: 400 + height: 400 + title: qsTr("2048") + + Text { + text: qsTr("Hello 2048") + anchors.centerIn: parent + font.pointSize: 24 + } } diff --git a/Jeu_2048/case.cpp b/Jeu_2048/case.cpp deleted file mode 100644 index 094b194cc96afb8200398a331142e135412d5b44..0000000000000000000000000000000000000000 --- a/Jeu_2048/case.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "case.h" - -case ::case (){ - -} diff --git a/Jeu_2048/case.h b/Jeu_2048/case.h deleted file mode 100644 index 1e3eb70ff13998da19a32c04a4e981a9ebe40410..0000000000000000000000000000000000000000 --- a/Jeu_2048/case.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef CASE_H -#define CASE_H - -class case {public: - case(); -}; - -#endif // CASE_H diff --git a/Jeu_2048/case2048.cpp b/Jeu_2048/case2048.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f55c480a51fa860dd4dd8ac24dcb95913b3ab6a --- /dev/null +++ b/Jeu_2048/case2048.cpp @@ -0,0 +1,5 @@ +#include "case2048.h" + +Case2048::Case2048 (){ + +} diff --git a/Jeu_2048/case2048.h b/Jeu_2048/case2048.h new file mode 100644 index 0000000000000000000000000000000000000000..ec25c10f59f4ac1c2acaf170035ee333f9920a9d --- /dev/null +++ b/Jeu_2048/case2048.h @@ -0,0 +1,10 @@ +#ifndef CASE2048_H +#define CASE2048_H + +class Case2048 +{ +public: + Case2048(); +}; + +#endif // CASE2048_H diff --git a/Jeu_2048/game2048.cpp b/Jeu_2048/game2048.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8427ae7a129a2d4f6d6befcae2cc83362d12864c --- /dev/null +++ b/Jeu_2048/game2048.cpp @@ -0,0 +1,87 @@ +#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; +} diff --git a/Jeu_2048/game2048.h b/Jeu_2048/game2048.h new file mode 100644 index 0000000000000000000000000000000000000000..d5f5b61a8f63d65ef30901f2a8424fa40774c267 --- /dev/null +++ b/Jeu_2048/game2048.h @@ -0,0 +1,40 @@ +#ifndef GAME2048_H +#define GAME2048_H + +#include <QObject> +#include <QVector> + +class Game2048 : public QObject { + Q_OBJECT + Q_PROPERTY(int score READ score NOTIFY scoreChanged) + Q_PROPERTY(QVector<int> board READ board NOTIFY boardChanged) + +public: + explicit Game2048(QObject *parent = nullptr); + + // Exposer méthodes QML + Q_INVOKABLE void resetGame(); + Q_INVOKABLE bool moveLeft(); + Q_INVOKABLE bool moveRight(); + Q_INVOKABLE bool moveUp(); + Q_INVOKABLE bool moveDown(); + + int score() const; + QVector<int> board() const; // Convertir tableau 2D en vecteur plat pour QML + +signals: + void scoreChanged(); + void boardChanged(); + void gameOver(); + +private: + int m_board[4][4]; + int m_score; + + void initBoard(); + void spawnTile(); + bool canMove() const; + void updateGameState(); +}; + +#endif // GAME2048_H diff --git a/Jeu_2048/main.cpp b/Jeu_2048/main.cpp index bc907cef49cc8e8cbaa62dcb73d2f0da75ee4398..dda871aab6e76a20ad17815ccf226a4879044c26 100644 --- a/Jeu_2048/main.cpp +++ b/Jeu_2048/main.cpp @@ -1,11 +1,18 @@ #include <QGuiApplication> #include <QQmlApplicationEngine> +#include <QQmlContext> +#include "game2048.h" -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); + // Objet logique du jeu + Game2048 game; + QQmlApplicationEngine engine; + // Exposition de l'objet C++ au contexte QML sous le nom "game" + engine.rootContext()->setContextProperty("game", &game); + QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed,