刽子手游戏 UVa489

题目如下:

n “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.

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。当出错次数到达7次时,判断你猜测失败。如果你要在出错次数到达7次之前猜出来了计算机所想的单词,那就判断你猜测正确。其中,还有一种情况就是,如果在你出错次数到达7次之前,并且计算机所想的单词你也没有猜出来。那么,就判断你放弃了。

本人所有的思路,在代码中的注释都提到了。大致思路就是:首先,先统计计算机所想的单词长度,之后将你所猜的字母在计算机所想的单词中进行计数操作。如果数字为0,代表你猜的字母没有猜中,出错次数+1。反之,如果数字非0(可能会有一个单词中有你所猜的多个字母的情况),则将计算机想的单词长度-你猜的字母个数。剩下的就为没有猜中的字母长度了,当计算机所想的单词长度为0时,则代表你猜测成功。当计算机所想的长度非0,且出错的次数没有到达7次,那么就代表你放弃了。 最后,当计算机所想的长度非0,且出错的次数到达了7次,那么就代表你放弃了。

其中,有一个注意事项:猜一个已经猜过的字母也算错!如何解决这个问题?可以利用Map键值对来建立进行字母和字母猜测次数的对应关系。当某个字母没猜的时候,次数为0。当某个字母猜过的时候,次数为1.当次数为1的字母再次被猜的时候,就代表猜测出错,猜测次数+1。

代码如下:

#include

#include

#include

#include

#include

using namespace std;

int programnumber; //程序编号

string computerword; //代表计算机想的单词

string playerword; //代表玩家猜的单词(字母)

map alphabet; //代表每个字母猜的个数,当个数为1时代表该字母已经猜过!

int chance = 7; //代表猜错的次数,当次数等于7时,代表输了

char letter = 'a';

int main()

{

int i,len,Count;

while (scanf("%d", &programnumber) != EOF)

{

if (programnumber != -1)

{

getchar();

getline(cin, computerword);

getline(cin, playerword);

len = computerword.size(); //代表计算机想的单词的长度

for (i = 0; i < 26; i++)

{

alphabet[letter++] = 0; //将每个字母猜的次数都初始化为0

}

for (i = 0; i < playerword.size(); i++)

{

Count = count(computerword.begin(), computerword.end(), playerword[i]); //代表我猜的单词中其中一个字母在计算机猜的单词中占的个数

if (chance != 0)

{

if (Count && alphabet[playerword[i]] == 0) //如果猜中了且目前猜的字母原先没有猜过

{

if (len)

{

len = len - Count;

alphabet[playerword[i]] = 1; //将猜过的字母个数计为1

}

}

else

{

chance--; //没猜中(该单词已经猜过),次数减1

}

}

else

{

break;

}

}

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

if (len == 0) //当计算机想的单词长度为0时,代表猜测成功

{

printf("You win.\n");

}

else if (len != 0 && chance != 0) //当计算机想的单词长度不为0且出错的次数没到七次

{

printf("You chickened out.\n");

}

else //当计算机想的单词长度不为0且出错的次数到了七次

{

printf("You lose.\n");

}

chance = 7;

letter = 'a';

}

else

{

break;

return 0;

}

}

}

