刽子手c语言,竞赛题目选讲——刽子手的游戏(C语言)

刽子手的游戏

原文:(最好先自己敲敲试试,不会的再看看思路,进步得比较快!)

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.

分析:刽子手游戏实质上是一个猜单词的游戏,猜一个单词,每次只能猜一个字母,如果那个字母猜对了,那么在单词中与之所有相同的字母都会显示出来,如果猜错了,那么久少了一个猜单词的机会。

在本题中,小菜鸟们的任务是编写一个“裁判程序”,输入单词和玩家的猜测,并输出结果,分别是玩家赢了(You win.)、玩家输了(You lose.)、

或者玩家中途放弃了(刚开始小编没理解中途放弃是什么操作,其实就是还有猜的机会,但是玩家已经不继续往下输入单词了,就为中途放弃(不知道无小编这样解释得够清楚吗?))

输入输出数据格式:

输入:每组数据包括三行:第一行是游戏编号(当玩家输入-1时程序结束,要记得这个也是作为判断的条件之一哟!),

第二行为计算机想的单词,第三行为玩家猜的单词(后两行保证只有小写字母)

在这里顺便延伸一下自顶向下和自底向上的概念(听着晕乎有米有!)

自顶向下:先写主程序包括所调用的函数,再写调用的函数的细节代码

自底向上;先设计所要调用的函数,再写主程序(这个时候写程序时就要注意被调用的函数和主程序之间的顺序问题了!

如果调用函数的定义在主程序之后,程序是会报错的!千万要注意哟)

#include

#include

#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 bad=1;

for(int i=0;i

if(s[i]==ch){

left--;

s[i]=' ';

bad=0;

}

}

if(bad) --chance;

if(!chance) lose=1;

if(!left) win=1;

}

int main(){

int rnd;

while(scanf("%d%s%s",&rnd,s,s2)==3&&rnd!=-1){

printf("Round %d\n",rnd);

win=lose=0; //不要忘记初始化哟

left=strlen(s);

chance=7;

for(int i=0;i

guess(s2[i]);

// printf("%s %d %d\n",s,left,chance); //可以用来调试程序看是否有误

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;

}

看到这里,你以为结束了? No~在这里,无小编想给小菜们讲几个应该注意的问题

1.变量名的选取要注意,不要和关键字或者函数名或者数组名相同,有的时候程序不会报错,自己也很难找到这么小的错误

2.指针应该少用(当然如果大神们觉得它好用,当然不能拦),这里仅仅是无小编个人看法,指针的使用有时候会让整段代码看起来很复杂。

3.大家注意到了吗?代码段中guess()函数把猜对的字符变成了空格!为什么呢,题目中有说明,只要是重复猜测某个字母也算错,把猜对的字母变成空格,下次再猜那个字母的时候,答案字符串里面的字符已经变成了空格,当然就是错的啦!!

如有雷同,不会有雷同,无小编亲笔

附上UVa的OJ网址:https://uva.onlinejudge.org/

如有错误,希望大家给无小编提出你们宝贵的建议哟~mua~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值