commit 955f9054305e1f638d8792c5c1aca596917e0f8a Author: Mylloon Date: Fri May 21 22:31:28 2021 +0200 recuperation mot dans fichier obfuscation mot début affichage jeu diff --git a/main.c b/main.c new file mode 100644 index 0000000..a4d4b8e --- /dev/null +++ b/main.c @@ -0,0 +1,113 @@ +#include +#include +#include + +int longueurMot(const char mot[]) { + int taille; + for(taille = 0; mot[taille] != 0; taille++); + + return taille; +} + +char * recuperationMot(const char file[]) { + int tailleMaxMot = 25; + char * mot = malloc(tailleMaxMot * sizeof(char)); + + // gestion erreur + FILE * fp = fopen(file, "r"); + if(fp == NULL) { + perror("Error"); + } + + long int tailleFichier = 0; + // on récupère le nombre de mots qu'il y a dans le fichier + while(fgets(mot, tailleMaxMot, fp) != NULL) tailleFichier++; + + // chiffre aléatoire pour mot aléatoire + srand(time(NULL)); + int chiffreAleatoire = (rand() % tailleFichier) + 1; + + // recuperation du mot sélectionner + tailleFichier = 0; + rewind(fp); // reset le pointeur au début du fichier + while(fgets(mot, tailleMaxMot, fp) != NULL) { + tailleFichier++; + if(tailleFichier == chiffreAleatoire) break; + } + + // fermeture du fichier + fclose(fp); + + int tailleMot = longueurMot(mot); + if(mot[tailleMot - 1] == '\n') mot[tailleMot - 1] = 0; + + return mot; +} + +char * obfuscation(char mot[], int lettresValidees[], int taille) { + int tailleMot = taille * 2; + char * motCache = malloc(tailleMot * sizeof(char)); + int j = 0; + for(int i = 0; i < taille; i++) { + if(lettresValidees[i] != 1) motCache[j] = '_'; else motCache[j] = mot[i]; + j++; + motCache[j] = ' '; + j++; + } + + printf("mot: %s, taille mot: %d, mot caché: %s\n", mot, taille, motCache); + + return motCache; +} + +int jeu() { + // mot aléatoire + char liste[14] = "listeMots.txt"; + char * mot = recuperationMot(liste); + int tailleMot = longueurMot(mot); + int tableauLettresValidees[tailleMot]; + for(int i = 0; i < tailleMot; i++) tableauLettresValidees[i] = 0; + + // lancement jeu + short int essaisRestants = 10; + int finDuJeu = 0; + while(finDuJeu == 0) { + char * motObfusque = obfuscation(mot, tableauLettresValidees, tailleMot); + printf("\nMot à trouver : %s\n", motObfusque); + free(motObfusque); + printf("Nombre d'erreurs restantes : %hu\n", essaisRestants); + printf("Saisissez une lettre : \n"); + // vérification si lettre dans le mot + + // si oui + printf("La lettre x est dans le mot !\n"); + // verification partie gagner + printf("Le mot est "); + return 1; + + // si non + printf("La lettre x n'est pas dans le mot !\n"); + essaisRestants--; + // verification plus d'essais restants + if(essaisRestants == 0) finDuJeu = 1; + + } + + free(mot); + return 0; +} + +int main(const int argc, const char * argv[]) { + printf("-- Jeu du Pendu --\n"); + if(argc > 1) { + if(argc > 2) { + printf("Trop d'arguments renseignés.\n"); + return 0; + } + argv++; + if(jeu(argv) == 1) printf("\\o/ Bravo ! Vous remportez la partie ! \\o/\n"); + else printf("Euh.. sérieux ? Vous perdez la partie.\n"); + } else printf("Veuillez préciser le dictionnaire à utiliser.\n"); + + return 0; +}