欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【递归】刽子手游戏Uva489

程序员文章站 2024-03-19 08:29:10
...

题目描述:
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 beturned over.’’ For example, if your guess is o'' and the word isbook’’, then both o''s in the solution will be counted assolved.’’
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.
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,错7次意味着失败了。
本题中,你的任务就是编写一个“裁判程序”,输入单词和猜测,判断输赢。
我相信自己的注释已经很详细了

#include<stdio.h>
#include<string.h>
#define maxn 100
int left,chance;//还需要猜left个位置,错chance次之后输 
char s[maxn],s2[maxn];//s为答案字符串,s2为猜测字符串 
int win,lose;//win=1表示赢,lose=1表示输 
void guess(char ch)
{
	int i;
	int bad=1;
	for(i=0;i<strlen(s);i++)//先遍历整个答案字符串 
	if(s[i]==ch)//如果存在输入的猜测字符等于其中某个答案字符 
	{
		left--;//还需猜测的left的值-1 
		s[i]=' ';//将此位置标记成空格,表示所有与此位置(猜对了的)相同字母,避免出现“再猜一次已经猜过的字母算错”的情况 
		bad =0;//如果猜对了那bad=0 
	}
	if(bad) --chance;//如果猜测的字母不是答案中的字母,那么表示猜错一次,机会就-1 
	if(!chance) lose=1;//如果机会减到0了,表示满足题中的“最多只能错6次”的条件,那lose=1表示你输了 
	if(!left) win=1; //同理,先回到第12行的循环中的left,表示猜对了一次,那还需要猜left-1次,如果left减到0,那么你赢了 
}
int main()
{
	int rnd;
	while(scanf("%d%s%s",&rnd,s,s2)==3&&rnd!=-1)//循环输入并且rnd!=-1,表示只要输入不结束就一直输入 
	{
		printf("Round %d\n",rnd);//对应刚开始输入的整数,表示第几局 
		win=0;//与下面的lose同样先将两个值初始化 
		lose=0;
		left=strlen(s);//left等于答案字符串的长度 
		chance =7;//一开始机会等于7,猜错则递减 
		int i; 
		for(i=0;i<strlen(s2);i++)//在猜测字符串中遍历 
		{
		guess(s2[i]);//猜测一个字母 
		if(win||lose) break;//如果赢了或者输了跳出 
		}
	
	if(win)printf("You win.\n");//最后 判断完那种情况然后根据每种情况输出 
	else if(lose)printf("You lose.\n");
	else printf("You chickened out.\n");
	}
	return 0;
}