Ajout difficulté #4

Open
Nathan BOUZON wants to merge 2 commits from bouzon into master
2 changed files with 49 additions and 11 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.out

59
pendu.c
View File

@ -20,7 +20,11 @@ const char *words[MAX_WORDS] = {
"gyroscope",
"periclitation",
"susurrer",
"eclesiastique"
"ecclesiastique",
"test",
"oui",
"non"
};
void display_hangman(int tries) {
@ -34,38 +38,70 @@ void display_hangman(int tries) {
case 6: printf(" ----\n | |\n | O\n | /|\\\n | / \\\n |\n--------\n"); break;
}
}
// Fonction pour choisir des mots jusqu'à atteindre la longueur demandée
char *hidden_word(int length_choice) {
srand(time(NULL));
char *total_word = malloc((length_choice + 1) * sizeof(char));
total_word[0] = '\0';
int total_length = 0;
while (total_length < length_choice) {
const char *word = words[rand() % MAX_WORDS];
int word_length = strlen(word);
if (total_length + word_length <= length_choice) {
if (total_length > 0) {
strcat(total_word, " ");
total_length++;
}
strcat(total_word, word);
total_length += word_length;
}
}
return total_word;
}
int main() {
srand(time(NULL));
const char *word = words[rand() % MAX_WORDS];
int length_choice;
printf("Entrez la difficulté (Taille maximum du mot à deviner) : ");
scanf("%d", &length_choice);
char *word = hidden_word(length_choice);
int word_length = strlen(word);
char guessed[word_length];
char guessed[word_length + 1];
int tries = 0;
int guessed_correctly = 0;
for (int i = 0; i < word_length; i++) {
guessed[i] = '_';
guessed[i] = (word[i] == ' ') ? ' ' : '_';
}
guessed[word_length] = '\0';
while (tries < MAX_TRIES && guessed_correctly < word_length) {
printf("\nMot à deviner : %s\n", guessed);
display_hangman(tries);
char guess;
printf("Entrez une lettre : ");
scanf(" %c", &guess);
int found = 0;
int found = 0;
for (int i = 0; i < word_length; i++) {
if (word[i] == guess) {
if (guessed[i] == '_') {
guessed[i] = guess;
guessed_correctly++;
}
if (word[i] == guess && guessed[i] == '_') {
guessed[i] = guess;
guessed_correctly++;
found = 1;
}
}
if (!found) {
tries++;
}
@ -78,5 +114,6 @@ int main() {
display_hangman(MAX_TRIES);
}
free(word);
return 0;
}