diff --git a/motus/GameWindow.qml b/motus/GameWindow.qml index 79a13687f694a526559a623c1ed3c5aeb092f0a7..3e77f6793a41c90bc45dd45ade6e8c491f66bbb6 100644 --- a/motus/GameWindow.qml +++ b/motus/GameWindow.qml @@ -43,5 +43,10 @@ Item { y: 594 } } + + Loader { + id: pageLoader2 + anchors.centerIn: parent + } } diff --git a/motus/brain.cpp b/motus/brain.cpp index 3f097513df60949d82fba4ec2ae0a85c88b63d13..0ebc8d962c0e98575a4c3a4621aa512aeabb99e7 100644 --- a/motus/brain.cpp +++ b/motus/brain.cpp @@ -4,6 +4,7 @@ #include <iostream> #include <filesystem> #include "fullligneexception.h" +#include "emptyligneexception.h" #include <QString> using namespace std; @@ -12,7 +13,7 @@ namespace fs = std::filesystem; #include "nowordexception.h" #include "brain.h" -Brain::Brain(string dicodir) : dicodir(dicodir), nbEssais(0) { +Brain::Brain(string dicodir) : dicodir(dicodir), nbEssais(0), inGame(0) { vector<string> files = getTxtFiles(); // TODO // Initialise le menu déroulant avec files @@ -20,6 +21,14 @@ Brain::Brain(string dicodir) : dicodir(dicodir), nbEssais(0) { string Brain::getGrid() { string res = ""; + + if (inGame > 0) { + for(int i=0;i<2;i++) {res += " ";} + res += (inGame == 1) ? " LOSER " : " WINNER "; + for(int i=0;i<2;i++) {res += " ";} + return res; + } + for (int i=0;i<mNbEssaisMax;i++) { res += lignes[i]->getMot(); } @@ -44,13 +53,20 @@ vector<string> Brain::getTxtFiles() { } void Brain::entreLettre(char lettre) { + if (inGame > 0) return; + if (lettre == '0') { validateWord(); return; } if (lettre == '!') { - supprLettre(); - lignes[nbEssais]->show(); + try { + supprLettre(); + lignes[nbEssais]->show(); + } + catch (EmptyLigneException e) { + cout << e.what() << endl; + } return; } try { @@ -75,10 +91,10 @@ void Brain::trouveMot() { } string line; - cout << "Mot de taille : " << mTailleMot << "dans" << dico << endl; + cout << "Mot de taille : " << mTailleMot << " dans " << dico << endl; while (std::getline(file, line)) { - if (line.length() == mTailleMot) { - listeMots.push_back(line); + if (line.length() == mTailleMot + 1) { + listeMots.push_back(line.substr(0, 8)); } } @@ -107,6 +123,7 @@ void Brain::initGame() { lignes.push_back(new Ligne(getMot())); lignes[i]->initLigne(); } + cout << "Solution : " << getMot() << endl; } catch (NoWordException e) { cout << e.what() << endl; } @@ -135,18 +152,13 @@ void Brain::validateWord() { void Brain::onGameFinish(bool isWin) { if (!isWin) { cout << "Perdu !" << endl; + inGame = 1; + } + else { + cout << "Gagné !" << endl; + inGame = 2; } - // TODO : - - // Si isWin - // Affiche un message de victoire sur la fenetre menu - // Ferme la fenetre de jeu - - // Sinon - // Affiche un message de defaite sur la fenetre menu - // Ferme la fenetre de jeu - // Reset du brain nbEssais = 0; lignes.clear(); } diff --git a/motus/brain.h b/motus/brain.h index 9f0ffe35dad84bdbdbb03be9c754713258f33937..b97fe479c44b74a5b3f49cc163791c17eb7116be 100644 --- a/motus/brain.h +++ b/motus/brain.h @@ -32,6 +32,7 @@ private: string dicodir; string dico; string mot; + int inGame; int mNbEssaisMax; int nbEssais; int mTailleMot; diff --git a/motus/jeu.cpp b/motus/jeu.cpp index 56be84cd061cc3f0df555f87bffd823c8f20c474..4b1444ebd90efde526522409beb2de1fa1a024b7 100644 --- a/motus/jeu.cpp +++ b/motus/jeu.cpp @@ -35,18 +35,6 @@ void Jeu::onClavierClick(QString lettre) { for (int index = 0; index < 40; index ++) { m_letterModel->setLetter(index, QString::fromLatin1(&brain->getGrid()[index], 1)); } - - // string lagrille = brain->getGrid(); - // for (int index = 0; index < 40; index ++) { - // string nomCell = "cell_"+to_string(index/8)+"_"+to_string(index%8); - // QObject *cell = rootObject->findChild<QObject*>(nomCell); - // if (cell) { - // cell->setProperty("color", "red"); - // } - // } - - - } diff --git a/motus/ligne.cpp b/motus/ligne.cpp index 71a17a0a9a2064c71e8d055c463e2f492d27a5cc..51c01db3eee34966199e873c6c6230dc2fe8a007 100644 --- a/motus/ligne.cpp +++ b/motus/ligne.cpp @@ -16,15 +16,15 @@ void Ligne::initLigne() { bool Ligne::isGameCleared() { int nbjustes = 0; - cout << bonmot.length() <<endl; show(); for (int i = 0; i < bonmot.length(); i++) { - char bonnelettre = toupper(bonmot[i]); // Convert to uppercase - char lettreacomparer = contenu[i]->getLetter(); // Get the QChar from contenu + char bonnelettre = toupper(bonmot[i]); + char lettreacomparer = contenu[i]->getLetter(); int etat1 = (bonnelettre == lettreacomparer) ? 1 : 0; int etat2 = Ligne::dansMot(lettreacomparer, bonmot); int etat = etat1+etat2; contenu[i]->setEtat(etat); + nbjustes += etat; } diff --git a/motus/main.cpp b/motus/main.cpp index f65c4ed875e02e2424058f540597b82095aed9ea..82d58df6d1589c0b00cc075e7df9f5402080e9f6 100644 --- a/motus/main.cpp +++ b/motus/main.cpp @@ -20,13 +20,14 @@ int main(int argc, char *argv[]) { engine.rootContext()->setContextProperty("letterModel", &letterModel); engine.loadFromModule("motus", "Main"); - QObject *rootObject = engine.rootObjects().first(); // Gets the first root object + QObject *rootObject = engine.rootObjects().first(); QQuickItem *rootItem = qobject_cast<QQuickItem *>(rootObject); QQuickItem *parentItem = rootItem; - // Créer l'objet Jeu sans mot + Jeu jeu(rootObject); jeu.setLetterModel(&letterModel); + engine.rootContext()->setContextProperty("jeu", &jeu);