HugoBG #3
111
pendu.c
111
pendu.c
@ -35,90 +35,105 @@ void display_hangman(int tries) {
|
||||
}
|
||||
}
|
||||
|
||||
void choose_words(int max_length, char *final_word) {
|
||||
void choose_words(char *chosen_word, int max_length) {
|
||||
int total_length = 0;
|
||||
int chosen[MAX_WORDS] = {0}; // Pour suivre les mots déjà choisis
|
||||
int attemps = 0;
|
||||
int max_attemps = 2 * MAX_WORDS;
|
||||
int used[MAX_WORDS] = {0}; // Array to mark used words
|
||||
int word_index;
|
||||
|
||||
while (total_length < max_length && attemps < max_attemps) {
|
||||
int index = rand() % MAX_WORDS;
|
||||
|
||||
// Si ce mot est déjà choisi ou dépasse la longueur maximale, on l'ignore
|
||||
if (chosen[index] || total_length + strlen(words[index]) + (total_length > 0 ? 1 : 0) > max_length) {
|
||||
attemps++;
|
||||
continue;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Si c'est le premier mot, on le copie directement, sinon on ajoute un espace et le mot
|
||||
if (total_length > 0) {
|
||||
strcat(final_word, " "); // Ajouter un espace entre les mots
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
strcat(final_word, words[index]);
|
||||
|
||||
total_length += strlen(words[index]) + (total_length > 0 ? 1 : 0); // Inclure l'espace si ce n'est pas le premier mot
|
||||
chosen[index] = 1; // Marquer ce mot comme choisi
|
||||
if (all_used) break;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
|
||||
// Demander la taille maximale pour le mot
|
||||
int max_length;
|
||||
printf("Choisissez la taille maximale du mot à deviner : ");
|
||||
scanf("%d", &max_length);
|
||||
int max_word_length;
|
||||
printf("Choisissez la taille maximum du mot à deviner : ");
|
||||
scanf("%d", &max_word_length);
|
||||
|
||||
// Créer la chaîne finale à deviner, basée sur la taille choisie
|
||||
char final_word[256] = ""; // Un buffer assez grand pour contenir plusieurs mots
|
||||
char chosen_word[max_word_length]; // String to store the chosen combination of words
|
||||
choose_words(chosen_word, max_word_length);
|
||||
|
||||
// Choisir un ou plusieurs mots pour constituer le mot final
|
||||
choose_words(max_length, final_word);
|
||||
|
||||
int word_length = strlen(final_word);
|
||||
char guessed[word_length];
|
||||
int word_length = strlen(chosen_word);
|
||||
char guessed[word_length + 1]; // +1 for the null terminator
|
||||
int tries = 0;
|
||||
int guessed_correctly = 0;
|
||||
int letters_to_guess = 0; // Total letters to guess (excluding spaces)
|
||||
|
||||
// Initialiser la chaîne des lettres devinées avec des '_'
|
||||
// Initialize the guessed array and count the total number of letters to guess
|
||||
for (int i = 0; i < word_length; i++) {
|
||||
guessed[i] = (final_word[i] == ' ') ? ' ' : '_'; // Conserver les espaces dans guessed
|
||||
if (chosen_word[i] == ' ') {
|
||||
guessed[i] = ' '; // Spaces are pre-revealed
|
||||
} else {
|
||||
guessed[i] = '_';
|
||||
letters_to_guess++; // Only count non-space characters
|
||||
}
|
||||
}
|
||||
guessed[word_length] = '\0';
|
||||
|
||||
while (tries < MAX_TRIES && guessed_correctly < word_length) {
|
||||
while (tries < MAX_TRIES && guessed_correctly < letters_to_guess) {
|
||||
printf("\nMot à deviner : %s\n", guessed);
|
||||
display_hangman(tries);
|
||||
char guess;
|
||||
printf("Entrez une lettre : ");
|
||||
scanf(" %c", &guess);
|
||||
int found = 0;
|
||||
int already_revealed = 0;
|
||||
|
||||
// Vérifier si la lettre devinée est dans le mot final
|
||||
// Check if the guessed letter is in the word
|
||||
for (int i = 0; i < word_length; i++) {
|
||||
if (final_word[i] == guess && guessed[i] == '_') {
|
||||
guessed[i] = guess;
|
||||
found = 1;
|
||||
if (chosen_word[i] == guess) {
|
||||
if (guessed[i] == '_') {
|
||||
guessed[i] = guess;
|
||||
guessed_correctly++;
|
||||
found = 1;
|
||||
} else {
|
||||
already_revealed = 1; // The letter is already revealed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Le comptage des lettres correctes doit exclure les espaces
|
||||
guessed_correctly = 0;
|
||||
for (int i = 0; i < word_length; i++) {
|
||||
if (guessed[i] != '_' && final_word[i] != ' ') {
|
||||
guessed_correctly++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
// Only increment tries if the letter was not found and not already revealed
|
||||
if (!found && !already_revealed) {
|
||||
tries++;
|
||||
}
|
||||
}
|
||||
|
||||
if (guessed_correctly == word_length) {
|
||||
printf("Félicitations ! Vous avez deviné le mot : %s\n", final_word);
|
||||
if (guessed_correctly == letters_to_guess) {
|
||||
printf("Félicitations ! Vous avez deviné le mot : %s\n", chosen_word);
|
||||
} else {
|
||||
printf("Désolé, vous avez perdu. Le mot était : %s\n", final_word);
|
||||
printf("Désolé, vous avez perdu. Le mot était : %s\n", chosen_word);
|
||||
display_hangman(MAX_TRIES);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user