|
|
@ -1,9 +1,10 @@ |
|
|
|
#include "playHangman.h" |
|
|
|
|
|
|
|
|
|
|
|
void playHangman(char *wordToGuess) { |
|
|
|
int mistakes = 0; |
|
|
|
char guessedLetters[30]; //guessed letters |
|
|
|
char currentGuess[50]; //current state of the guessed word |
|
|
|
char guessedLetters[30]; //Guessed letters |
|
|
|
char currentGuess[50]; //Current state of the guessed word |
|
|
|
|
|
|
|
// Initialize the current guess and print the rules |
|
|
|
initializeHangman(wordToGuess, currentGuess); |
|
|
@ -17,7 +18,7 @@ void playHangman(char *wordToGuess) { |
|
|
|
printf("Enter your guess: \n"); |
|
|
|
scanf(" %c", &guess); |
|
|
|
|
|
|
|
// check if the guess is lower case and is a letter (valid) |
|
|
|
// 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; |
|
|
@ -42,19 +43,19 @@ void playHangman(char *wordToGuess) { |
|
|
|
} |
|
|
|
|
|
|
|
// Update mistakes (if the guess is wrong) |
|
|
|
if(!found){ |
|
|
|
if (!found) { |
|
|
|
mistakes++; |
|
|
|
} |
|
|
|
|
|
|
|
// Win: Check if the player guessed all the letters |
|
|
|
if (strcmp(currentGuess,wordToGuess) == 0){ |
|
|
|
if (strcmp(currentGuess, wordToGuess) == 0) { |
|
|
|
currentState(currentGuess, mistakes); |
|
|
|
printf("Bravo! You guessed the word: %s \n", wordToGuess); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
// Lose: Print the answer + game over |
|
|
|
if (mistakes == MAX_MISTAKES){ |
|
|
|
if (mistakes == MAX_MISTAKES) { |
|
|
|
currentState(currentGuess, mistakes); |
|
|
|
printf("Oops! You have no more guesses :( \n The answer was: %s \n", wordToGuess); |
|
|
|
} |
|
|
|