Hangman Judge UVA - 489

本文介绍了一个Hangman游戏胜负判断程序的设计与实现。通过输入谜底和玩家猜测的字符序列,程序能够准确判断游戏结果:胜利、失败或放弃,并输出相应的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hangman Judge UVA - 489

题目描述
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.

大致题意
给你两串字符,第一串是看不见的,第二串是你每次所猜测的第一串的字符组成的,如果你猜中了第一串中的某个字符,则第一串中被猜中的那个字符会全部显示出来,如果你所猜的字符第一串中没有,则错误次数加一,当错误次数达到7时你失败,结果输出You lose.,当第一串字符全部显示出来时你赢,结果输出You win.,如果你的错误次数没达到7而且猜完后第一串字符没有全部显示出来则输出You chickened out.

思路
简单的模拟

代码如下

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<sstream>
#include<ctime>
using namespace std;

int main()
{
    int n,l,sum;
    char s1[1000],s2[1000];
    int f1[30],f2[30];
    while(1)
    {
        cin>>n;
        getchar();
        if(n==-1)
        return 0;
        memset(f1,0,sizeof(f1));
        memset(f2,0,sizeof(f2));
        gets(s1);
        gets(s2);
        l=strlen(s1);
        for(int i=0;i<l;i++)   //f1记录字符串1中有哪些字母出现
        {
            f1[s1[i]-'a'+1]=1;
        }   
        sum=0;
        for(int i=1;i<=26;i++)//sum记录字符串1中出现的不同字母总和
        sum+=f1[i];
        l=strlen(s2);
        int flag1=0,flag2=0,cnt=0;
        for(int i=0;i<l;i++)
        {
            if(f1[s2[i]-'a'+1]==1)// 如果猜测的字母在字符串1中有出现
            {
                if(f2[s2[i]-'a'+1]==0)// 如果该字母没被猜过
                {
                   f2[s2[i]-'a'+1]=1; // 将其标记为1表示猜过
                    sum--;          
                    if(!sum)  //如果sum为0表示全被猜完,结束
                    {
                        flag1=1;
                        break;
                    }
                }
            }
            else   //如果猜测的字母没在字符串1中出现,说明猜错
            {
                cnt++;
                if(cnt==7) //如果错误次数到达7,结束
                {
                    flag2=1;
                    break;
                }
            }
        }
        if(flag1==1) //输出
        {
            printf("Round %d\n",n);
            printf("You win.\n");
        }
        else if(flag2==1)
        {
            printf("Round %d\n",n);
            printf("You lose.\n");
        }
        else 
        {
            printf("Round %d\n",n);
            printf("You chickened out.\n");
        }
    }  
    return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值