您的位置:首页 > 其它

UVa 489 Hangman Judge(刽子手游戏)

2013-08-02 10:29 405 查看


 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.


题目大意:

刽子手游戏的规则:

1.参赛人通过每次猜一个字母来揭开谜底;

2.每猜对一个字母,那么谜底中所有与猜测相同的字母都翻开(显示),例如:如果你猜的是‘o’,而单词是“book”,那么两个‘o’都算被解答出来;

3.没错一次,刽子手就在题目所示的图片中添一笔,需要7次才能完成。每一次独特的错误算一次(也就是说重复的错误不算);

4.如果图片在猜出所有字母前完成,那么参赛者判输;

5.如果提前猜出所有单词,那么参赛者判赢;

6.如果没有猜出足够的字母,既没赢也没输,那么判“chickens out”。

解析:

因为字母只算一次无论对错,所以我用一个alph[]数组来记录该字母是否使用过,没有猜过才进行对错的判断;

注意:这里输和赢都得提前判断出,不然容易发生错误!!!

CODE:

/*
给几个例子,
1.注意正确提起输出,
2.猜字母可以是无序的,
3.每一次猜测的字母只算一次,重复错误不记录
4.错误要也要提前输出
5.注意“chickened out.”的形式

输入:
1
china
chineseabcdefg
2
sleep
leps
3
happy
ssssssssss
4
happy
bcdefgijhapy
5
cheers
cer
-1

输出:
Round 1
You win.
Round 2
You win.
Round 3
You chickened out.
Round 4
You lose.
Round 5
You chickened out.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 1010

char ans[MAXN];
char guess[MAXN];
int alpha[26];

int main(){
int i, j, n;
int len1,len2;
int flag, count;

while(~scanf("%d", &n)&&n!=-1){
scanf("%s", ans);
scanf("%s", guess);

len1 = strlen(guess);
len2 = strlen(ans);

memset(alpha, 0, sizeof(alpha));
count = 0;

printf("Round %d\n", n);
for(i = 0; i<len1; i++){
flag = 0;
if(!alpha[guess[i]-'a']){
for(j = 0; j<len2; j++){
if(ans[j] == guess[i]){
ans[j] = '0';
flag = 1;
}
}
alpha[guess[i]-'a'] = 1;
if(!flag)    count++;
}
if(count == 7){
printf("You lose.\n");
flag = 1;
break;
}

flag = 1;
for(j = 0; j<len2; j++){
if(ans[j] != '0'){
flag = 0;
break;
}
}
if(flag){
printf("You win.\n");
break;
}
}

if(!flag)
printf("You chickened out.\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息