Browse Source

check the validation of the guessed letter

remotes/origin/Saba
Saba Fazlali 11 months ago
parent
commit
dcceee5faf
  1. 14
      src/main/c/Hangman/playHangman.c

14
src/main/c/Hangman/playHangman.c

@ -14,9 +14,21 @@ void playHangman(char *wordToGuess) {
// Get a letter from the player // Get a letter from the player
char guess; char guess;
printf("\n\t Enter your guess (lower case): ");
printf("Enter your guess: \n");
scanf(" %c", &guess); 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 // Add the guessed letter to the list
guessedLetters[strlen(guessedLetters)] = guess; guessedLetters[strlen(guessedLetters)] = guess;

Loading…
Cancel
Save