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:

  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.
 
说实话,这个题不算难,但是关键是细心,这个题有几个关键点
1、在屠夫把标准答案提出来后,你如何处理他重复的字母
2、你如何处理你重复的字符
3、怎么进行结束判断
注意1:如果你定义两个循环,意思是拿出一个字符判断与标准答案作比较,而循环的比较过程是从大循环里拿出一个字母和整个的串比较,所以并不能遇见不匹配的的错误的计数变量就加1,而是应该遇见有相同的就break(在已经妥善处理完1、2的条件下),遇不见相同的,在循环外加1.
注意2:注意break的用法,break用不好,很容易导致,循环没办法进行例如,你发现满足某个条件,直接用break,而导致接近循环结束的计数变量没有加上,进而导致继续判断这个字符(而这个字符很有可能已经改变,而导致出现错误的结果)
注意3:注意goto语句,你想goto哪里,是结束计数变量的++还是判断条件,这个逻辑上不应该有错误。
下面贴一个时间用的较少的代码
#include<stdio.h>
#include<string.h>
main()
{
    char a[100],b[100],c;
    int i,j,k,m,n,t,q;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==-1)
            break;
        m=0;
        k=-1;
        i=0;
        memset(a,'0',100);/*先清‘0’,因为我把有相同的字母都换成‘0’了*/
        scanf("%s",a);
        scanf("%s",b);/*注意尽量不要用gets,因为容易出错*/
        while(b[i]!='\0')/*拿自己的答案和标准答案作比较*/
        {
            for(t=0; t<i; t++)/*这是个关键步骤,它就是处理条件2的,如何后面的字符和前面的有相同的了,就不再进行下面的操作了*/
            {
                if(b[i]==b[t])
                {
                    goto loop;/*这就是break用法的慎用,你如果用break,你会发现它只是跳出你的for循环,一点用都没有,为什么要到那个判断条件,其实*/
                }             /*是我懒得再写一个标示符了,其实为了省时间应该写到i++那里*/
            }
            q=1;/*标记变量*/
            j=0;
            while(a[j]!='\0')
            {
                if(a[j]==b[i])
                {
                    c=a[j];/*用于记录下是哪个字符被相等了,以后有用*/
                    a[j]='0';
                    q=0;
                    break;/*如果找到相等的q这个标记变量=0然后break跳出while循环*/
                }
                j++;
            }
            if(q==0)/*把判断条件那外面来,这样做,找不到也只会加上1次*/
            {
                for(t=0; t<strlen(a); t++)/*这里是处理2的如果后面的标准答案的字符与被相等的字符相同,则一样被赋予'0'*/
                {
                    if(a[t]==c)
                        a[t]='0';
                }
                goto loop;/*多余了...*/

            }
            else
            {
                m++;
            }
            loop:for(t=0;t<strlen(a);t++)/*判断*/
            {
                if(a[t]=='0')
                    {k=0;}
                else
                    {k=-1;break;}

            }
            if(k==0)
                break;
            if(m>=7)
            {
                k=1;
                break;
            }
           i++;
        }
        if(k==1)
            printf("Round %d\nYou lose.\n",n);
        else if(!k)
            printf("Round %d\nYou win.\n",n);
        else if(k==-1)
            printf("Round %d\nYou chickened out.\n",n);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值