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