From 3e92ebda4a64692449221a22595b3395e7a6e3f7 Mon Sep 17 00:00:00 2001 From: bouzon Date: Tue, 15 Oct 2024 11:47:35 +0200 Subject: [PATCH 1/2] fusion --- .gitignore | 1 + pendu.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f47cb20 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.out diff --git a/pendu.c b/pendu.c index 1b0cfd6..4e3a5c9 100644 --- a/pendu.c +++ b/pendu.c @@ -20,7 +20,9 @@ const char *words[MAX_WORDS] = { "gyroscope", "periclitation", "susurrer", - "eclesiastique" + "eclesiastique", + "de", + "a" }; void display_hangman(int tries) { @@ -35,14 +37,53 @@ void display_hangman(int tries) { } } -int main() { +char *hidden_word(int length_choice) { + srand(time(NULL)); - const char *word = words[rand() % MAX_WORDS]; + char word = words[rand() % MAX_WORDS]; int word_length = strlen(word); + int stay_length = length_choice; + char *total_word; + int number_of_word = 0; + int total_length = word_length; + + for(int y=0; word_length > length_choice; y++){ + word = *words[rand() % MAX_WORDS]; + word_length = strlen(word); + } + + total_word = word; + + for(int y=0; word_length > stay_length; y++){ + + word = *words[rand() % MAX_WORDS]; + word_length = strlen(total_word); + + if(stay_length >= word_length){ + total_word = word +'\0'+ *total_word; + total_length = strlen(total_word); + stay_length = total_length - length_choice; + } + } + + + + return total_word; +} + +int main() { + int length_choice = 26; + 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]; int tries = 0; int guessed_correctly = 0; + for (int i = 0; i < word_length; i++) { guessed[i] = '_'; } -- 2.47.0 From 51b14f6e15506f1af9fb8da1515abdcf0f351f58 Mon Sep 17 00:00:00 2001 From: Nathan BOUZON Date: Wed, 16 Oct 2024 18:05:29 +0200 Subject: [PATCH 2/2] =?UTF-8?q?changement=20total=20de=20la=20difficult?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pendu.c | 70 +++++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/pendu.c b/pendu.c index 4e3a5c9..f01822c 100644 --- a/pendu.c +++ b/pendu.c @@ -20,9 +20,11 @@ const char *words[MAX_WORDS] = { "gyroscope", "periclitation", "susurrer", - "eclesiastique", - "de", - "a" + "ecclesiastique", + "test", + "oui", + "non" + }; void display_hangman(int tries) { @@ -36,77 +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 word = words[rand() % MAX_WORDS]; - int word_length = strlen(word); - int stay_length = length_choice; - char *total_word; - int number_of_word = 0; - int total_length = word_length; - for(int y=0; word_length > length_choice; y++){ - word = *words[rand() % MAX_WORDS]; - word_length = strlen(word); - } + char *total_word = malloc((length_choice + 1) * sizeof(char)); + total_word[0] = '\0'; - total_word = word; + int total_length = 0; - for(int y=0; word_length > stay_length; y++){ - - word = *words[rand() % MAX_WORDS]; - word_length = strlen(total_word); - if(stay_length >= word_length){ - total_word = word +'\0'+ *total_word; - total_length = strlen(total_word); - stay_length = total_length - length_choice; + 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; + return total_word; } int main() { - int length_choice = 26; + int length_choice; printf("Entrez la difficulté (Taille maximum du mot à deviner) : "); - scanf(" %d", &length_choice); + 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++; } @@ -119,5 +114,6 @@ int main() { display_hangman(MAX_TRIES); } + free(word); return 0; } -- 2.47.0