ajout score
This commit is contained in:
parent
8309e24cc7
commit
abd9623930
32
pendu.c
32
pendu.c
@ -36,19 +36,36 @@ 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) {
|
void alarm_handler(int sig) {
|
||||||
printf("\nTrop tard ! Vous avez dépassé les 30 secondes.\n");
|
printf("\nTrop tard ! Vous avez dépassé les 30 secondes.\n");
|
||||||
exit(1);
|
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];
|
char guessed[word_length + 1];
|
||||||
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] = '_';
|
||||||
@ -56,15 +73,16 @@ int main() {
|
|||||||
guessed[word_length] = '\0';
|
guessed[word_length] = '\0';
|
||||||
|
|
||||||
signal(SIGALRM, alarm_handler);
|
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);
|
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);
|
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++) {
|
||||||
@ -79,15 +97,21 @@ 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