Hangman Judge
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:
- The contestant tries to solve to puzzle by guessing one letter at a time.
- 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.''
- Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete. Each unique wrong guess only counts against the contestant once.
______ | | | O | /|\ | | | / \ __|_ | |______ |_________|
- If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word, the contestant loses.
- If the contestant has guessed all the characters of the word before the drawing is complete, the contestant wins the game.
- If the contestant does not guess enough letters to either win or lose, the contestant chickens out.
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.
题目大意:
Hangman Judge是一个猜英文单字的小游戏(在电子字典中常会看到),游戏规则如下:
1.答案单字写在纸上(每个字符一张纸),并且被盖起来,玩家每次猜一个英文字符(letter)。
2.如果这个英文字符猜中(在答案的英文单字中有出现),被猜中的字符就被翻开。例如:答案是book,如果你猜o,book中的两个o就会被视为已猜中。
3.如果这个英文字符未出现在答案的单字中,就会在hangman的图中多加一划。要完成hangman图共需7划,如下图。注意:同一个猜错的字符只能再图上画一划,例如:答案是book,第一次你猜a(未猜中)会在图上画一划,但第二次以后再猜a并不会再多画。
4.如果在hangman图完成之前,玩家已猜中所有答案中的字符,则玩家赢(win)。
5.如果玩家尚未猜中所有答案中的字符而hangman图完成了,,则玩家输(lose)。
6.如果玩家在还没输赢的情况之下就不玩了,那我们说玩家胆小放弃了(chicken out)。
解析:这题主要用到了set集合,分别用三个set来存放,s1中的字符,s2和s1相同的字符,s1中不存在的字符,
如果不存在的字符超过7个lost,如果s1中的字符 和 s2和s1相同的字符个数一样多就win,否则chicken out。
#include <stdio.h>
#include <string.h>
#include <set>
using namespace std;
const int N = 500;
char s1[N];
char s2[N];
int main() {
int cas;
set<char> v1; //保存s1的字符
set<char> v2; //保存s2中与s1相同的字符
set<char> v3; //保存s1中不存在的字符
while(scanf("%d",&cas) != EOF && cas != -1) {
printf("Round %d\n",cas);
scanf("%s%s",s1,s2);
int len1 = strlen(s1);
int len2 = strlen(s2);
for(int i = 0; i < len1; i++) {
v1.insert(s1[i]);
}
int i;
for(i = 0; i < len2; i++) {
bool flag = true;
for(int j = 0; j < len1; j++) {
if(s2[i] == s1[j]) {
v2.insert(s2[i]);
flag = false;
break;
}
}
if(flag) {
v3.insert(s2[i]);
}
if(v3.size() == 7) {
printf("You lose.\n");
break;
}
if(v1.size() == v2.size()) {
printf("You win.\n");
break;
}
}
if( i == len2) {
printf("You chickened out.\n");
}
v1.clear();
v2.clear();
v3.clear();
}
return 0;
}