From dcceee5faf9ef8bfc529df270ae539dccb702d56 Mon Sep 17 00:00:00 2001 From: Saba Fazlali Date: Wed, 31 Jan 2024 13:42:38 +0100 Subject: [PATCH] check the validation of the guessed letter --- src/main/c/Hangman/playHangman.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/c/Hangman/playHangman.c b/src/main/c/Hangman/playHangman.c index 4712c5e..20380e5 100644 --- a/src/main/c/Hangman/playHangman.c +++ b/src/main/c/Hangman/playHangman.c @@ -14,9 +14,21 @@ void playHangman(char *wordToGuess) { // Get a letter from the player char guess; - printf("\n\t Enter your guess (lower case): "); + printf("Enter your guess: \n"); scanf(" %c", &guess); + // check if the guess is lower case and is a letter (valid) + if (!isalpha(guess) || isupper(guess)) { + printf("Please enter a valid lowercase alphabet.\n"); + continue; + } + + // Check if the letter has already been guessed by the player + if (strchr(guessedLetters, guess) != NULL) { + printf("You already guessed that letter. Try another letter.\n"); + continue; + } + // Add the guessed letter to the list guessedLetters[strlen(guessedLetters)] = guess;