faire la mr

This commit is contained in:
Hugo BRANCO-GOMES 2024-10-25 15:48:43 +02:00
parent f4de9cf8bd
commit 245f56be9c

61
pendu.c
View File

@ -9,9 +9,20 @@
// Array of words used in the game
const char *words[MAX_WORDS] = {
"programmation", "ordinateur", "langage", "jeu", "algorithmique",
"fontainebleau", "koala", "anticonstitutionnellement", "code",
"canard", "gyroscope", "periclitation", "susurrer", "eclesiastique"
"programmation",
"ordinateur",
"langage",
"jeu",
"algorithmique",
"fontainebleau",
"koala",
"anticonstitutionnellement",
"code",
"canard",
"gyroscope",
"periclitation",
"susurrer",
"eclesiastique"
};
// Function to display the hangman figure based on the number of tries
@ -28,7 +39,6 @@ void display_hangman(int tries) {
}
// Function to randomly choose words and concatenate them to create a puzzle word
// Ensures that the maximum word length is respected by avoiding overflow with strncat
void choose_words(char *chosen_word, int max_length) {
int total_length = 0, used[MAX_WORDS] = {0}, word_index;
chosen_word[0] = '\0';
@ -36,50 +46,48 @@ void choose_words(char *chosen_word, int max_length) {
// Select words until the maximum word length is reached or exceeded
while (total_length < max_length) {
word_index = rand() % MAX_WORDS;
int word_length = strlen(words[word_index]);
int additional_space = (total_length > 0 ? 1 : 0); // Space between words if needed
// Ensure word fits within the remaining length
if (!used[word_index] && total_length + word_length + additional_space <= max_length) {
if (additional_space > 0) {
strncat(chosen_word, " ", max_length - total_length - 1);
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++;
}
strncat(chosen_word, words[word_index], max_length - total_length - 1);
total_length += word_length;
strcat(chosen_word, words[word_index]);
total_length += strlen(words[word_index]);
used[word_index] = 1;
if (total_length == max_length)
break;
}
// Break if no other words can fit in the remaining space
// 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 (!used[i] && total_length + strlen(words[i]) + 1 <= max_length) { all_used = 0;
break; }
}
if (all_used) break;
if (all_used)
break;
}
}
// Function to play the hangman game
// Displays the game state and checks if each guess is correct or already revealed
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;
// Initialize guessed word display with underscores
for (int i = 0; i < word_length; i++) guessed[i] = (chosen_word[i] == ' ') ? ' ' : '_';
for (int i = 0; i < word_length; i++)
guessed[i] = (chosen_word[i] == ' ') ? ' ' : '_';
guessed[word_length] = '\0';
// Main game loop, stops if max tries are reached or the word is guessed
while (tries < MAX_TRIES && guessed_correctly < letters_to_guess) {
printf("\nWord to guess: %s\n", guessed);
printf("\nMot à deviner : %s\n", guessed);
display_hangman(tries);
// Get a letter guess from the player
char guess;
printf("Enter a letter: ");
printf("Entrez une lettre : ");
scanf(" %c", &guess);
int found = 0, already_revealed = 0;
@ -98,14 +106,15 @@ 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
if (!found && !already_revealed) tries++;
if (!found && !already_revealed)
tries++;
}
// Display win or lose message based on game outcome
if (guessed_correctly == letters_to_guess)
printf("Congratulations! You've guessed the word: %s\n", chosen_word);
printf("Félicitations ! Vous avez deviné le mot : %s\n", chosen_word);
else {
printf("Sorry, you've lost. The word was: %s\n", chosen_word);
printf("Désolé, vous avez perdu. Le mot était : %s\n", chosen_word);
display_hangman(MAX_TRIES);
}
}
@ -116,7 +125,7 @@ int main() {
int max_word_length;
// Prompt user for maximum word length for the puzzle
printf("Enter the maximum word length to guess: ");
printf("Choisissez la taille maximum du mot à deviner : ");
scanf("%d", &max_word_length);
// Select a word within the specified length