This repository has been archived on 2022-03-31. You can view files and clone it, but cannot push or open issues or pull requests.
penduEnC/main.c

125 lines
3.5 KiB
C
Raw Normal View History

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
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++;
}
2021-05-21 23:14:56 +02:00
printf("[DEBUG: mot: %s, taille mot: %d, mot caché: %s]\n", mot, taille, motCache);
return motCache;
}
2021-05-21 23:14:56 +02:00
int lettreDansMot(char mot[], int tailleMot, char lettre[]) {
for(int i = 0; i < tailleMot; i++) if(mot[i] == lettre[0]) return 1;
return 0;
}
int jeu() {
// mot aléatoire
char liste[14] = "listeMots.txt";
char * mot = recuperationMot(liste);
int tailleMot = longueurMot(mot);
int tableauLettresValidees[tailleMot];
2021-05-21 23:14:56 +02:00
char * motObfusque;
int demandeLettre;
char lettre;
for(int i = 0; i < tailleMot; i++) tableauLettresValidees[i] = 0;
// lancement jeu
short int essaisRestants = 10;
int finDuJeu = 0;
while(finDuJeu == 0) {
2021-05-21 23:14:56 +02:00
motObfusque = obfuscation(mot, tableauLettresValidees, tailleMot);
printf("\nMot à trouver : %s\n", motObfusque);
free(motObfusque);
printf("Nombre d'erreurs restantes : %hu\n", essaisRestants);
2021-05-21 23:14:56 +02:00
printf("Saisissez une lettre : ");
demandeLettre = scanf("%c\n", &lettre);
if(demandeLettre == 1) {
// vérification si lettre dans le mot
if(lettreDansMot(mot, tailleMot, &lettre) == 1) {
printf("La lettre %c est dans le mot !\n", lettre);
// verification partie gagner
printf("Le mot est %s\n", mot);
return 1;
} else {
printf("La lettre %c n'est pas dans le mot !\n", lettre);
essaisRestants--;
}
} else perror("Error");
// 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;
}