UVa 00489 – Hangman Judge
Hangman Judge is a small game where players guess English words (commonly seen in electronic dictionaries). The rules of the game are as follows:
- The answer word is written on paper (with each character on a separate piece), and it is covered up. Players guess one English letter at a time.
- If the guessed letter is correct (appears in the answer English word), the guessed letter is revealed. For example: If the answer is "book" and you guess "o", both "o"s in "book" are considered guessed correctly.
- If the guessed letter does not appear in the answer word, one more stroke is added to the hangman's drawing. **Completing the hangman drawing requires a total of 7 strokes**. Note: The same incorrect guessed letter can only be drawn once on the drawing. For example: If the answer is "book" and you guess "a" the first time (incorrectly), one stroke is drawn on the hangman's drawing, but if you guess "a" again after that, no additional strokes are added.
- If the player correctly guesses all the characters in the answer before completing the hangman's drawing, the player wins.
- If the player has not guessed all the characters in the answer before completing the hangman's drawing, the player loses.
- If the player quits the game without winning or losing, we say the player "chickened out."
Your task is to write a program that determines whether the player wins, loses, or chickens out based on the answer and the player's input guesses.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
EOF inputs: each set consists of 3 lines. The first line contains a number. N, indicating the round number. The second line is the answer for this round, and the third line is the player's input guess for this round. If N = -1, it means the input ends. | Please output each round and the game result. There are only three possible game results: You win. You lose. You chickened out. |
1 cheese chese 2 cheese abcdefg 3 cheese abcdefgij -1 | Round 1 You win. Round 2 You chickened out. Round 3 You lose. |
Thought Process
When receiving the guessed string, run a for loop to check each character. If a character is guessed correctly, set the Map value of that character in the answer to zero. If guessed incorrectly, increase the count of incorrect guesses by 1. If the count exceeds 7, output "lose"; if the count is within 7 and the answer is guessed correctly, output "win". If after the for loop, no output has been generated, it means the player has chickened out, so output the specified string.
Sample Code-ZeroJudge D217: Hangman Judge
#include <iostream>
#include <map>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
while (cin >> N && N != -1)
{
string str;
cin >> str;
map<char, int>ans, guess;
for (int i = 0; i<str.length(); i++)
{
ans[str[i]] = 1;
}
cin >> str;
int count = 0;
bool chicken = true;
for (int i = 0; i<str.length(); i++)
{
if (ans[str[i]] == 1)
{
ans[str[i]] = 0;
count--;
}
if (guess[str[i]] == 0)
{
guess[str[i]]++;
count++;
}
if (count >= 7)
{
chicken = false;
cout << "Round " << N << "\n";
cout << "You lose.\n";
break;
}
bool ok = true;
for (auto it:ans)
{
if (it.second == 1)
{
ok = false;
break;
}
}
if (ok)
{
chicken = false;
cout << "Round " << N << "\n";
cout << "You win.\n";
break;
}
}
if (chicken && count < 7)
{
cout << "Round " << N << "\n";
cout << "You chickened out.\n";
}
}
}
//ZeroJudge D217
//Dr. SeanXD