Compare commits
No commits in common. "d2fd7716eac1c144a171fd38ac1f6473e523107d" and "2361771f21a831c0de1a6d60935737ceeb64afd1" have entirely different histories.
d2fd7716ea
...
2361771f21
37
pendu.c
37
pendu.c
@ -2,8 +2,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
#define MAX_WORDS 14
|
#define MAX_WORDS 14
|
||||||
#define MAX_TRIES 6
|
#define MAX_TRIES 6
|
||||||
@ -36,53 +34,26 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 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() {
|
int main() {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
const char *word = words[rand() % MAX_WORDS];
|
const char *word = words[rand() % MAX_WORDS];
|
||||||
int word_length = strlen(word);
|
int word_length = strlen(word);
|
||||||
char guessed[word_length + 1];
|
char guessed[word_length];
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
int guessed_correctly = 0;
|
int guessed_correctly = 0;
|
||||||
int mistakes = 0;
|
|
||||||
int score = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < word_length; i++) {
|
for (int i = 0; i < word_length; i++) {
|
||||||
guessed[i] = '_';
|
guessed[i] = '_';
|
||||||
}
|
}
|
||||||
guessed[word_length] = '\0';
|
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) {
|
while (tries < MAX_TRIES && guessed_correctly < word_length) {
|
||||||
printf("\nMot à deviner : %s\n", guessed);
|
printf("\nMot à deviner : %s\n", guessed);
|
||||||
display_hangman(tries);
|
display_hangman(tries);
|
||||||
char guess;
|
char guess;
|
||||||
alarm(30); //début du compteur des 30 sec a chaque tour
|
|
||||||
printf("Entrez une lettre : ");
|
printf("Entrez une lettre : ");
|
||||||
scanf(" %c", &guess);
|
scanf(" %c", &guess);
|
||||||
alarm(0); //fin du compteur et sortie du programme via le signal
|
|
||||||
int found = 0;
|
int found = 0;
|
||||||
|
|
||||||
for (int i = 0; i < word_length; i++) {
|
for (int i = 0; i < word_length; i++) {
|
||||||
@ -97,21 +68,15 @@ int main() {
|
|||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
tries++;
|
tries++;
|
||||||
mistakes++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
score = calculate_score(mistakes, start_time); //calcul le score en fonction des erreurs et du temps écoulé
|
|
||||||
|
|
||||||
if (guessed_correctly == word_length) {
|
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);
|
printf("Félicitations ! Vous avez deviné le mot : %s\n", word);
|
||||||
} 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Votre score final est : %d\n", score);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user