From 0305df62f4244b7bb6d0cd541f3e2fa960dd7604 Mon Sep 17 00:00:00 2001
From: Yanis Dziki <yanis.dziki@ens-lyon.fr>
Date: Fri, 11 Apr 2025 15:03:56 +0200
Subject: [PATCH] Gen grille

---
 motus/CMakeLists.txt |  1 +
 motus/Case.qml       | 21 +++++++++++++++++++++
 motus/GameWindow.qml | 42 ++++++++++++++++++++----------------------
 motus/Main.qml       |  2 +-
 motus/Resources.qrc  |  1 +
 motus/jeu.cpp        | 17 +++++++++++++----
 motus/jeu.h          |  6 ++++++
 motus/main.cpp       |  3 ---
 8 files changed, 63 insertions(+), 30 deletions(-)
 create mode 100644 motus/Case.qml

diff --git a/motus/CMakeLists.txt b/motus/CMakeLists.txt
index a166b2f..0a2d9ef 100644
--- a/motus/CMakeLists.txt
+++ b/motus/CMakeLists.txt
@@ -36,6 +36,7 @@ qt_add_qml_module(appmotus
         QML_FILES GameWindow.qml
         SOURCES jeu.h jeu.cpp
         SOURCES grillemanager.h grillemanager.cpp
+        QML_FILES Case.qml
 )
 
 # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
diff --git a/motus/Case.qml b/motus/Case.qml
new file mode 100644
index 0000000..0fb3e00
--- /dev/null
+++ b/motus/Case.qml
@@ -0,0 +1,21 @@
+import QtQuick
+
+Rectangle {
+    width: 50
+    height: 50
+    radius: 5
+    color: etat === 0 ? "#ffffff" : etat === 1 ? "#fdd835" : "#66bb6a"
+    border.color: "#000000"
+    border.width: 1
+
+    property string letter: ""
+    property int etat: 0
+
+    Text {
+        anchors.centerIn: parent
+        text: letter
+        font.pixelSize: 24
+        font.bold: true
+        color: "#000"
+    }
+}
diff --git a/motus/GameWindow.qml b/motus/GameWindow.qml
index 65ce47a..252fa0d 100644
--- a/motus/GameWindow.qml
+++ b/motus/GameWindow.qml
@@ -3,6 +3,9 @@
 
 import QtQuick
 
+
+
+
 Item {
     width: 900
     height: 800
@@ -11,31 +14,25 @@ Item {
         id: rectangle
         color: "#ffffff"
         anchors.fill: parent
-
         Grid {
-                id: gridView
-                anchors.top: parent.top
-                anchors.left: parent.left
-                columns: 5
-                spacing: 5
-
-                Repeater {
-                    model: grilleManager.cases
-                    Rectangle {
-                        width: 50
-                        height: 50
-                        color: modelData.etat === 0 ? "lightgray" :
-                               modelData.etat === 1 ? "yellow" : "green"
-                        border.color: "black"
-
-                        Text {
-                            anchors.centerIn: parent
-                            text: modelData.letter
-                            font.bold: true
-                        }
-                    }
+            id: grille
+            anchors.top: parent.top
+            anchors.topMargin: 50
+            anchors.horizontalCenter: parent.horizontalCenter
+            rows: 6
+            columns: 5
+            spacing: 5
+
+            Repeater {
+                model: jeu.grilleManager.cases
+
+                delegate: Case {
+                    letter: modelData.letter
+                    etat: modelData.etat
                 }
             }
+        }
+
 
         Clavier {
             id: clavier
@@ -44,3 +41,4 @@ Item {
         }
     }
 }
+
diff --git a/motus/Main.qml b/motus/Main.qml
index 967f86e..21723ab 100644
--- a/motus/Main.qml
+++ b/motus/Main.qml
@@ -39,7 +39,7 @@ Window {
         y: 624
         mouseArea.onClicked:{
             pageLoader.source = "GameWindow.qml";
-            jeu.initGame();
+            jeu.startGame();
         }
     }
     
diff --git a/motus/Resources.qrc b/motus/Resources.qrc
index 90f00a4..00e9bc3 100644
--- a/motus/Resources.qrc
+++ b/motus/Resources.qrc
@@ -7,5 +7,6 @@
         <file>bouton.qml</file>
         <file>Bouton.qml</file>
         <file>GameWindow.qml</file>
+        <file>Case.qml</file>
     </qresource>
 </RCC>
diff --git a/motus/jeu.cpp b/motus/jeu.cpp
index e077dc3..1f788ce 100644
--- a/motus/jeu.cpp
+++ b/motus/jeu.cpp
@@ -2,10 +2,7 @@
 #include <iostream>
 
 Jeu::Jeu(QObject *parent) : QObject(parent), brain("./") {
-    brain.setFichierDico("words_alpha.txt");
-    brain.setNombreEssais(5);
-    brain.setTailleMots(5);
-    brain.initGame();
+
 
     mot = QString::fromStdString(brain.getMot()); // ✅ Conversion std::string → QString
 
@@ -30,3 +27,15 @@ void Jeu::onClavierClick(QString lettre) {
 void Jeu::initGame() {
     brain.initGame();
 }
+
+
+void Jeu::startGame() {
+    brain.setFichierDico("words_alpha.txt");
+
+    brain.setNombreEssais(6); // ou ce que tu veux
+    brain.setTailleMots(5);   // idem
+
+    brain.initGame();
+
+    grilleManager.createGrid(6, 5); // à synchroniser avec les paramètres du brain
+}
diff --git a/motus/jeu.h b/motus/jeu.h
index bf308bf..c654e81 100644
--- a/motus/jeu.h
+++ b/motus/jeu.h
@@ -3,16 +3,21 @@
 
 #include <QObject>
 #include "brain.h"
+#include "grillemanager.h"
 
 class Jeu : public QObject {
     Q_OBJECT
     Q_PROPERTY(QString motAffiche READ getMotAffiche NOTIFY motChanged)
+    Q_PROPERTY(GrilleManager* grilleManager READ getGrilleManager CONSTANT)
 
 public:
     explicit Jeu(QObject *parent = nullptr);
     QString getMotAffiche();
     Q_INVOKABLE void onClavierClick(QString lettre);
     Q_INVOKABLE void initGame();
+    Q_INVOKABLE void startGame(); // pour init depuis QML
+    GrilleManager* getGrilleManager() { return &grilleManager; }
+
 
 signals:
     void motChanged();
@@ -20,6 +25,7 @@ signals:
 private:
     Brain brain;
     QString mot;
+    GrilleManager grilleManager; // <- On le garde en vie ici
 };
 
 #endif // JEU_H
diff --git a/motus/main.cpp b/motus/main.cpp
index bc6161e..6fef187 100644
--- a/motus/main.cpp
+++ b/motus/main.cpp
@@ -13,9 +13,6 @@ int main(int argc, char *argv[]) {
     qmlRegisterType<VraieCase>("motus", 1, 0, "VraieCase");
     qmlRegisterType<GrilleManager>("motus", 1, 0, "GrilleManager");
 
-    GrilleManager grilleManager;
-    engine.rootContext()->setContextProperty("grilleManager", &grilleManager);
-
     Jeu jeu;
     engine.rootContext()->setContextProperty("jeu", &jeu);
 
-- 
GitLab