/*==================================== AUTEUR : Jérémy Hébert & William Godin PROJET : Taxon NOM DU FICHIER : mesFonctions.cpp DATE : 17 novembre 2025 BUT : Contient la définition des fonctions déclarées dans le fichier d’en-tête ====================================*/ //Librairies #include #include #include #include #include #include #include "mesFonctions.h" using namespace std; #include #include 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 = 51; /*==================================== MENU ====================================*/ void afficherMenu(string nomFichier) { fstream monFlux; system("cls"); ouvrirFichier(monFlux, nomFichier); afficherImage(monFlux); fermerFichier(monFlux); } void ouvrirFichier(fstream& monFlux, string nomFichier) { monFlux.open(nomFichier, ios::in); if (!monFlux) { cout << "Une erreur est survenue ! Veuillez relancer le programme."; exit(1); } } void afficherImage(fstream& monFlux) { const char carre = 219; HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE); //Déclaration de variable pour les images while (!monFlux.eof()) { int val = 0; monFlux >> val; if (val == 99) { cout << endl; } else { SetConsoleTextAttribute(hconsole, val); cout << carre << carre; } } SetConsoleTextAttribute(hconsole, 15); } void fermerFichier(fstream& monFlux) { monFlux.close(); } void delai() { // Source : https://search.brave.com/search?q=comment+ajouter+du+delai+avant+une+instruction+en+cpp&conversation=ee2f26795aae1fe85434f1&summary=1 this_thread::sleep_for(0.3s); } void curseur(bool interrupteur) { // Source : https://search.brave.com/search?q=comment+cacher+notre+curseur+dans+la+console+en+cpp&summary=1&conversation=c93091ff881c9c38dfd1a2 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(hConsole, &cci); cci.bVisible = interrupteur; SetConsoleCursorInfo(hConsole, &cci); } /*==================================== 1 - JOUER ====================================*/ //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; fstream monFlux; ouvrirFichier(monFlux, nomFichier); string motRandom = goToLine(monFlux, numeroLigne); fermerFichier(monFlux); return motRandom; } string goToLine(fstream& monFlux, int numeroLigne) {//génère un nb aléatoire, et choisi une ligne associé pour un mot aléatoire. renvoi le mot choisi string mot; for (int i = 0; i < numeroLigne; i++) { getline(monFlux, mot); } return mot; } void effectuerTour(int nbLettre) { fstream monFlux; string nomFichier; HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE);//pour la couleur string motRandom = choisirMot("Mots/mot3lettres.txt", NOMBREMOT3LETTRE); cout << setw(83) << "=============================================\n"; string motPlayer; for (int nbTentative = 0; nbTentative < 6; nbTentative++) { do { cout << setw(65) << "Tentative #" << nbTentative + 1 << endl; cout << setw(69) << "Saisir votre mot : "; cin >> motPlayer; } while (motPlayer.length() != nbLettre); if (dansListe(motPlayer)) { cout << setw(66) << "Votre mot est : " << motPlayer << endl; for (int i = 0; i < nbLettre; i++) { if (lettreDansMot(motRandom, motPlayer, nbLettre, i) == BONNE_PLACE) { string lettrePlayer; lettrePlayer = motPlayer[i]; lettrePlayer.insert(0, "Alphabet/Normal/"); lettrePlayer.insert(17, ".txt"); nomFichier = lettrePlayer; ouvrirFichier(monFlux, nomFichier); printLettre(monFlux, 1); fermerFichier(monFlux); } else if (lettreDansMot(motRandom, motPlayer, nbLettre, i) == MAUVAIS_PLACE) { string lettrePlayer; lettrePlayer = motPlayer[i]; lettrePlayer.insert(0, "Alphabet/Normal/"); lettrePlayer.insert(17, ".txt"); nomFichier = lettrePlayer; ouvrirFichier(monFlux, nomFichier); printLettre(monFlux, 2); fermerFichier(monFlux); } else { string lettrePlayer; lettrePlayer = motPlayer[i]; lettrePlayer.insert(0, "Alphabet/Normal/"); lettrePlayer.insert(17, ".txt"); nomFichier = lettrePlayer; ouvrirFichier(monFlux, nomFichier); printLettre(monFlux, 3); fermerFichier(monFlux); } } cout << endl; } else { cout << setw(66) << "Votre mot est : " << motPlayer; cout << endl << setw(75) << "Mot n'est pas dans la liste\n\n"; nbTentative--; } } } bool dansListe(string motPlayer) { fstream monFlux; ouvrirFichier(monFlux, "Mots/mot3lettres.txt"); string mot; for (int i = 0; i < NOMBREMOT3LETTRE; i++) { getline(monFlux, mot); if (motPlayer == mot) { fermerFichier(monFlux); return true; } } fermerFichier(monFlux); return false; } int lettreDansMot(string motRandom, string motPlayer, int nbLettre, int i) { for (int k = 0; k < nbLettre; k++) { if (motRandom[k] == motPlayer[i] && i == k) { return BONNE_PLACE; } else if ((motRandom[k] == motPlayer[i]) && i != k) { return MAUVAIS_PLACE; } } return PAS_LA; } /*==================================== 2 - OPTIONS ====================================*/ void background(int couleur) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); switch (couleur) { case '1': SetConsoleTextAttribute(hConsole, BACKGROUND_RED | BACKGROUND_INTENSITY); break; case '2': SetConsoleTextAttribute(hConsole, BACKGROUND_RED); break; case '3': SetConsoleTextAttribute(hConsole, BACKGROUND_BLUE); break; case '4': SetConsoleTextAttribute(hConsole, BACKGROUND_GREEN); break; case '5': //SetConsoleTextAttribute(hConsole, BACKGROUND_RED | BACKGROUND_INTENSITY); break; case '6': //SetConsoleTextAttribute(hConsole, BACKGROUND_RED | BACKGROUND_INTENSITY); break; default: SetConsoleTextAttribute(hConsole, BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY); } } int demanderBackground() { cout << "Choix"; int choix = _getche(); return choix; } void backgroundDefault() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, 0x0F); } /*==================================== Affichage couleur des lettres ====================================*/ void printLettre(fstream& monFlux, int positionLettre) { int couleurLettre; if (positionLettre == 1 /*Lettre bien placé*/) { couleurLettre = 10; } else if (positionLettre == 2 /*Lettre mal placé*/) { couleurLettre = 06; } else if (positionLettre == 3 /*Lettre non présente*/) { couleurLettre = 8; } else /*Pas une lettre*/ { exit(2); } const char carre = 219; HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE); //Déclaration de variable pour les images while (!monFlux.eof()) { int val = 0; monFlux >> val; if (val == 00) { val = couleurLettre; } if (val == 99) { cout << endl; } else { SetConsoleTextAttribute(hconsole, val); cout << carre << carre; } } SetConsoleTextAttribute(hconsole, 15); }