From 2fd65615914e97ed3bd53c01a85ce12c9533e948 Mon Sep 17 00:00:00 2001 From: schied Date: Tue, 15 Oct 2024 11:32:49 +0200 Subject: [PATCH 1/2] Added difficulty --- pendu.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/pendu.c b/pendu.c index 1b0cfd6..cfa5f3b 100644 --- a/pendu.c +++ b/pendu.c @@ -35,9 +35,44 @@ void display_hangman(int tries) { } } +/*getWordByLength() function*/ +const char* getWordByLength(){ + int maxLetters; + printf("Difficulté - Entrez le nombre de lettres maximum à deviner : "); + scanf(" %d", &maxLetters); + const char *newWords[MAX_WORDS]; + int wordsFound = 0; + for (int i = 0; i < MAX_WORDS; i++) { + if(strlen(words[i]) <= maxLetters){ + newWords[wordsFound] = words[i]; + wordsFound++; + } + } + if(wordsFound==0){ + return ""; + } + const char *word = newWords[rand() % wordsFound]; + /* SECOND WORD TO GUESS (NOT WORKING) + wordsFound = 0; + for (int i = 0; i < MAX_WORDS; i++) { + if(strlen(words[i]) <= maxLetters-strlen(word)){ + newWords[wordsFound] = words[i]; + wordsFound++; + } + } + if(wordsFound != 0){ + word = (const char*) strcat((char *) word, newWords[rand() % wordsFound]); + }*/ + return word; +} + int main() { srand(time(NULL)); - const char *word = words[rand() % MAX_WORDS]; + const char *word = getWordByLength(); + if(word==""){ + printf("Aucun mot n'a été trouvé."); + return 1; + } int word_length = strlen(word); char guessed[word_length]; int tries = 0; @@ -79,4 +114,4 @@ int main() { } return 0; -} +} \ No newline at end of file From 509db169170753c2bc2382a89ab3afe4f1e3f79c Mon Sep 17 00:00:00 2001 From: schied Date: Tue, 15 Oct 2024 11:59:29 +0200 Subject: [PATCH 2/2] Added comments --- pendu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pendu.c b/pendu.c index cfa5f3b..7ede675 100644 --- a/pendu.c +++ b/pendu.c @@ -35,7 +35,12 @@ void display_hangman(int tries) { } } -/*getWordByLength() function*/ +/* +getWordByLength() function adds the possibility to the user to choose how many letters maximum the word to guess can contains. + +Returns: + A random word from the words list that correspond to the user input +*/ const char* getWordByLength(){ int maxLetters; printf("Difficulté - Entrez le nombre de lettres maximum à deviner : ");