java 刽子手图像代码_刽子手游戏

刽子手游戏 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;

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值