diff --git a/motus/CMakeLists.txt b/motus/CMakeLists.txt index 0a2d9ef52bc906bcef8e55986d0e51c3be8a9a9c..47d03d0853a2491d896e63c4dfc0eb27f7dadf88 100644 --- a/motus/CMakeLists.txt +++ b/motus/CMakeLists.txt @@ -37,6 +37,7 @@ qt_add_qml_module(appmotus SOURCES jeu.h jeu.cpp SOURCES grillemanager.h grillemanager.cpp QML_FILES Case.qml + QML_FILES Grille.qml ) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. diff --git a/motus/GameWindow.qml b/motus/GameWindow.qml index 252fa0d20ac464aedf99ff525d239574f033518f..eb6f17e21ef8289706ad5da835ac64f581f72c7e 100644 --- a/motus/GameWindow.qml +++ b/motus/GameWindow.qml @@ -14,25 +14,6 @@ Item { id: rectangle color: "#ffffff" anchors.fill: parent - Grid { - 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 diff --git a/motus/Grille.qml b/motus/Grille.qml new file mode 100644 index 0000000000000000000000000000000000000000..bf3708c533a41bff9ce18a6c0703f3e72bf930c2 --- /dev/null +++ b/motus/Grille.qml @@ -0,0 +1,18 @@ +import QtQuick +import QtQuick.Controls + +Grid { + id: grille + rows: jeu.grilleManager.rows // Get the number of rows dynamically + columns: jeu.grilleManager.columns // Get the number of columns dynamically + spacing: 5 + + // Use a Repeater to populate the grid with cases + Repeater { + model: jeu.grilleManager.cases + delegate: Case { + letter: modelData.letter + etat: modelData.etat + } + } +} diff --git a/motus/Main.qml b/motus/Main.qml index 21723aba930ba8ee1f01b750f402186c0f924994..8d38be1289ebd863a0b8a5da2c3fb2df40a077a0 100644 --- a/motus/Main.qml +++ b/motus/Main.qml @@ -7,7 +7,6 @@ Window { color: "#8ff0a4" title: qsTr("Hello World") - Rectangle { id: rectangle x: 286 @@ -37,16 +36,22 @@ Window { id: bouton x: 360 y: 624 - mouseArea.onClicked:{ + mouseArea.onClicked: { pageLoader.source = "GameWindow.qml"; jeu.startGame(); } } - + Loader { id: pageLoader anchors.centerIn: parent - } - + // Once the page is loaded, notify C++ + onItemChanged: { + if (item) { + // Call a function in C++ and pass the root item of GameWindow + jeu.setGameWindowItem(item); + } + } + } } diff --git a/motus/Resources.qrc b/motus/Resources.qrc index 00e9bc39fd009e4ecce81536b0c23265754476c8..4c3e6a54d9698a0e535ebaa8fdacfb0063f79f01 100644 --- a/motus/Resources.qrc +++ b/motus/Resources.qrc @@ -8,5 +8,6 @@ <file>Bouton.qml</file> <file>GameWindow.qml</file> <file>Case.qml</file> + <file>Grille.qml</file> </qresource> </RCC> diff --git a/motus/brain.cpp b/motus/brain.cpp index da614aedfb04f4443dac79ec45ec4c0f68aaca56..985ad217bbe958e9d4c9b582da0dbcf8926a2360 100644 --- a/motus/brain.cpp +++ b/motus/brain.cpp @@ -4,20 +4,16 @@ #include <iostream> #include <filesystem> #include "grillemanager.h" -<<<<<<< HEAD #include "fullligneexception.h" -======= #include <QString> ->>>>>>> e6f8b0eb13207f90ae4ed37356359f928e941f2a - using namespace std; namespace fs = std::filesystem; #include "nowordexception.h" #include "brain.h" -Brain::Brain(string dicodir) : dicodir(dicodir) { +Brain::Brain(QQmlComponent *grilleComponent, string dicodir, QQuickItem * parentItem) : dicodir(dicodir), parentItem(parentItem), grilleComponent(grilleComponent) { vector<string> files = getTxtFiles(); // TODO // Initialise le menu déroulant avec files @@ -100,11 +96,6 @@ string Brain::getMot() {return mot;} void Brain::initGame() { try { trouveMot(); - cout << "Réponse : ["; - cout << getMot(); - cout << "]"; - cout << endl; - for (int i=0;i<mNbEssaisMax;i++) { lignes.push_back(new Ligne(getMot())); lignes[i]->initLigne(); @@ -113,7 +104,7 @@ void Brain::initGame() { // Convertir std::string (getMot()) en QString QString motQString = QString::fromStdString(getMot()); // Conversion de std::string en QString GrilleManager grille(motQString); // Passer le QString à GrilleManager - grille.createGrid(mNbEssaisMax, mTailleMot); // Créer la grille + grille.createGrid(grilleComponent, parentItem,this, mNbEssaisMax, mTailleMot); // Créer la grille } catch (NoWordException e) { cout << e.what() << endl; } diff --git a/motus/brain.h b/motus/brain.h index 957046cdb41fd5c19afd05a8777887ddae2d0d5c..1db79ebe0d365a80bcbf634384cc51c70a2b21ba 100644 --- a/motus/brain.h +++ b/motus/brain.h @@ -4,6 +4,7 @@ #include <string> #include <vector> #include <map> +#include <QQuickItem> #include "ligne.h" @@ -12,7 +13,7 @@ using namespace std; class Brain { public: - Brain(string dicodir); + Brain(QQmlComponent *grilleComponent, string dicodir, QQuickItem * parentItem); void trouveMot(); void setFichierDico(string fichierDico); void setNombreEssais(int nbEssais); @@ -24,6 +25,7 @@ public: vector<string> getTxtFiles(); void entreLettre(char lettre); void supprLettre(); + vector<Ligne*> lignes; private: string dicodir; @@ -32,8 +34,9 @@ private: int mNbEssaisMax; int nbEssais; int mTailleMot; - vector<Ligne*> lignes; map<char, int> lettres; + QQuickItem * parentItem; + QQmlComponent *grilleComponent; }; #endif // BRAIN_H diff --git a/motus/grillemanager.cpp b/motus/grillemanager.cpp index 2150363d60fe233c19b9ef03d3bc5d2168029ad1..5f99dfb817a839a25add69dbc9135f19769a4492 100644 --- a/motus/grillemanager.cpp +++ b/motus/grillemanager.cpp @@ -1,27 +1,37 @@ #include "grillemanager.h" GrilleManager::GrilleManager(const QString &mot, QObject *parent) - : QObject(parent), m_mot(mot) { - // Initialisation de la grille (si tu veux le faire immédiatement dans le constructeur) - createGrid(6, m_mot.length()); // Exemple : 6 essais, longueur du mot -} + : QObject(parent), m_mot(mot) {} -void GrilleManager::createGrid(int rows, int columns) { +void GrilleManager::createGrid(QQmlComponent *grilleComponent, QQuickItem * parentItem,Brain* brain, int rows, int columns) { m_cases.clear(); // Vide la grille avant de la remplir // Remplir la grille avec des cases for (int i = 0; i < rows * columns; ++i) { - VraieCase *vraieCase = new VraieCase(this); + VraieCase* lacase = brain->lignes[i / columns]->contenu[i % columns]; // Si on est à la première case, on met la première lettre du mot, sinon on met '_' if (i < m_mot.length()) { - vraieCase->setLetter(m_mot[i]); // Conversion automatique depuis QString + lacase->setLetter(m_mot[i]); // Conversion automatique depuis QString } else { - vraieCase->setLetter(QString("_")); // Conversion du char '_' en QString + lacase->setLetter(QString("_")); // Conversion du char '_' en QString } - vraieCase->setEtat(0); // Tu peux définir l'état comme tu veux - m_cases.append(vraieCase); + m_cases.append(lacase); + } + + + // context->setContextProperty("jeu", jeu); + + if (grilleComponent->status() == QQmlComponent::Ready) { + QObject *object = grilleComponent->create(); + QQuickItem *item = qobject_cast<QQuickItem *>(object); + if (item) { + // Add it to the parent item + item->setParentItem(parentItem); + } + } else { + qWarning() << "Failed to load QML component:" << grilleComponent->errorString(); } emit casesChanged(); // Émettre le signal pour indiquer que la grille a changé diff --git a/motus/grillemanager.h b/motus/grillemanager.h index 970e528665c639289d7fc2ec284f9a2a02cf506c..00f1d1a9e67f6baf0184069c0c33a2ae775354c6 100644 --- a/motus/grillemanager.h +++ b/motus/grillemanager.h @@ -4,6 +4,7 @@ #include <QObject> #include <QQmlListProperty> #include "vraiecase.h" +#include "brain.h" class GrilleManager : public QObject { Q_OBJECT @@ -11,7 +12,7 @@ class GrilleManager : public QObject { public: explicit GrilleManager(const QString &mot, QObject *parent = nullptr); // Le mot en argument - void createGrid(int rows, int columns); // Créer la grille + void createGrid(QQmlComponent *grilleComponent, QQuickItem * parentItem, Brain* brain, int rows, int columns); // Créer la grille QQmlListProperty<VraieCase> getCases(); // Obtenir les cases signals: diff --git a/motus/jeu.cpp b/motus/jeu.cpp index 3376b9b3da72bab81d7aaf8b58eb4aa659325f8f..1aa63c67e569c51440f42496c7a9350662b11506 100644 --- a/motus/jeu.cpp +++ b/motus/jeu.cpp @@ -3,37 +3,43 @@ -Jeu::Jeu(QObject *parent) : QObject(parent), brain("./"), grilleManager("", this) { +Jeu::Jeu(QObject *parent) : QObject(parent), grilleManager("", this) { } -void Jeu::setMot(const QString& mot) { - this->mot = mot; - grilleManager.createGrid(mot.length(), 1); // Utilise la longueur du mot pour la grille - emit motChanged(); -} +// void Jeu::setMot(const QString& mot) { +// this->mot = mot; +// grilleManager.createGrid(mot.length(), 1); // Utilise la longueur du mot pour la grille +// emit motChanged(); +// } QString Jeu::getMotAffiche() { return mot; } +void Jeu::setContext(QQmlComponent *grilleComponent, QQuickItem* parentItem) { + grilleComponent = grilleComponent; + parentItem = parentItem; + brain = new Brain(grilleComponent,"./",parentItem); +} void Jeu::onClavierClick(QString lettre) { - brain.entreLettre(lettre.toStdString()[0]); + brain->entreLettre(lettre.toStdString()[0]); } + void Jeu::initGame() { - brain.initGame(); + brain->initGame(); } void Jeu::startGame() { - brain.setFichierDico("words_alpha.txt"); + brain->setFichierDico("words_alpha.txt"); - brain.setNombreEssais(6); - brain.setTailleMots(5); + brain->setNombreEssais(6); + brain->setTailleMots(5); - brain.initGame(); + brain->initGame(); - grilleManager.createGrid(6, 5); // à synchroniser avec les paramètres du brain + // grilleManager.createGrid(6, 5); // à synchroniser avec les paramètres du brain } diff --git a/motus/jeu.h b/motus/jeu.h index da98313e8416d5a0bf64c9534abcec20585d92be..a13350f97e1992d2af04879addb2e7688dfde005 100644 --- a/motus/jeu.h +++ b/motus/jeu.h @@ -2,6 +2,8 @@ #define JEU_H #include <QObject> +#include <QQuickItem> +#include <QQmlComponent> #include "brain.h" #include "grillemanager.h" @@ -9,6 +11,7 @@ class Jeu : public QObject { Q_OBJECT Q_PROPERTY(QString motAffiche READ getMotAffiche NOTIFY motChanged) Q_PROPERTY(GrilleManager* grilleManager READ getGrilleManager CONSTANT) + Q_PROPERTY(QQuickItem* gameWindow READ getGameWindow NOTIFY gameWindowChanged) public: explicit Jeu(QObject *parent = nullptr); @@ -16,16 +19,21 @@ public: Q_INVOKABLE void onClavierClick(QString lettre); Q_INVOKABLE void initGame(); Q_INVOKABLE void startGame(); // pour init depuis QML - Q_INVOKABLE void setMot(const QString& mot); // Ajouter cette méthode pour définir le mot GrilleManager* getGrilleManager() { return &grilleManager; } + QQuickItem* getGameWindow() const { return gameWindow; } // Getter for gameWindow + void setContext(QQmlComponent *grilleComponent, QQuickItem* parentItem); + signals: void motChanged(); + void gameWindowChanged(); // Signal to notify when the game window is set private: - Brain brain; + Brain* brain; QString mot; - GrilleManager grilleManager; // <- On le garde en vie ici + GrilleManager grilleManager; + QQmlComponent* grilleComponent; + QQuickItem* gameWindow; // Pointer to the QQuickItem of the GameWindow }; #endif // JEU_H diff --git a/motus/ligne.cpp b/motus/ligne.cpp index f7e0050ad75d9f156736928bd736d71e92fa4545..78a424521f81501c1310390cd74475d4f59ddd6c 100644 --- a/motus/ligne.cpp +++ b/motus/ligne.cpp @@ -1,13 +1,10 @@ #include "ligne.h" #include "fullligneexception.h" #include "emptyligneexception.h" -<<<<<<< HEAD #include "iostream" #include <cctype> using namespace std; -======= #include <QString> ->>>>>>> e6f8b0eb13207f90ae4ed37356359f928e941f2a Ligne::Ligne(string bonmot) : bonmot(bonmot), positionCurseur(0) {} @@ -19,12 +16,18 @@ void Ligne::initLigne() { bool Ligne::isGameCleared() { int nbjustes = 0; - for (int i=0;i<bonmot.length();i++) { - int etat = (tolower(contenu[i]->getLetter()) == bonmot[i]) ? 0 : 1; - etat += Ligne::dansMot(tolower(contenu[i]->getLetter()), bonmot); + cout << bonmot << endl; + show(); + for (int i = 0; i < bonmot.length(); i++) { + char bonnelettre = toupper(bonmot[i]); // Convert to uppercase + QChar lettreacomparer = contenu[i]->getLetter().at(0); // Get the QChar from contenu + int etat1 = (bonnelettre == lettreacomparer.toUpper().toLatin1()) ? 1 : 0; + int etat2 = Ligne::dansMot(lettreacomparer, bonmot); + int etat = etat1+etat2; + cout << etat1 << etat2 << endl; contenu[i]->setEtat(etat); - cout << contenu[i]->getLetter() << bonmot[i] << etat << endl; } + cout << "TEST" << endl; show(); return (nbjustes == 2*bonmot.length()); } @@ -33,16 +36,16 @@ void Ligne::show() { cout << "La ligne : |"; for (int i=0;i<bonmot.length();i++) { if (contenu[i]->getEtat() == 1) {cout << "\033[33m";} - if (contenu[i]->getEtat() == 2) {cout << "";} - cout << contenu[i]->getLetter(); - if (contenu[i]->getEtat() >= 1) cout << "\033[31m"; + if (contenu[i]->getEtat() == 2) {cout << "\033[31m";} + cout << contenu[i]->getLetter().toStdString(); + if (contenu[i]->getEtat() >= 1) cout << "\033[30m"; } cout << "|" << endl; } -bool Ligne::dansMot(char lettre, string mot) { +bool Ligne::dansMot(QChar lettre, string mot) { for (int i=0;i<mot.length();i++) { - if (mot[i] == lettre) { + if (toupper(mot[i]) == lettre.toLatin1()) { return true; } } diff --git a/motus/ligne.h b/motus/ligne.h index e7600f4f1e253d6feb8af5aa92b57adbf9bb9ba4..8cef7c958de02fe775f1da7ac66e90d63b4399ad 100644 --- a/motus/ligne.h +++ b/motus/ligne.h @@ -22,12 +22,12 @@ public: void entreLettre(char lettre); void supprLettre(); void show(); + vector<VraieCase*> contenu; private: string bonmot; int positionCurseur; int taille; - vector<VraieCase*> contenu; static bool dansMot(QChar lettre, std::string mot); }; diff --git a/motus/main.cpp b/motus/main.cpp index 98e34cd2ed777f0acea0d2b6ff974b305ec12b39..7a39861244667f377022e2b8b9edd2f689a84a0e 100644 --- a/motus/main.cpp +++ b/motus/main.cpp @@ -1,6 +1,8 @@ #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> +#include <QQuickItem> +#include <QQmlApplicationEngine> #include "jeu.h" #include "vraiecase.h" #include "grillemanager.h" @@ -12,12 +14,22 @@ int main(int argc, char *argv[]) { qmlRegisterType<VraieCase>("motus", 1, 0, "VraieCase"); qmlRegisterType<GrilleManager>("motus", 1, 0, "GrilleManager"); + + + // Charger l'interface utilisateur (QML) + engine.loadFromModule("motus", "Main"); + QObject *rootObject = engine.rootObjects().first(); // Gets the first root object + QQuickItem *rootItem = qobject_cast<QQuickItem *>(rootObject); + + QQmlComponent grilleComponent(&engine, QUrl::fromLocalFile("../../Grille.qml")); + + QQuickItem *parentItem = rootItem; // Créer l'objet Jeu sans mot Jeu jeu; engine.rootContext()->setContextProperty("jeu", &jeu); - // Charger l'interface utilisateur (QML) - engine.loadFromModule("motus", "Main"); + jeu.setContext(&grilleComponent,parentItem); + return app.exec(); }