TD3_DEV51_Qualite_Algo/pendu.c

118 lines
3.5 KiB
C
Raw Normal View History

2024-10-14 18:41:58 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
2024-10-15 10:36:25 +02:00
#include <unistd.h>
#include <signal.h>
2024-10-14 18:41:58 +02:00
2024-10-14 18:47:11 +02:00
#define MAX_WORDS 14
2024-10-14 18:41:58 +02:00
#define MAX_TRIES 6
const char *words[MAX_WORDS] = {
"programmation",
"ordinateur",
"langage",
"jeu",
2024-10-14 18:47:11 +02:00
"algorithmique",
"fontainebleau",
"koala",
"anticonstitutionnellement",
"code",
"canard",
"gyroscope",
"periclitation",
"susurrer",
"eclesiastique"
2024-10-14 18:41:58 +02:00
};
void display_hangman(int tries) {
switch (tries) {
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
case 1: printf(" ----\n | |\n | O\n |\n |\n |\n--------\n"); break;
case 2: printf(" ----\n | |\n | O\n | |\n |\n |\n--------\n"); break;
case 3: printf(" ----\n | |\n | O\n | /|\n |\n |\n--------\n"); break;
case 4: printf(" ----\n | |\n | O\n | /|\\\n |\n |\n--------\n"); break;
case 5: printf(" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n"); break;
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
}
}
2024-10-15 11:19:32 +02:00
// Fct qui fait sortir du prog après avoir passé 30sec
2024-10-15 10:36:25 +02:00
void alarm_handler(int sig) {
printf("\nTrop tard ! Vous avez dépassé les 30 secondes.\n");
exit(1);
}
2024-10-15 11:19:32 +02:00
//FCT pour calculer le score en fonction du temps mis et des erreurs obtenus
int calculate_score(int mistakes, time_t start_time) {
time_t end_time = time(NULL);
int time_taken = (int)difftime(end_time, start_time);
int base_score = 1000 - (mistakes * 100) - time_taken;
return (base_score < 0) ? 0 : base_score;
}
//FCT pour ajouter un bonus si le joueur trouve en moins de 3 essais
void apply_bonus(int *score, int tries) {
if (tries <= 3) {
*score += 500;
}
}
2024-10-14 18:41:58 +02:00
int main() {
srand(time(NULL));
const char *word = words[rand() % MAX_WORDS];
int word_length = strlen(word);
2024-10-15 11:19:32 +02:00
char guessed[word_length + 1];
2024-10-14 18:41:58 +02:00
int tries = 0;
int guessed_correctly = 0;
2024-10-15 11:19:32 +02:00
int mistakes = 0;
int score = 0;
2024-10-14 18:41:58 +02:00
for (int i = 0; i < word_length; i++) {
guessed[i] = '_';
}
guessed[word_length] = '\0';
2024-10-15 10:36:25 +02:00
signal(SIGALRM, alarm_handler);
2024-10-15 11:19:32 +02:00
time_t start_time = time(NULL); // Début du chronomètre pour le score
2024-10-15 10:36:25 +02:00
2024-10-14 18:41:58 +02:00
while (tries < MAX_TRIES && guessed_correctly < word_length) {
printf("\nMot à deviner : %s\n", guessed);
display_hangman(tries);
char guess;
2024-10-15 11:19:32 +02:00
alarm(30); //début du compteur des 30 sec a chaque tour
2024-10-14 18:41:58 +02:00
printf("Entrez une lettre : ");
scanf(" %c", &guess);
2024-10-15 11:19:32 +02:00
alarm(0); //fin du compteur et sortie du programme via le signal
2024-10-14 18:41:58 +02:00
int found = 0;
for (int i = 0; i < word_length; i++) {
if (word[i] == guess) {
if (guessed[i] == '_') {
guessed[i] = guess;
guessed_correctly++;
}
found = 1;
}
}
if (!found) {
tries++;
2024-10-15 11:19:32 +02:00
mistakes++;
2024-10-14 18:41:58 +02:00
}
}
2024-10-15 11:19:32 +02:00
score = calculate_score(mistakes, start_time); //calcul le score en fonction des erreurs et du temps écoulé
2024-10-14 18:41:58 +02:00
if (guessed_correctly == word_length) {
2024-10-15 11:19:32 +02:00
apply_bonus(&score, tries); //aplique le bonus de score si le nb d'essais <=3
2024-10-14 18:41:58 +02:00
printf("Félicitations ! Vous avez deviné le mot : %s\n", word);
} else {
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
display_hangman(MAX_TRIES);
}
2024-10-15 11:19:32 +02:00
printf("Votre score final est : %d\n", score);
2024-10-14 18:41:58 +02:00
return 0;
}