From 24cd682bdbfbd59f8b1a12dbd557c345352559f7 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Fri, 5 Dec 2025 23:52:58 -0500 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20total=20de=20la=20prise?= =?UTF-8?q?=20en=20charge=20en=20simultan=C3=A9=20des=20mots=20a=203=20et?= =?UTF-8?q?=204=20lettres.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 0 | 2 +- main.cpp | 9 ++++---- mesFonctions.cpp | 58 ++++++++++++++++++++++++++---------------------- mesFonctions.h | 6 +++-- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/0 b/0 index d7619da..4588032 100644 --- a/0 +++ b/0 @@ -1 +1 @@ -Appuyez sur une touche pour continuer... \ No newline at end of file +Appuyez sur une touche pour continuer... diff --git a/main.cpp b/main.cpp index 92b436d..e36fc03 100644 --- a/main.cpp +++ b/main.cpp @@ -17,6 +17,9 @@ BUT : Rassemble les appels de fonction de Taxon //Déclaration des namespaces using namespace std; +extern int nbEssai = 6; +extern int nbLettre = 4; + //Déclaration du main int main() { srand(time(NULL)); @@ -24,11 +27,7 @@ int main() { bool quitter = false; //Quitter le jeu int nbMot3lettre = 15; //Nombre de mot présent dans le fichier - int nbLettre = 4; string motRandom; // Mot random - const int NOMBREMOT3LETTRE = 101; - const int NOMBREMOT4LETTRE = 74; - int nbEssai = 6; while (quitter == false) { curseur(false); @@ -44,7 +43,7 @@ int main() { system("cls"); - effectuerTour(nbLettre); + effectuerTour(); break; case '2': diff --git a/mesFonctions.cpp b/mesFonctions.cpp index 6883557..8a0312d 100644 --- a/mesFonctions.cpp +++ b/mesFonctions.cpp @@ -24,10 +24,8 @@ using namespace std::chrono_literals; #include //Sources : https://www.tutorialspoint.com/cpp_standard_library/limits.htm et https://stackoverflow.com/questions/2158943/split-string-into-array-of-chars const int BONNE_PLACE = 1, MAUVAIS_PLACE = 2, PAS_LA = 3; -extern const int NOMBREMOT3LETTRE = 101; -extern const int NOMBREMOT4LETTRE = 74; -extern int nbEssai = 6; -extern int nbLettre = 4; +extern int nbEssai; +extern int nbLettre; /*==================================== MENU @@ -92,11 +90,26 @@ void curseur(bool interrupteur) { /*==================================== 1 - JOUER ====================================*/ +//Fonction pour connaitre le nombre de ligne d'un fichier +int nombreLigneFichier(fstream& monFlux) { + string str; + int nbLigne = 0; + + while (!monFlux.eof()) { + getline(monFlux, str); + nbLigne++; + } + + monFlux.seekg(ios::beg); + + return nbLigne; +} //fonction pour choisir le mot de facon random -string choisirMot(string nomFichier, int maximum) {//Choisi un mot aléatoire et le renvoi - int numeroLigne = rand() % (maximum)+1; +string choisirMot(string nomFichier) {//Choisi un mot aléatoire et le renvoi fstream monFlux; ouvrirFichier(monFlux, nomFichier); + int maximum = nombreLigneFichier(monFlux); + int numeroLigne = rand() % (maximum)+1; string motRandom = goToLine(monFlux, numeroLigne); fermerFichier(monFlux); return motRandom; @@ -110,14 +123,14 @@ string goToLine(fstream& monFlux, int numeroLigne) {//g return mot; } -void effectuerTour(int nbLettre) +void effectuerTour() { string motRandom; if (nbLettre == 3) { - motRandom = choisirMot("Mots/mot3lettres.txt", NOMBREMOT3LETTRE); + motRandom = choisirMot("Mots/mot3lettres.txt"); } else if (nbLettre == 4) { - motRandom = choisirMot("Mots/mot4lettres.txt", NOMBREMOT4LETTRE); + motRandom = choisirMot("Mots/mot4lettres.txt"); } cout << setw(83) << "=============================================\n"; @@ -128,7 +141,7 @@ void effectuerTour(int nbLettre) { string lettrePlayer; do { - cout << setw(65) << "Tentative #" << nbTentative + 1 << endl; + cout << setw(69) << "Tentative des mots de " << nbLettre << " #" << nbTentative + 1 << endl; cout << setw(69) << "Saisir votre mot : "; cin >> motPlayer; //TODO: mettre tolower string (boucle) } while (motPlayer.length() != nbLettre); @@ -166,28 +179,21 @@ void effectuerTour(int nbLettre) bool dansListe(string motPlayer) { fstream monFlux; string mot; + int nbMots = 0; if (nbLettre == 3) { ouvrirFichier(monFlux, "Mots/mot3lettres.txt"); - for (int i = 0; i < NOMBREMOT3LETTRE; i++) { - getline(monFlux, mot); - - if (motPlayer == mot) - { - fermerFichier(monFlux); - return true; - } - } } else if (nbLettre == 4) { ouvrirFichier(monFlux, "Mots/mot4lettres.txt"); - for (int i = 0; i < NOMBREMOT4LETTRE; i++) { - getline(monFlux, mot); + } + nbMots = nombreLigneFichier(monFlux); + for (int i = 0; i < nbMots; i++) { + getline(monFlux, mot); //TODO: Verification d'erreur du getline (Exemple si le fichier est trops petit) - if (motPlayer == mot) - { - fermerFichier(monFlux); - return true; - } + if (motPlayer == mot) + { + fermerFichier(monFlux); + return true; } } fermerFichier(monFlux); diff --git a/mesFonctions.h b/mesFonctions.h index 577a913..ac20825 100644 --- a/mesFonctions.h +++ b/mesFonctions.h @@ -25,11 +25,13 @@ void fermerFichier(fstream& monFlux); void delai(); void curseur(bool interrupteur); +int nombreLigneFichier(fstream& monFlux); + // Mot -string choisirMot(string nomFichier, int maximum); +string choisirMot(string nomFichier); string goToLine(fstream& monFlux, int numeroLigne); -void effectuerTour(int nbLettre); +void effectuerTour(); bool dansListe(string motPlayer); bool comparerMot(fstream& monFlux, string motPlayer, int numeroLigne); int lettreDansMot(string motRandom, string motPlayer, int nbLettre, int i);