2024-10-14 18:41:58 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Define constants for the maximum number of words and tries
|
2024-10-14 18:47:11 +02:00
|
|
|
#define MAX_WORDS 14
|
2024-10-14 18:41:58 +02:00
|
|
|
#define MAX_TRIES 6
|
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Array of words used in the game
|
2024-10-14 18:41:58 +02:00
|
|
|
const char *words[MAX_WORDS] = {
|
2024-10-25 15:48:43 +02:00
|
|
|
"programmation",
|
|
|
|
"ordinateur",
|
|
|
|
"langage",
|
|
|
|
"jeu",
|
|
|
|
"algorithmique",
|
|
|
|
"fontainebleau",
|
|
|
|
"koala",
|
|
|
|
"anticonstitutionnellement",
|
|
|
|
"code",
|
|
|
|
"canard",
|
|
|
|
"gyroscope",
|
|
|
|
"periclitation",
|
|
|
|
"susurrer",
|
|
|
|
"eclesiastique"
|
2024-10-14 18:41:58 +02:00
|
|
|
};
|
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Function to display the hangman figure based on the number of tries
|
2024-10-14 18:41:58 +02:00
|
|
|
void display_hangman(int tries) {
|
|
|
|
switch (tries) {
|
|
|
|
case 0: printf(" ----\n | |\n |\n |\n |\n |\n--------\n"); break;
|
|
|
|
case 1: printf(" ----\n | |\n | O\n |\n |\n |\n--------\n"); break;
|
|
|
|
case 2: printf(" ----\n | |\n | O\n | |\n |\n |\n--------\n"); break;
|
|
|
|
case 3: printf(" ----\n | |\n | O\n | /|\n |\n |\n--------\n"); break;
|
|
|
|
case 4: printf(" ----\n | |\n | O\n | /|\\\n |\n |\n--------\n"); break;
|
|
|
|
case 5: printf(" ----\n | |\n | O\n | /|\\\n | /\n |\n--------\n"); break;
|
|
|
|
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Function to randomly choose words and concatenate them to create a puzzle word
|
2024-10-24 15:57:21 +02:00
|
|
|
void choose_words(char *chosen_word, int max_length) {
|
2024-10-25 15:39:23 +02:00
|
|
|
int total_length = 0, used[MAX_WORDS] = {0}, word_index;
|
|
|
|
chosen_word[0] = '\0';
|
2024-10-24 15:57:21 +02:00
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Select words until the maximum word length is reached or exceeded
|
2024-10-24 15:57:21 +02:00
|
|
|
while (total_length < max_length) {
|
|
|
|
word_index = rand() % MAX_WORDS;
|
2024-10-25 15:48:43 +02:00
|
|
|
|
|
|
|
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++;
|
2024-10-25 15:45:38 +02:00
|
|
|
}
|
2024-10-25 15:48:43 +02:00
|
|
|
|
|
|
|
strcat(chosen_word, words[word_index]);
|
|
|
|
total_length += strlen(words[word_index]);
|
2024-10-25 15:39:23 +02:00
|
|
|
used[word_index] = 1;
|
2024-10-25 15:48:43 +02:00
|
|
|
if (total_length == max_length)
|
|
|
|
break;
|
2024-10-15 11:34:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-25 15:48:43 +02:00
|
|
|
// Check if all words that could fit are used
|
2024-10-24 15:57:21 +02:00
|
|
|
int all_used = 1;
|
|
|
|
for (int i = 0; i < MAX_WORDS; i++) {
|
2024-10-25 15:48:43 +02:00
|
|
|
if (!used[i] && total_length + strlen(words[i]) + 1 <= max_length) { all_used = 0;
|
|
|
|
break; }
|
2024-10-15 11:34:40 +02:00
|
|
|
}
|
2024-10-25 15:48:43 +02:00
|
|
|
if (all_used)
|
|
|
|
break;
|
2024-10-15 11:34:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// 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;
|
2024-10-15 11:34:40 +02:00
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Initialize guessed word display with underscores
|
2024-10-25 15:48:43 +02:00
|
|
|
for (int i = 0; i < word_length; i++)
|
|
|
|
guessed[i] = (chosen_word[i] == ' ') ? ' ' : '_';
|
2024-10-14 18:41:58 +02:00
|
|
|
guessed[word_length] = '\0';
|
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Main game loop, stops if max tries are reached or the word is guessed
|
2024-10-24 15:57:21 +02:00
|
|
|
while (tries < MAX_TRIES && guessed_correctly < letters_to_guess) {
|
2024-10-25 15:48:43 +02:00
|
|
|
printf("\nMot à deviner : %s\n", guessed);
|
2024-10-14 18:41:58 +02:00
|
|
|
display_hangman(tries);
|
2024-10-25 15:39:23 +02:00
|
|
|
|
|
|
|
// Get a letter guess from the player
|
2024-10-14 18:41:58 +02:00
|
|
|
char guess;
|
2024-10-25 15:48:43 +02:00
|
|
|
printf("Entrez une lettre : ");
|
2024-10-14 18:41:58 +02:00
|
|
|
scanf(" %c", &guess);
|
2024-10-25 15:39:23 +02:00
|
|
|
|
|
|
|
int found = 0, already_revealed = 0;
|
2024-10-14 18:41:58 +02:00
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Check if the letter is in the word and update guessed word display
|
2024-10-15 11:34:40 +02:00
|
|
|
for (int i = 0; i < word_length; i++) {
|
2024-10-24 15:57:21 +02:00
|
|
|
if (chosen_word[i] == guess) {
|
|
|
|
if (guessed[i] == '_') {
|
|
|
|
guessed[i] = guess;
|
|
|
|
guessed_correctly++;
|
|
|
|
found = 1;
|
|
|
|
} else {
|
2024-10-25 15:39:23 +02:00
|
|
|
already_revealed = 1;
|
2024-10-24 15:57:21 +02:00
|
|
|
}
|
2024-10-15 11:34:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Increment tries if the letter was not found and not already revealed
|
2024-10-25 15:48:43 +02:00
|
|
|
if (!found && !already_revealed)
|
|
|
|
tries++;
|
2024-10-14 18:41:58 +02:00
|
|
|
}
|
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Display win or lose message based on game outcome
|
|
|
|
if (guessed_correctly == letters_to_guess)
|
2024-10-25 15:48:43 +02:00
|
|
|
printf("Félicitations ! Vous avez deviné le mot : %s\n", chosen_word);
|
2024-10-25 15:39:23 +02:00
|
|
|
else {
|
2024-10-25 15:48:43 +02:00
|
|
|
printf("Désolé, vous avez perdu. Le mot était : %s\n", chosen_word);
|
2024-10-14 18:41:58 +02:00
|
|
|
display_hangman(MAX_TRIES);
|
|
|
|
}
|
2024-10-25 15:39:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Main function to start the game
|
|
|
|
int main() {
|
|
|
|
srand(time(NULL));
|
|
|
|
int max_word_length;
|
|
|
|
|
|
|
|
// Prompt user for maximum word length for the puzzle
|
2024-10-25 15:48:43 +02:00
|
|
|
printf("Choisissez la taille maximum du mot à deviner : ");
|
2024-10-25 15:39:23 +02:00
|
|
|
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++;
|
2024-10-14 18:41:58 +02:00
|
|
|
|
2024-10-25 15:39:23 +02:00
|
|
|
// Start the game with the chosen word
|
|
|
|
play_game(chosen_word, word_length, letters_to_guess);
|
2024-10-14 18:41:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|