Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| abd9623930 | |||
| 8309e24cc7 |
BIN
TD3 - DEV5.1.pdf
BIN
TD3 - DEV5.1.pdf
Binary file not shown.
117
pendu.c
Normal file
117
pendu.c
Normal file
@@ -0,0 +1,117 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define MAX_WORDS 14
|
||||
#define MAX_TRIES 6
|
||||
|
||||
const char *words[MAX_WORDS] = {
|
||||
"programmation",
|
||||
"ordinateur",
|
||||
"langage",
|
||||
"jeu",
|
||||
"algorithmique",
|
||||
"fontainebleau",
|
||||
"koala",
|
||||
"anticonstitutionnellement",
|
||||
"code",
|
||||
"canard",
|
||||
"gyroscope",
|
||||
"periclitation",
|
||||
"susurrer",
|
||||
"eclesiastique"
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
// Fct qui fait sortir du prog après avoir passé 30sec
|
||||
void alarm_handler(int sig) {
|
||||
printf("\nTrop tard ! Vous avez dépassé les 30 secondes.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
const char *word = words[rand() % MAX_WORDS];
|
||||
int word_length = strlen(word);
|
||||
char guessed[word_length + 1];
|
||||
int tries = 0;
|
||||
int guessed_correctly = 0;
|
||||
int mistakes = 0;
|
||||
int score = 0;
|
||||
|
||||
for (int i = 0; i < word_length; i++) {
|
||||
guessed[i] = '_';
|
||||
}
|
||||
guessed[word_length] = '\0';
|
||||
|
||||
signal(SIGALRM, alarm_handler);
|
||||
time_t start_time = time(NULL); // Début du chronomètre pour le score
|
||||
|
||||
while (tries < MAX_TRIES && guessed_correctly < word_length) {
|
||||
printf("\nMot à deviner : %s\n", guessed);
|
||||
display_hangman(tries);
|
||||
char guess;
|
||||
alarm(30); //début du compteur des 30 sec a chaque tour
|
||||
printf("Entrez une lettre : ");
|
||||
scanf(" %c", &guess);
|
||||
alarm(0); //fin du compteur et sortie du programme via le signal
|
||||
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++;
|
||||
mistakes++;
|
||||
}
|
||||
}
|
||||
|
||||
score = calculate_score(mistakes, start_time); //calcul le score en fonction des erreurs et du temps écoulé
|
||||
|
||||
if (guessed_correctly == word_length) {
|
||||
apply_bonus(&score, tries); //aplique le bonus de score si le nb d'essais <=3
|
||||
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);
|
||||
}
|
||||
|
||||
printf("Votre score final est : %d\n", score);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user