Uva- 489 - Hangman Judge

4 篇文章 0 订阅
3 篇文章 0 订阅

原文地址:https://blog.qjm253.cn/?p=382

刽子手游戏(Hangman Judge, UVa 489)

原题地址:Uva489

刽子手游戏其实是一款猜单词游戏,如图4-1所示。游戏规则是这样的:计算机想一个单词让你猜,你每次可以猜一个字母。如果单词里有那个字母,所有该字母会显示出来;如果没有那个字母,则计算机会在一幅“刽子手”画上填一笔。这幅画一共需要7笔就能完成,因此你最多只能错6次。注意,猜一个已经猜过的字母也算错。

在本题中,你的任务是编写一个“裁判”程序,输入单词和玩家的猜测,判断玩家赢了(You win.)、输了(You lose.)还是放弃了(You chickened out.)。每组数据包含3行,第1行是游戏编号(-1为输入结束标记),第2行是计算机想的单词,第3行是玩家的猜测。后两行保证只含小写字母。

样例输入:

1
cheese
chese
2
cheese
abcdefg
3
cheese
abcdefgij
-1

样例输出:

Round 1
You win.
Round 2
You chickened out.
Round 3
You lose.

代码实现

/**
 * UVa489
 */
#include <iostream>
#include <string>
#include <set>
#include <algorithm>

using namespace std;

const int ERROR_TORRENT = 7;
/**
 *
 * @param s
 * @param guess
 * @return  1 -> success  2 -> fail  0 -> give up
 */
int doGuess(string &s, string &guess){
    int errorTime = 0;
    set<char> errorSet;
    bool isMatch;
    bool isFinish;
    for(auto g : guess){
        isMatch = false;
        isFinish = true;
        if(errorSet.find(g) != errorSet.end()) //猜错一次后就不再猜了
            continue;
        errorSet.insert(g);
        for(auto &c : s){
            if(c == ' ')
                continue;
            isFinish = false;
            if(g == c) {
                c = ' ';
                isMatch = true;
            }
        }
        if(isFinish)
            return 1;
        if(!isMatch) {
            ++errorTime;
        }
        if(errorTime >= ERROR_TORRENT)
            return 2;
    }
    for(auto &g : s){
        if(g != ' ')
            return 0;
    }
    return 1;
}

int main(){
    int round;
    string target;
    string guess;
    while(cin >> round){
        if(round == -1)
            break;
        cin >> target >> guess;
        cout << "Round " << round << endl;
        switch (doGuess(target, guess)){
            case 0:
                cout << "You chickened out." << endl;
                break;
            case 1:
                cout << "You win." << endl;
                break;
            case 2:
                cout << "You lose." << endl;
                break;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值