489 - Hangman Judge

来源:互联网 发布:淘宝名忘了怎么找回 编辑:程序博客网 时间:2024/06/01 15:02

In ``Hangman Judge,'' you are to write aprogram that judges a series of Hangman games. For each game, the answer to thepuzzle is given as well as the guesses. Rules are the same as the classic gameof 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.

4.         ______  

5.         | |    

6.         |  O    

7.         |/|\   

8.         | |    

9.         |/ \   

10.   __|_      

11.   |  |______

 |_________|

  1. If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word, the contestant loses.
  2. If the contestant has guessed all the characters of the word before the drawing is complete, the contestant wins the game.
  3. If the contestant does not guess enough letters to either win or lose, the contestant chickens out.

Your task as the ``Hangman Judge'' is todetermine, for each game, whether the contestant wins, loses, or fails tofinish a game.

Input

Your program will be given a series of inputsregarding the status of a game. All input will be in lower case. The first lineof each section will contain a number to indicate which round of the game isbeing played; the next line will be the solution to the puzzle; the last lineis a sequence of the guesses made by the contestant. A round numberof -1 would indicate the end of all games (and input).

Output

The output of your program is to indicatewhich round of the game the contestant is currently playing as well as theresult 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.

代码:

#include<iostream>

using namespacestd;

string ans;//答案字符串

string guess;//猜测字符串

intwin=0,lose=0;//

void  check();//检测函数

int main()

{

    int time;

    while(cin>>time&&time!=-1)

    {

        cin>>ans>>guess;

        check(guess,ans);

        if(win)

        {

            cout<<"Round"<<time<<"\nYou win."<<endl;

        }

        else if(lose)

        {

            cout<<"Round"<<time<<"\nYou lose."<<endl;

        }

        else

        {

            cout<<"Round"<<time<<"\nYou chickened out."<<endl;//既不赢也不输,那么就主动退出

        }

    }

}

void check()

{

    win=0;//每次执行检测函数都要将标记结果的变量重置为0

    lose=0;

    int left=ans.size();//left是答案序列中还没有被猜到的字符的个数

    int chance=7;//chance==0时,玩家就输了

    for(int i=0; i<guess.size(); i++)

    {

        bool bad=true;//标志guess中的字符能否在ans找到

        for(int j=0; j<ans.size(); j++)

        {

            if(guess[i]==ans[j])

            {

                ans[j]=' ';//只要找到,将ans中的对应字符变为空格,同时解决了如果多次输入同一个字符//的问题(任何字符输入多次除了第一次可能正确外,以后都是错的)

                left--;

                bad=false;

            }

        }

        if(bad)

        {

            chance--;

        }

        if(chance==0)

        {

            lose=1;//先判断chance的大小,因为有可能在left=0之前,chance早已经是负数了

        }

        else if(left==0)

        {

            win=1;

        }

        if((win+lose)!=0)//只要赢或者输有一个结果定了,那么后续的字符没有必要再判断了

        {

            break;

        }

    }

}

0 0
原创粉丝点击