réduire taille du main
This commit is contained in:
parent
0457b82c4a
commit
912354e062
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 total_length = 0;
|
||||||
int chosen[MAX_WORDS] = {0}; // Pour suivre les mots déjà choisis
|
int used[MAX_WORDS] = {0}; // Array to mark used words
|
||||||
int attemps = 0;
|
int word_index;
|
||||||
int max_attemps = 2 * MAX_WORDS;
|
|
||||||
|
|
||||||
while (total_length < max_length && attemps < max_attemps) {
|
chosen_word[0] = '\0'; // Initialize empty string
|
||||||
int index = rand() % MAX_WORDS;
|
|
||||||
|
while (total_length < max_length) {
|
||||||
// Si ce mot est déjà choisi ou dépasse la longueur maximale, on l'ignore
|
word_index = rand() % MAX_WORDS;
|
||||||
if (chosen[index] || total_length + strlen(words[index]) + (total_length > 0 ? 1 : 0) > max_length) {
|
|
||||||
attemps++;
|
// Ensure we do not exceed max_length or reuse a word
|
||||||
continue;
|
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
|
// Break if no more suitable words can be found
|
||||||
if (total_length > 0) {
|
int all_used = 1;
|
||||||
strcat(final_word, " "); // Ajouter un espace entre les mots
|
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]);
|
if (all_used) break;
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
// Demander la taille maximale pour le mot
|
int max_word_length;
|
||||||
int max_length;
|
printf("Choisissez la taille maximum du mot à deviner : ");
|
||||||
printf("Choisissez la taille maximale du mot à deviner : ");
|
scanf("%d", &max_word_length);
|
||||||
scanf("%d", &max_length);
|
|
||||||
|
|
||||||
// Créer la chaîne finale à deviner, basée sur la taille choisie
|
char chosen_word[max_word_length]; // String to store the chosen combination of words
|
||||||
char final_word[256] = ""; // Un buffer assez grand pour contenir plusieurs mots
|
choose_words(chosen_word, max_word_length);
|
||||||
|
|
||||||
// Choisir un ou plusieurs mots pour constituer le mot final
|
int word_length = strlen(chosen_word);
|
||||||
choose_words(max_length, final_word);
|
char guessed[word_length + 1]; // +1 for the null terminator
|
||||||
|
|
||||||
int word_length = strlen(final_word);
|
|
||||||
char guessed[word_length];
|
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
int guessed_correctly = 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++) {
|
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';
|
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);
|
printf("\nMot à deviner : %s\n", guessed);
|
||||||
display_hangman(tries);
|
display_hangman(tries);
|
||||||
char guess;
|
char guess;
|
||||||
printf("Entrez une lettre : ");
|
printf("Entrez une lettre : ");
|
||||||
scanf(" %c", &guess);
|
scanf(" %c", &guess);
|
||||||
int found = 0;
|
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++) {
|
for (int i = 0; i < word_length; i++) {
|
||||||
if (final_word[i] == guess && guessed[i] == '_') {
|
if (chosen_word[i] == guess) {
|
||||||
guessed[i] = guess;
|
if (guessed[i] == '_') {
|
||||||
found = 1;
|
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
|
// Only increment tries if the letter was not found and not already revealed
|
||||||
guessed_correctly = 0;
|
if (!found && !already_revealed) {
|
||||||
for (int i = 0; i < word_length; i++) {
|
|
||||||
if (guessed[i] != '_' && final_word[i] != ' ') {
|
|
||||||
guessed_correctly++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!found) {
|
|
||||||
tries++;
|
tries++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (guessed_correctly == word_length) {
|
if (guessed_correctly == letters_to_guess) {
|
||||||
printf("Félicitations ! Vous avez deviné le mot : %s\n", final_word);
|
printf("Félicitations ! Vous avez deviné le mot : %s\n", chosen_word);
|
||||||
} else {
|
} 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);
|
display_hangman(MAX_TRIES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user