題目內容
Hangman Judge |
In
Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows:
<ol>
<li>The contestant tries to solve to puzzle by guessing one letter at a time.</li>
<li>Every time a guess is correct, all the characters in the word that match the guess will be
turned over." For example, if your guess is o'' and the word is
book", then both o''s in the solution will be counted as
solved."______ | | | O | /|\ | | | / \ __|_ | |______ |_________|
Your task as the “Hangman Judge" is to determine, for each game, whether the contestant wins, loses, or fails to finish a game.
Input
Your program will be given a series of inputs regarding the status of a game. All input will be in lower case. The first line of each section will contain a number to indicate which round of the game is being played; the next line will be the solution to the puzzle; the last line is a sequence of the guesses made by the contestant. A round number of -1 would indicate the end of all games (and input).
Output
The output of your program is to indicate which round of the game the contestant is currently playing as well as the result of the game. There are three possible results:
You win. You lose. You chickened out.
Sample Input
1 cheese chese 2 cheese abcdefg 3 cheese abcdefgij -1
Sample Output
Round 1 You win. Round 2 You chickened out. Round 3 You lose.
Solution
#include <iostream> #include <string.h> #include <stdio.h> #define maxn 100 using namespace std; int lf, chance; char s[maxn], s2[maxn]; int win, lose; void guess( char ch){ int bad = 1; for ( int i = 0; i < strlen (s); i++) if (s[i] == ch){ lf--; s[i] = ' ' ; bad = 0; } if (bad) --chance; if (!chance) lose = 1; if (!lf) win = 1; } int main(){ int p; while (cin >> p){ if (p == -1) break ; cin >> s >> s2; cout << "Round " << p << endl; win = lose = 0; lf = strlen (s); chance = 7; for ( int i = 0; i < strlen (s2); i++){ guess(s2[i]); if (win||lose){ break ; } } if (win) cout << "You win." << endl; else if (lose) cout << "You lose." << endl; else cout << "You chickened out." << endl; } return 0; } |
No comments:
Post a Comment