diff --git a/motus/GameWindow.qml b/motus/GameWindow.qml index 8fd720e3497b059a4d6a6d0598e081f4cfabf51b..767077983326de5c5fd526e3888951f1230a7ba2 100644 --- a/motus/GameWindow.qml +++ b/motus/GameWindow.qml @@ -55,4 +55,3 @@ Item { } } - diff --git a/motus/brain.cpp b/motus/brain.cpp index 4c363d62a4a1da28d204fffcf24d4319ba8ccfa5..ea30506592858244be6a9600c5bbfef080fba848 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(); } @@ -55,13 +64,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 { @@ -86,10 +102,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)); } } @@ -118,6 +134,7 @@ void Brain::initGame() { lignes.push_back(new Ligne(getMot())); lignes[i]->initLigne(); } + cout << "Solution : " << getMot() << endl; } catch (NoWordException e) { cout << e.what() << endl; } @@ -146,18 +163,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 a597890f8fb310dbe5bf64e0ea1d453e888f9236..0571e51cf6f782cd1380f17f1240b8ed52811507 100644 --- a/motus/brain.h +++ b/motus/brain.h @@ -33,6 +33,7 @@ private: string dicodir; string dico; string mot; + int inGame; int mNbEssaisMax; int nbEssais; int mTailleMot; diff --git a/motus/ligne.cpp b/motus/ligne.cpp index edc96500a039219a3871618af775b07d7b9558e3..8621eb3c705cb6d8d8d9c3a5563cee8f626bdcd7 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);