TD3_DEV51_Qualite_Algo/pendu.c

142 lines
4.5 KiB
C
Raw Normal View History

2024-10-14 18:41:58 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
2024-10-14 18:47:11 +02:00
#define MAX_WORDS 14
2024-10-14 18:41:58 +02:00
#define MAX_TRIES 6
const char *words[MAX_WORDS] = {
"programmation",
"ordinateur",
"langage",
"jeu",
2024-10-14 18:47:11 +02:00
"algorithmique",
"fontainebleau",
"koala",
"anticonstitutionnellement",
"code",
"canard",
"gyroscope",
"periclitation",
"susurrer",
"eclesiastique"
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-24 15:57:21 +02:00
void choose_words(char *chosen_word, int max_length) {
2024-10-15 11:34:40 +02:00
int total_length = 0;
2024-10-24 15:57:21 +02:00
int used[MAX_WORDS] = {0}; // Array to mark used words
int word_index;
chosen_word[0] = '\0'; // Initialize empty string
while (total_length < max_length) {
word_index = rand() % MAX_WORDS;
// Ensure we do not exceed max_length or reuse a word
if (!used[word_index] && total_length + strlen(words[word_index]) + (total_length > 0 ? 1 : 0) <= max_length) {
// Add a space if this isn't the first word
if (total_length > 0) {
strcat(chosen_word, " ");
total_length++; // Account for the space
}
strcat(chosen_word, words[word_index]); // Add the word to the chosen word
total_length += strlen(words[word_index]);
used[word_index] = 1; // Mark the word as used
// If we're already at the exact length, break
if (total_length == max_length) {
break;
}
2024-10-15 11:34:40 +02:00
}
2024-10-24 15:57:21 +02:00
// Break if no more suitable words can be found
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;
}
2024-10-15 11:34:40 +02:00
}
2024-10-24 15:57:21 +02:00
if (all_used) break;
2024-10-15 11:34:40 +02:00
}
}
2024-10-14 18:41:58 +02:00
int main() {
srand(time(NULL));
2024-10-15 11:34:40 +02:00
2024-10-24 15:57:21 +02:00
int max_word_length;
printf("Choisissez la taille maximum du mot à deviner : ");
scanf("%d", &max_word_length);
2024-10-15 11:34:40 +02:00
2024-10-24 15:57:21 +02:00
char chosen_word[max_word_length]; // String to store the chosen combination of words
choose_words(chosen_word, max_word_length);
2024-10-15 11:34:40 +02:00
2024-10-24 15:57:21 +02:00
int word_length = strlen(chosen_word);
char guessed[word_length + 1]; // +1 for the null terminator
2024-10-14 18:41:58 +02:00
int tries = 0;
int guessed_correctly = 0;
2024-10-24 15:57:21 +02:00
int letters_to_guess = 0; // Total letters to guess (excluding spaces)
2024-10-14 18:41:58 +02:00
2024-10-24 15:57:21 +02:00
// Initialize the guessed array and count the total number of letters to guess
2024-10-14 18:41:58 +02:00
for (int i = 0; i < word_length; i++) {
2024-10-24 15:57:21 +02:00
if (chosen_word[i] == ' ') {
guessed[i] = ' '; // Spaces are pre-revealed
} else {
guessed[i] = '_';
letters_to_guess++; // Only count non-space characters
}
2024-10-14 18:41:58 +02:00
}
guessed[word_length] = '\0';
2024-10-24 15:57:21 +02:00
while (tries < MAX_TRIES && guessed_correctly < letters_to_guess) {
2024-10-14 18:41:58 +02:00
printf("\nMot à deviner : %s\n", guessed);
display_hangman(tries);
char guess;
printf("Entrez une lettre : ");
scanf(" %c", &guess);
int found = 0;
2024-10-24 15:57:21 +02:00
int already_revealed = 0;
2024-10-14 18:41:58 +02:00
2024-10-24 15:57:21 +02:00
// Check if the guessed letter is in the word
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 {
already_revealed = 1; // The letter is already revealed
}
2024-10-15 11:34:40 +02:00
}
}
2024-10-24 15:57:21 +02:00
// Only increment tries if the letter was not found and not already revealed
if (!found && !already_revealed) {
2024-10-14 18:41:58 +02:00
tries++;
}
}
2024-10-24 15:57:21 +02:00
if (guessed_correctly == letters_to_guess) {
printf("Félicitations ! Vous avez deviné le mot : %s\n", chosen_word);
2024-10-14 18:41:58 +02:00
} else {
2024-10-24 15:57:21 +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);
}
return 0;
}