java 刽子手图像代码_刽子手游戏相关推荐

  1. java 刽子手图像代码_闲来的代码:无主之地前传 头像及皮肤包SE代码——刽子手威尔海姆...

    无主之地前传 头像及皮肤包SE代码--刽子手威尔海姆部分 找时间做了一下,虽然可能有前人做过. 使用须知 1.都是SE代码,粘贴就行,不过得需要前传的SE,2代的不行.至于怎么使用SE,去这里 htt ...

  2. 拼图java源码_拼图游戏Java版源代码JAVA游戏源码下载

    Java版的拼图游戏,玩家能够自己更换图片,只需你把它分成块,另外它是以成绩=1000-时间(秒)-移动步数*10来决意你是否输了,按F1键起头游戏,Y健预览图片. 拼图游戏Java版源代码 (1 f ...

  3. java简单通讯录的实现02person类_用java实现简单的小游戏(你一定玩过)

    用java实现简单的小游戏(你一定玩过) 对于java初学者来说,通过一些学习小游戏来对swing学习以及对java基础的学习是一个好的方法,同时也给学习带来了很多的乐趣,接下来就给大家分享一个jav ...

  4. 简易贪吃蛇小游戏java版_用GUI实现java版贪吃蛇小游戏

    本文实例为大家分享了java版贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 项目结构 新建一个JFrame窗口,作为程序入口 public class GameStart{ public stat ...

  5. 推箱子游戏的java设计思路_用JAVA实现一个推箱子游戏

    技术应用 TECHNOLOGY AND MARKET Vol. 26,No. 2,2019 用 JAVA 实现一个推箱子游戏 马寅璞1,孔阳坤2 ( 1. 南京信息工程大学计算机软件学院物联网工程 1 ...

  6. java愤怒的小鸟代码_用java实现小游戏“愤怒的小鸟”附源码和效果演示

    推荐下我自己的群:6915 72518,不管你是小白还是大牛,小编我都挺欢迎,不定期分享干货,包括我自己整理的一份最新资料和零基础入门教程!,欢迎初学和进阶中的小伙伴 今天跟大家分享一个用纯Java代 ...

  7. java 坦克大战_基于JAVA实现的坦克大战游戏

    一.课程题目 实现一个java版本的坦克大战游戏. 功能提示: 游戏要有图形用户界面,界面能够反映游戏所有的细节 界面中要有坦克,墙,树林,河流 界面中要有一个"家"," ...

  8. java实现简单窗体小游戏----球球大作战

    java实现简单窗体小游戏----球球大作战 需求分析 1.分析小球的属性: ​ 坐标.大小.颜色.方向.速度 2.抽象类:Ball ​ 设计类:BallMain-创建窗体 ​ BallJPanel- ...

  9. 今天分享的Java开源游戏项目是桌球游戏,初学者也可以用来练习喔~

    今天分享的Java开源游戏项目是桌球游戏,初学者也可以用来练习喔~ [源码+教程]Java桌球游戏_Java初级项目_Java练手项目_Java项目实战_Java游戏开发实战教学_Java小游戏_体验 ...

  10. java计算机毕业设计技术的游戏交易平台源程序+mysql+系统+lw文档+远程调试

    java计算机毕业设计技术的游戏交易平台源程序+mysql+系统+lw文档+远程调试 java计算机毕业设计技术的游戏交易平台源程序+mysql+系统+lw文档+远程调试 本源码技术栈: 项目架构:B ...

最新文章

  1. d3.json php,使用d3.json从PHP进行JSON输出
  2. torch.nn.LogSoftmax()与输入具有相同尺寸和形状的张量,其值在[-inf,0)范围内
  3. Maven修改远程仓库配置
  4. C# 系统应用之清除Cookies、IE临时文件、历史记录
  5. .NET Core 使用 grpc 实现微服务
  6. arcgis mxt模板 创建工具条无法保存_【从零开始学GIS】ArcGIS中的绘图基本操作(二)...
  7. 今天的成功的滋味 企业即时通讯
  8. 月薪2万程序员面试,被HR直面吐槽:毕业生能值这个数?
  9. Exchange 2010 PowerShell
  10. 《文献管理与信息分析》速看提问
  11. python内置数据类型列表_python数据类型内置方法 字符串和列表
  12. Windows工具 - 查看apk信息 - 包名/支持Android版本/支持架构等
  13. 电脑桌面便签小工具下载,好用的桌面便签软件推荐
  14. Rayman的绝顶之路——Leetcode每日一题打卡21
  15. 计算机网络拓扑星型结构应用,浅谈计算机网络拓扑结构及其应用.doc
  16. 2021-11-25【数据结构/严蔚敏】【Dijkstra】【代码实现算法7.15】
  17. 企业做短视频常见的3问题:太真实了
  18. 牵线搭桥,『桥接模式』
  19. WIN32 2048游戏
  20. Jquery给HTML元素绑定按键事件-回车事件

热门文章

  1. 常见笔顺错误的字_有哪些常见笔顺错误?
  2. EPLAN电气工程图纸EPLAN电气工程图纸,标准化程度高
  3. 新能源汽车厂四大派系
  4. Xcode iphone模拟器运行不流畅
  5. A goal-driven tree-structured neural model for math word problems论文阅读
  6. Event-Driven架构设计消息处理系统
  7. mysql创建临时表
  8. 侯晓迪:全身心的投入,吃住都在实验室
  9. python读取手机通讯录_利用python解析手机通讯录
  10. markdown编辑器Typora的使用方法(保姆级教程)