现在的位置: 首页 > 综合 > 正文

寒假刷题之8——侩子手游戏

2014年09月05日 ⁄ 综合 ⁄ 共 3972字 ⁄ 字号 评论关闭

 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:

  1. The contestant tries to solve to puzzle by guessing one letter at a time.
  2. 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.''
  3. 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     
       | /|\    
       |  |     
       | / \    
     __|_       
     |   |______
     |_________|
  4. If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word, the contestant loses.
  5. If the contestant has guessed all the characters of the word before the drawing is complete, the contestant wins the game.
  6. 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.



   这题真心蛋疼。。。这游戏没玩过谁知道怎么玩啊。。。我认真看了一遍,单词查了好久还是看不太懂,google翻译了一下也还是无从下手,百度题目找到几篇解题报告都没有详细讲怎么玩(没看代码哦)。。。

   最后在一论坛上找到这个游戏的解释:

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). 
   ______   
   |  |     
   |  O     
   | /|\    
   |  |     
   | / \    
__|_       
|   |______
|_________|
你的任务就是要写一个程序根据答案及玩家输入的猜测来判断玩家是赢、输、或放弃。
Input
会有好几组测试资料,每一组有3列。第一列为一个数字n,代表第几回合,第二列为这一回合的答案,第三列为这一回合玩家输入的猜测。如果 n = -1代表输入结束。
Output
请输出每一回合及游戏结果。游戏结果只有三种可能:
You win.
You lose.
You chickened out.


  现在差不多有思路了,开始动手了。。。

  我的思路是搞个letter[26]数组代表26个字母使用情况,初始化为0代表没用过,1代表答案的字母,2代表猜过且猜对了,-1代表猜过且猜错了。。。

  于是编出代码如下:

#include <stdio.h>
#include<ctype.h>

int main()
{
	int n, i;
	char ch;
	
	while(1){
		scanf("%d", &n);
		if (n == -1)
			return 0;
		getchar();
		int count = 0, kill = 0, bingo = 0, letter[26] = {0};
		
		for (; (ch = toupper(getchar())) != '\n';)
			letter[(ch - 'A')] = 1;
		
		for (i = 0; i < 26; i ++)
			if (letter[i] == 1)
				count ++;

		for (; (ch = toupper(getchar())) != '\n';){
			if (letter[ch - 'A'] == 0){
				kill ++;
				letter[ch - 'A'] = -1;
			}
			if (letter[ch - 'A'] == 1){
				bingo ++;
				letter[(ch - 'A')] = 2;
			}
		}

		printf("Round %d\n", n);
		
		if (kill >= 7)
			printf("You lose.\n");
		else if (bingo == count)
			printf("You win.\n");
		else
			printf("You chickened out.\n");
	}
}

   自行测试能过,却ac不了。(到现在已经多少次这样了。。。)

   上网看了前辈们的解题报告,他们说要一步一步判断,前面部分符合win的条件不管后面怎么样都得是win,其实这个问题我也隐约有感觉到。

   修改时还遇到一个小问题,判断到win后马上break会导致后面字符留到下一轮的input,我用了一个for循环把多余的字符给读掉了。

AC代码:

#include <stdio.h>
#include<ctype.h>

int main()
{
	int n, i;
	char ch;
	
	while(1){
		scanf("%d", &n);
		if (n == -1)
			return 0;
		getchar();
		int count = 0, kill = 0, bingo = 0, letter[26] = {0};
		
		for (; (ch = toupper(getchar())) != '\n';)
			letter[(ch - 'A')] = 1;
		
		for (i = 0; i < 26; i ++)
			if (letter[i] == 1)
				count ++;
		
		for (; (ch = toupper(getchar())) != '\n';){
			if (letter[ch - 'A'] == 0){
				kill ++;
				letter[ch - 'A'] = -1;
			}
			if (letter[ch - 'A'] == 1){
				bingo ++;
				letter[(ch - 'A')] = 2;
			}
			if (kill >= 7){
				printf("Round %d\nYou lose.\n", n);
				for (; (ch = getchar()) != '\n';);
			break;
			}
			else if (bingo == count){
				printf("Round %d\nYou win.\n", n);
				for (; (ch = getchar()) != '\n';);
				break;
			}
		}
		
		if (kill < 7 && bingo != count)
			printf("Round %d\nYou chickened out.\n", n);
	}
}

  嘿,自认为这次我的思路比较优良,因为我看其他人都用了很多数组,而我的letter[26]数组显得简单多了。。。


抱歉!评论已关闭.