From dbdaee6be1c6d6a4578130acbbfec29433cc71c1 Mon Sep 17 00:00:00 2001 From: Saba Fazlali Date: Fri, 9 Feb 2024 18:28:32 +0100 Subject: [PATCH 1/2] 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; From aedad7f488bca8b5261865b62d3df9a07b407ea5 Mon Sep 17 00:00:00 2001 From: Saba Fazlali Date: Fri, 9 Feb 2024 18:28:47 +0100 Subject: [PATCH 2/2] hint after 3 mistakes - usage --- src/main/c/Hangman/playHangman.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/c/Hangman/playHangman.c b/src/main/c/Hangman/playHangman.c index 3dc3936..d80e197 100644 --- a/src/main/c/Hangman/playHangman.c +++ b/src/main/c/Hangman/playHangman.c @@ -40,7 +40,7 @@ char generateHint(char *wordToGuess, char *guessedLetters) { void playHangman() { char *wordToGuess; int mistakes = 0; - int score = 10; + int score = 6; char guessedLetters[30] = ""; //Guessed letters char currentGuess[50]; //Current state of the guessed word int difficulty; @@ -60,6 +60,27 @@ void playHangman() { while (mistakes < MAX_MISTAKES) { + // Asks for hint + if (mistakes == 3 && hintAvailable) { + int wantHint; + printf("Do you want a hint? (1 or 2): \n"); + printf("1. YES\n"); + printf("2. NO\n"); + + scanf(" %d", &wantHint); + if (tolower(wantHint) == 1) { + char hintLetter = generateHint(wordToGuess, guessedLetters); + if (hintLetter != '-') { + printf("Hint: %c\n", hintLetter); // Provide the hint + hintAvailable = 0; // Hint is provided, mark as unavailable + } else { + printf("No available hint.\n"); + } + } else { + printf("No hint provided.\n"); + } + } + // Get a letter from the user char input[20]; printf("Enter your guess: \n"); @@ -107,7 +128,7 @@ void playHangman() { if (strcmp(currentGuess, wordToGuess) == 0) { currentState(currentGuess, mistakes); printf("Bravo! You guessed the word: %s \n", wordToGuess); - printf("your score is %d/10\n", score); + printf("your score is %d/6\n", score); printf("\n" " __ __ __ __ _ \n" " \\ \\ / /__ _ _ \\ \\ / /__ _ _ | |\n"