Files
Taxon/mesFonctions.cpp

306 lines
7.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*====================================
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 den-tête
====================================*/
//Librairies
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <Windows.h>
#include <iomanip>
#include "mesFonctions.h"
using namespace std;
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
#include <limits> //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;
/*====================================
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.";
system("PAUSE");
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)
{
string motRandom;
if (nbLettre == 3) {
motRandom = choisirMot("Mots/mot3lettres.txt", NOMBREMOT3LETTRE);
}
else if (nbLettre == 4) {
motRandom = choisirMot("Mots/mot4lettres.txt", NOMBREMOT4LETTRE);
}
cout << setw(83) << "=============================================\n";
string motPlayer;
for (int nbTentative = 0; nbTentative < nbEssai; nbTentative++)
{
string lettrePlayer;
do {
cout << setw(65) << "Tentative #" << nbTentative + 1 << endl;
cout << setw(69) << "Saisir votre mot : ";
cin >> motPlayer; //TODO: mettre tolower string (boucle)
} 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) {
lettrePlayer = motPlayer[i];
afficherMotPlayer(lettrePlayer, BONNE_PLACE);
}
else if (lettreDansMot(motRandom, motPlayer, nbLettre, i) == MAUVAIS_PLACE) {
lettrePlayer = motPlayer[i];
afficherMotPlayer(lettrePlayer, MAUVAIS_PLACE);
}
else {
lettrePlayer = motPlayer[i];
afficherMotPlayer(lettrePlayer, PAS_LA);
}
}
cout << endl;
}
else {
cout << setw(66) << "Votre mot est : " << motPlayer;
cout << endl << setw(75) << "Mot n'est pas dans la liste\n\n"; //TODO: Ajouter dans essai.txt
nbTentative--;
}
}
}
bool dansListe(string motPlayer) {
fstream monFlux;
string mot;
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);
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;
}
//Affichage couleur des lettres
void afficherMotPlayer(string lettrePlayer, int positionLettre)
{
fstream monFlux;
string nomFichier;
HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE); //Pour la couleur
lettrePlayer.insert(0, "Alphabet/");
lettrePlayer.append(".txt");
nomFichier = lettrePlayer;
ouvrirFichier(monFlux, nomFichier);
printLettre(monFlux, positionLettre);
fermerFichier(monFlux);
}
void printLettre(fstream& monFlux, int positionLettre)
{
int couleurLettre;
if (positionLettre == BONNE_PLACE)
{
couleurLettre = 10;
}
else if (positionLettre == MAUVAIS_PLACE)
{
couleurLettre = 06;
}
else if (positionLettre == PAS_LA)
{
couleurLettre = 8;
}
else
{
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);
}
/*====================================
2 - OPTIONS
====================================*/
void choixNbEssai()
{
cout << "Veuillez choisir le nombre de tentative que vous souhaitez avoir."
<< "\nPar defaut, le nombre de tentative est a 6."
<< endl
<< "Nombre de tentative : ";
cin >> nbEssai;
cout << "Vous avez choisi d'avoir " << nbEssai << " tentatives."
<< endl
<< endl;
}
void choixLongeurMot() {
cout << "Veuillez choisir la longueur des mots a deviner."
<< "\nPar defaut, la longeur est a 3 lettres."
<< "\n***Pour le moment, uniquement les mots de 3 et de 4 lettres fonctionnent.***"
<< endl
<< "Longeur des mots : ";
cin >> nbLettre;
if (nbLettre == 3 || nbLettre == 4) {
cout << "Vous avez choisi d'avoir " << nbLettre << " lettres dans les mots a trouver."
<< endl
<< endl;
}
else {
nbLettre = 3;
cout << "Choix non disponible, nous avons choisis pour vous. Les mots auront 3 lettres."
<< endl
<< endl;
}
}