From 56ca9b3d2378cf333fae964eee46d26d46af12ac Mon Sep 17 00:00:00 2001 From: Ruf Quentin <quentin.ruf@etu.ec-lyon.fr> Date: Mon, 24 Mar 2025 09:59:37 +0100 Subject: [PATCH] =?UTF-8?q?implementation=20r=C3=A8gles=20partie=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jeu_2048/CMakeLists.txt | 3 +- Jeu_2048/Main.qml | 12 ++++-- Jeu_2048/case.cpp | 5 --- Jeu_2048/case.h | 8 ---- Jeu_2048/case2048.cpp | 5 +++ Jeu_2048/case2048.h | 10 +++++ Jeu_2048/game2048.cpp | 87 +++++++++++++++++++++++++++++++++++++++++ Jeu_2048/game2048.h | 40 +++++++++++++++++++ Jeu_2048/main.cpp | 11 +++++- 9 files changed, 162 insertions(+), 19 deletions(-) delete mode 100644 Jeu_2048/case.cpp delete mode 100644 Jeu_2048/case.h create mode 100644 Jeu_2048/case2048.cpp create mode 100644 Jeu_2048/case2048.h create mode 100644 Jeu_2048/game2048.cpp create mode 100644 Jeu_2048/game2048.h diff --git a/Jeu_2048/CMakeLists.txt b/Jeu_2048/CMakeLists.txt index 5fba64a..f38abf7 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 41424f8..3b28f4f 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 094b194..0000000 --- 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 1e3eb70..0000000 --- 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 0000000..8f55c48 --- /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 0000000..ec25c10 --- /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 0000000..8427ae7 --- /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 0000000..d5f5b61 --- /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 bc907ce..dda871a 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, -- GitLab