Question
Below is a C++ hangman game code,i am not sure how to modify it so that to pass the stupid tests on hypergrade like shown
Below is a C++ hangman game code,i am not sure how to modify it so that to pass the stupid tests on hypergrade like shown #include
using namespace std;
/// @brief Puts dashes in place of alphabetic characters in the phrase. /// @param phrase the phrase to be solved /// @return the phrase with all alphabetic characters converted to dashes string setupUnsolved(string phrase) { string unsolved = ""; int len = phrase.length();
for (int i = 0; i
return unsolved; }
/// @brief Replaces the dashes with the guessed character. /// @param phrase the phrase to be solved /// @param unsolved the phrase with dashes for all unsolved characters /// @param guess the char containing the current guessed character /// @return the new unsolved string with dashes replaced by new guess string updateUnsolved(string phrase, string unsolved, char guess) { string newUnsolved = unsolved; int len = phrase.length();
for (int i = 0; i
return newUnsolved; }
/// @brief Gets valid guess as input. /// /// A guess is taken as input as a character. It is valid if /// 1) it is an alphabetic character; and /// 2) the character has not already been guessed /// /// @param prevGuesses the string containing all characters guessed so far /// @return a valid guess and only a valid guess as a character char getGuess(string prevGuesses) { int len = prevGuesses.length(); char guess; cin >> guess; guess = tolower(guess);
int flag = 1; if (!isalpha(guess)) flag = 0;
for (int i = 0; i
if (flag) return guess; else return '#'; }
int main() { string phrase; cout
string unsolved = setupUnsolved(phrase); cout
int guessLeft = 7; int finish = 0; char guess; string prevGuesses = "";
while (guessLeft > 0 && !finish) { cout
while (guess == '#') { cout
string temp = unsolved; unsolved = updateUnsolved(phrase, unsolved, guess); if (temp == unsolved) { guessLeft--; } prevGuesses += guess; cout
if (unsolved == phrase) { finish = 1; } }
cout
if (finish) cout
} This is what it looks like when my code gets into hypergrade
and this is what hypergrade asks for
basically it only accepts lowercase letter guesses, please help
Test Case 3 Enter phrase: Hello World ENTER Phrase: Enter a guess: HENTER Guessed so far: h Wrong guesses left: 7 H=n ln Enter a guess: HENTER Invalid guess! Please re-enter a guess: hENTER Invalid guess! Please re-enter a guess: 8 ENTER Invalid guess! Please re-enter a guess:) ENTER Invalid guess! Please re-enter a guess: > ENTER Invalid guess! Please re-enter a guess: ; ENTER Invalid guess! Please re-enter a guess: ! ENTER Invalid guess! Please re-enter a guess: dENTER Guessed so far: hd Wrong guesses left: 7 H=nnn Enter a guess: ENTER Guessed so far: hdo Wrong guesses left: 7 H=oOd\ln Test Case 3 Enter phrase: Hello World ENTER Phrase: -..-.-- nn Enter a guess: HENTER Invalid guess! Please re-enter a guess: H ENTER Invalid guess! Please re-enter a guess: h ENTER Guessed so far: h Wrong guesses left: 7 nn Enter a guess: 8 ENTER Invalid guess! Please re-enter a guess: ENTER Invalid guess! Please re-enter a guess: > ENTER Invalid guess! Please re-enter a guess: ; ENTER Invalid guess! Please re-enter a guess: ! ENTER Invalid guess! Please re-enter a guess: d ENTER Guessed so far: hd \ n Wrong guesses left: 7 Enter a guess: ENTER Guessed so far: hdo Wrong guesses left: 7 H=ood\lnStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started