Merge branch 'master' into HugoBG
This commit is contained in:
commit
da54858009
26
Readme.md
Normal file
26
Readme.md
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# Compte rendu TD3
|
||||||
|
Groupe composé de Hugo Branco Gomes et Jean-Luc Nelet
|
||||||
|
|
||||||
|
# 14/10/2024
|
||||||
|
### Exercice 3
|
||||||
|
### Revue de code
|
||||||
|
**Revue du code de Jean-Luc par Hugo**
|
||||||
|
Merge Request accepté, aucun problème détecté. Les conditions de l'ex3 ont bien été respectées.
|
||||||
|
|
||||||
|
**Revue du code de Hugo par Jean-Luc**
|
||||||
|
Merge request refusé, le code compile mais ne retourne pas ce qui est demandé (lorsque qu'on trouve le résultat du pendu pour 2 mots ou plus, le jeu renvoie un message d'erreur)
|
||||||
|
|
||||||
|
### Exercice 4
|
||||||
|
### Profiler avec gprof
|
||||||
|
Nous n'avons pas trouvé l'utilité de gprof étant donné que le programme éxécuté est en tour par tour donc il n'y a pas de métrique mesurable telle que le temps, le nombre d'appel de fonctions etc. Il en est de même pour l'arbre d'appel du programme.
|
||||||
|
|
||||||
|
### Complexité cyclomatique des fonctions
|
||||||
|
* display_hangman : 7
|
||||||
|
* alarm_handler : 1
|
||||||
|
* calculate_score : 1
|
||||||
|
* apply_bonus : 2
|
||||||
|
* main : 9
|
||||||
|
|
||||||
|
|
||||||
|
-------
|
||||||
|
|
38
pendu.c
38
pendu.c
@ -2,6 +2,8 @@
|
|||||||
#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 constants for the maximum number of words and tries
|
// Define constants for the maximum number of words and tries
|
||||||
#define MAX_WORDS 14
|
#define MAX_WORDS 14
|
||||||
@ -37,6 +39,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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Function to randomly choose words and concatenate them to create a puzzle word
|
// Function to randomly choose words and concatenate them to create a puzzle word
|
||||||
void choose_words(char *chosen_word, int max_length) {
|
void choose_words(char *chosen_word, int max_length) {
|
||||||
@ -73,13 +95,16 @@ void choose_words(char *chosen_word, int max_length) {
|
|||||||
// Function to play the hangman game
|
// Function to play the hangman game
|
||||||
void play_game(const char *chosen_word, int word_length, int letters_to_guess) {
|
void play_game(const char *chosen_word, int word_length, int letters_to_guess) {
|
||||||
char guessed[word_length + 1];
|
char guessed[word_length + 1];
|
||||||
int tries = 0, guessed_correctly = 0;
|
int tries = 0, guessed_correctly = 0, score = 0, mistakes = 0;
|
||||||
|
|
||||||
// Initialize guessed word display with underscores
|
// Initialize guessed word display with underscores
|
||||||
for (int i = 0; i < word_length; i++)
|
for (int i = 0; i < word_length; i++)
|
||||||
guessed[i] = (chosen_word[i] == ' ') ? ' ' : '_';
|
guessed[i] = (chosen_word[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
|
||||||
|
|
||||||
// Main game loop, stops if max tries are reached or the word is guessed
|
// Main game loop, stops if max tries are reached or the word is guessed
|
||||||
while (tries < MAX_TRIES && guessed_correctly < letters_to_guess) {
|
while (tries < MAX_TRIES && guessed_correctly < letters_to_guess) {
|
||||||
printf("\nMot à deviner : %s\n", guessed);
|
printf("\nMot à deviner : %s\n", guessed);
|
||||||
@ -87,9 +112,11 @@ void play_game(const char *chosen_word, int word_length, int letters_to_guess) {
|
|||||||
|
|
||||||
// Get a letter guess from the player
|
// Get a letter guess from the player
|
||||||
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, already_revealed = 0;
|
int found = 0, already_revealed = 0;
|
||||||
|
|
||||||
// Check if the letter is in the word and update guessed word display
|
// Check if the letter is in the word and update guessed word display
|
||||||
@ -106,17 +133,24 @@ void play_game(const char *chosen_word, int word_length, int letters_to_guess) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Increment tries if the letter was not found and not already revealed
|
// Increment tries if the letter was not found and not already revealed
|
||||||
if (!found && !already_revealed)
|
if (!found && !already_revealed) {
|
||||||
tries++;
|
tries++;
|
||||||
|
mistakes++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
score = calculate_score(mistakes, start_time); //calcul le score en fonction des erreurs et du temps écoulé
|
||||||
|
|
||||||
// Display win or lose message based on game outcome
|
// Display win or lose message based on game outcome
|
||||||
if (guessed_correctly == letters_to_guess)
|
if (guessed_correctly == letters_to_guess)
|
||||||
|
apply_bonus(&score, tries);
|
||||||
printf("Félicitations ! Vous avez deviné le mot : %s\n", chosen_word);
|
printf("Félicitations ! Vous avez deviné le mot : %s\n", chosen_word);
|
||||||
else {
|
else {
|
||||||
printf("Désolé, vous avez perdu. Le mot était : %s\n", chosen_word);
|
printf("Désolé, vous avez perdu. Le mot était : %s\n", chosen_word);
|
||||||
display_hangman(MAX_TRIES);
|
display_hangman(MAX_TRIES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("Votre score final est : %d\n", score);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main function to start the game
|
// Main function to start the game
|
||||||
|
Loading…
Reference in New Issue
Block a user