Merge pull request 'HugoBG' (#3) from HugoBG into master
Reviewed-on: branco-g/TD3_DEV51_branco-g_nelet#3
This commit is contained in:
commit
6dabbf4aea
102
pendu.c
102
pendu.c
@ -5,9 +5,11 @@
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
||||
// Define constants for the maximum number of words and tries
|
||||
#define MAX_WORDS 14
|
||||
#define MAX_TRIES 6
|
||||
|
||||
// Array of words used in the game
|
||||
const char *words[MAX_WORDS] = {
|
||||
"programmation",
|
||||
"ordinateur",
|
||||
@ -25,6 +27,7 @@ const char *words[MAX_WORDS] = {
|
||||
"eclesiastique"
|
||||
};
|
||||
|
||||
// Function to display the hangman figure based on the number of tries
|
||||
void display_hangman(int tries) {
|
||||
switch (tries) {
|
||||
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
||||
@ -57,45 +60,80 @@ void apply_bonus(int *score, int tries) {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
// Function to randomly choose words and concatenate them to create a puzzle word
|
||||
void choose_words(char *chosen_word, int max_length) {
|
||||
int total_length = 0, used[MAX_WORDS] = {0}, word_index;
|
||||
chosen_word[0] = '\0';
|
||||
|
||||
for (int i = 0; i < word_length; i++) {
|
||||
guessed[i] = '_';
|
||||
// Select words until the maximum word length is reached or exceeded
|
||||
while (total_length < max_length) {
|
||||
word_index = rand() % MAX_WORDS;
|
||||
|
||||
if (!used[word_index] && total_length + strlen(words[word_index]) + (total_length > 0 ? 1 : 0) <= max_length) {
|
||||
if (total_length > 0) { strcat(chosen_word, " ");
|
||||
total_length++;
|
||||
}
|
||||
|
||||
strcat(chosen_word, words[word_index]);
|
||||
total_length += strlen(words[word_index]);
|
||||
used[word_index] = 1;
|
||||
if (total_length == max_length)
|
||||
break;
|
||||
}
|
||||
|
||||
// Check if all words that could fit are used
|
||||
int all_used = 1;
|
||||
for (int i = 0; i < MAX_WORDS; i++) {
|
||||
if (!used[i] && total_length + strlen(words[i]) + 1 <= max_length) { all_used = 0;
|
||||
break; }
|
||||
}
|
||||
if (all_used)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to play the hangman game
|
||||
void play_game(const char *chosen_word, int word_length, int letters_to_guess) {
|
||||
char guessed[word_length + 1];
|
||||
int tries = 0, guessed_correctly = 0, score = 0, mistakes = 0;
|
||||
|
||||
// Initialize guessed word display with underscores
|
||||
for (int i = 0; i < word_length; i++)
|
||||
guessed[i] = (chosen_word[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) {
|
||||
// Main game loop, stops if max tries are reached or the word is guessed
|
||||
while (tries < MAX_TRIES && guessed_correctly < letters_to_guess) {
|
||||
printf("\nMot à deviner : %s\n", guessed);
|
||||
display_hangman(tries);
|
||||
|
||||
// Get a letter guess from the player
|
||||
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;
|
||||
|
||||
alarm(0); //fin du compteur et sortie du programme via le signal
|
||||
int found = 0, already_revealed = 0;
|
||||
|
||||
// Check if the letter is in the word and update guessed word display
|
||||
for (int i = 0; i < word_length; i++) {
|
||||
if (word[i] == guess) {
|
||||
if (chosen_word[i] == guess) {
|
||||
if (guessed[i] == '_') {
|
||||
guessed[i] = guess;
|
||||
guessed_correctly++;
|
||||
}
|
||||
found = 1;
|
||||
} else {
|
||||
already_revealed = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
// Increment tries if the letter was not found and not already revealed
|
||||
if (!found && !already_revealed) {
|
||||
tries++;
|
||||
mistakes++;
|
||||
}
|
||||
@ -103,15 +141,37 @@ int main() {
|
||||
|
||||
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);
|
||||
// Display win or lose message based on game outcome
|
||||
if (guessed_correctly == letters_to_guess) {
|
||||
apply_bonus(&score, tries);
|
||||
printf("Félicitations ! Vous avez deviné le mot : %s\n", chosen_word);
|
||||
} else {
|
||||
printf("Désolé, vous avez perdu. Le mot était : %s\n", word);
|
||||
printf("Désolé, vous avez perdu. Le mot était : %s\n", chosen_word);
|
||||
display_hangman(MAX_TRIES);
|
||||
}
|
||||
|
||||
printf("Votre score final est : %d\n", score);
|
||||
}
|
||||
|
||||
// Main function to start the game
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
int max_word_length;
|
||||
|
||||
// Prompt user for maximum word length for the puzzle
|
||||
printf("Choisissez la taille maximum du mot à deviner : ");
|
||||
scanf("%d", &max_word_length);
|
||||
|
||||
// Select a word within the specified length
|
||||
char chosen_word[max_word_length];
|
||||
choose_words(chosen_word, max_word_length);
|
||||
|
||||
// Calculate total letters to guess (excluding spaces)
|
||||
int word_length = strlen(chosen_word), letters_to_guess = 0;
|
||||
for (int i = 0; i < word_length; i++)
|
||||
if (chosen_word[i] != ' ') letters_to_guess++;
|
||||
|
||||
// Start the game with the chosen word
|
||||
play_game(chosen_word, word_length, letters_to_guess);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user