2 Commits

Author SHA1 Message Date
51b14f6e15 changement total de la difficulté 2024-10-16 18:05:29 +02:00
3e92ebda4a fusion 2024-10-15 11:47:35 +02:00
2 changed files with 62 additions and 51 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.out

112
pendu.c
View File

@@ -5,15 +5,28 @@
#define MAX_WORDS 14 #define MAX_WORDS 14
#define MAX_TRIES 6 #define MAX_TRIES 6
#define TIME_LIMIT 10 // Limite de temps en secondes
const char *words[MAX_WORDS] = { const char *words[MAX_WORDS] = {
"programmation", "ordinateur", "langage", "jeu", "algorithmique", "programmation",
"fontainebleau", "koala", "anticonstitutionnellement", "code", "ordinateur",
"canard", "gyroscope", "periclitation", "susurrer", "eclesiastique" "langage",
"jeu",
"algorithmique",
"fontainebleau",
"koala",
"anticonstitutionnellement",
"code",
"canard",
"gyroscope",
"periclitation",
"susurrer",
"ecclesiastique",
"test",
"oui",
"non"
}; };
// Fonction pour afficher le pendu selon le nombre d'essais incorrects
void display_hangman(int tries) { void display_hangman(int tries) {
switch (tries) { switch (tries) {
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break; case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
@@ -25,51 +38,49 @@ void display_hangman(int tries) {
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break; case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
} }
} }
// Fonction pour choisir des mots jusqu'à atteindre la longueur demandée
char *hidden_word(int length_choice) {
srand(time(NULL));
// Fonction pour mesurer le temps écoulé char *total_word = malloc((length_choice + 1) * sizeof(char));
double measure_time_taken(char *guess) { total_word[0] = '\0';
time_t start_time = time(NULL); // Démarrer le chronomètre
printf("Entrez une lettre : ");
scanf(" %c", guess); // Demande d'entrée de la lettre à deviner
time_t end_time = time(NULL); // Terminer le chronomètre
return difftime(end_time, start_time);
}
// Fonction pour traiter la lettre devinée et mettre à jour l'état du jeu int total_length = 0;
void process_guess(const char *word, char *guessed, char guess, int *guessed_correctly, int *score, int *tries) {
int found = 0;
int word_length = strlen(word);
for (int i = 0; i < word_length; i++) {
if (word[i] == guess) { while (total_length < length_choice) {
if (guessed[i] == '_') { const char *word = words[rand() % MAX_WORDS];
guessed[i] = guess; int word_length = strlen(word);
(*guessed_correctly)++;
(*score) += 10; // Bonus de points pour chaque lettre correcte if (total_length + word_length <= length_choice) {
if (total_length > 0) {
strcat(total_word, " ");
total_length++;
} }
found = 1;
strcat(total_word, word);
total_length += word_length;
} }
} }
if (!found) { return total_word;
(*tries)++;
(*score) -= 5; // Pénalité pour une lettre incorrecte
}
} }
int main() { int main() {
srand(time(NULL)); int length_choice;
const char *word = words[rand() % MAX_WORDS]; printf("Entrez la difficulté (Taille maximum du mot à deviner) : ");
scanf("%d", &length_choice);
char *word = hidden_word(length_choice);
int word_length = strlen(word); int word_length = strlen(word);
char guessed[word_length + 1]; char guessed[word_length + 1];
int tries = 0; int tries = 0;
int guessed_correctly = 0; int guessed_correctly = 0;
int score = 100; // Score initial
char guess; // Variable pour la lettre entrée
// Initialiser le mot deviné avec des underscores
for (int i = 0; i < word_length; i++) { for (int i = 0; i < word_length; i++) {
guessed[i] = '_'; guessed[i] = (word[i] == ' ') ? ' ' : '_';
} }
guessed[word_length] = '\0'; guessed[word_length] = '\0';
@@ -77,33 +88,32 @@ int main() {
printf("\nMot à deviner : %s\n", guessed); printf("\nMot à deviner : %s\n", guessed);
display_hangman(tries); display_hangman(tries);
// Appel de la fonction pour mesurer le temps char guess;
double time_taken = measure_time_taken(&guess); printf("Entrez une lettre : ");
scanf(" %c", &guess);
// Vérification du temps écoulé int found = 0;
if (time_taken > TIME_LIMIT) { for (int i = 0; i < word_length; i++) {
printf("Temps écoulé ! Vous avez mis plus de %d secondes.\n", TIME_LIMIT); if (word[i] == guess && guessed[i] == '_') {
printf("Désolé, vous avez perdu. Le mot était : %s\n", word); guessed[i] = guess;
display_hangman(MAX_TRIES); guessed_correctly++;
break; found = 1;
}
} }
// Appel de la fonction pour traiter la lettre devinée
process_guess(word, guessed, guess, &guessed_correctly, &score, &tries); if (!found) {
tries++;
printf("Temps écoulé pour cette tentative : %.2f secondes\n", time_taken); }
} }
if (guessed_correctly == word_length) { if (guessed_correctly == word_length) {
printf("Félicitations ! Vous avez deviné le mot : %s\n", word); printf("Félicitations ! Vous avez deviné le mot : %s\n", word);
if (tries < 3) {
score += 20; // Bonus si deviné en moins de 3 essais
printf("Bravo ! Bonus de points pour avoir deviné le mot en moins de 3 essais.\n");
}
} else { } else {
printf("Désolé, vous avez perdu. Le mot était : %s\n", word); printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
display_hangman(MAX_TRIES); display_hangman(MAX_TRIES);
} }
free(word);
return 0; return 0;
} }