Skip to content
Snippets Groups Projects
Commit 56ca9b3d authored by Ruf Quentin's avatar Ruf Quentin
Browse files

implementation règles partie 1

parent dd516949
Branches
No related tags found
No related merge requests found
...@@ -18,9 +18,10 @@ qt_add_qml_module(appJeu_2048 ...@@ -18,9 +18,10 @@ qt_add_qml_module(appJeu_2048
QML_FILES QML_FILES
Main.qml Main.qml
SOURCES bouton.h bouton.cpp SOURCES bouton.h bouton.cpp
SOURCES case.h case.cpp SOURCES case2048.h case2048.cpp
QML_FILES Partie.qml QML_FILES Partie.qml
QML_FILES Menu_de_Controle.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. # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
......
import QtQuick import QtQuick
Window { Window {
width: 640
height: 480
visible: true visible: true
title: qsTr("Hello World") width: 400
height: 400
title: qsTr("2048")
Text {
text: qsTr("Hello 2048")
anchors.centerIn: parent
font.pointSize: 24
}
} }
#include "case.h"
case ::case (){
}
#ifndef CASE_H
#define CASE_H
class case {public:
case();
};
#endif // CASE_H
#include "case2048.h"
Case2048::Case2048 (){
}
#ifndef CASE2048_H
#define CASE2048_H
class Case2048
{
public:
Case2048();
};
#endif // CASE2048_H
#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;
}
#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
#include <QGuiApplication> #include <QGuiApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlContext>
#include "game2048.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
// Objet logique du jeu
Game2048 game;
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
// Exposition de l'objet C++ au contexte QML sous le nom "game"
engine.rootContext()->setContextProperty("game", &game);
QObject::connect( QObject::connect(
&engine, &engine,
&QQmlApplicationEngine::objectCreationFailed, &QQmlApplicationEngine::objectCreationFailed,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment