From dbdaee6be1c6d6a4578130acbbfec29433cc71c1 Mon Sep 17 00:00:00 2001 From: Saba Fazlali Date: Fri, 9 Feb 2024 18:28:32 +0100 Subject: [PATCH] hint after 3 mistakes - function impl --- src/main/c/Hangman/playHangman.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/c/Hangman/playHangman.c b/src/main/c/Hangman/playHangman.c index 59fa7cc..3dc3936 100644 --- a/src/main/c/Hangman/playHangman.c +++ b/src/main/c/Hangman/playHangman.c @@ -9,6 +9,34 @@ char toLower(char ch) { return ch; } +// Hint function +char *hint = NULL; +int hintAvailable = 1; + +char generateHint(char *wordToGuess, char *guessedLetters) { + int length = strlen(wordToGuess); + char availableLetters[length]; // Array to store letters not guessed yet + int numAvailableLetters = 0; + + // Check each letter in the word + for (int i = 0; i < length; i++) { + char letter = wordToGuess[i]; + // Check if the letter has not been guessed + if (strchr(guessedLetters, letter) == NULL) { + availableLetters[numAvailableLetters++] = letter; // Store the available letter + } + } + + // Randomly select a letter from available letters + if (numAvailableLetters > 0) { + int randomIndex = rand() % numAvailableLetters; + return availableLetters[randomIndex]; + } else { + // No available letters, return a placeholder value + return '-'; + } +} + void playHangman() { char *wordToGuess; int mistakes = 0;