UVA 489 - Hangman Judge

ATTENTION ：我与书上的方法不一样。并且因为我很菜，用的方法会很笨，有什么想指点的请在评论区留言，勿喷👌。

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 “turnedover.” For example, if your guess is ‘o’ and the word is “book”, then both ‘o’s in the solution willbe counted as“solved”.
3.Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, whichneeds 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 thecharacters of the word, the contestant loses.
5.If the contestant has guessed all the characters of the word before the drawing is complete, thecontestant 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 lowercase. The first line of each section will contain a number to indicate which round of the game is beingplayed; 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.

You win. 即在错误7次一下成功猜出了所有第一个字符串中的字符。
You chickened out. 即虽然错误次数没有超过7，但是并没有猜出来所有的第一个字符串中的字符。
You lose. 即全部猜对之前错误次数大于等于7了。

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
string x,y;
while(cin>>n&&n!=-1)//题目要求n为-1时退出程序
{
map<char,int> ma;//用来标记第一个字符串中出现的字符
map<char,int> ma1;//用来判断是否需要对错误次数进行累加
getchar();
cin>>x>>y;
int p=0;
for(int i=0;i<x.size();i++)
{
ma[x[i]]=1;//把第一个字符串中出现的字符全部存进map并标记为1
}
for(int i=0;i<y.size();i++)
{
if(ma[y[i]])//如果ma[y[i]]不为0，即说明这个元素在第一个字符串中
{
ma[y[i]]=0;//对最后的判断第一个字符串有没有被猜完做准备
ma1[y[i]]=1;//因为这是一个正确的字符，所以标记为1，在接下来的判断中将其跳过
}
else if(!ma1[y[i]])//如果ma1[y[i]]为0，说明这个字符是错误且只出现了一次的，此时需要对错误次数加一
{
p++;
ma1[y[i]]=1;//将y[i]标记为1，再遇见y[i]的时候，错误次数不变
}
if(p>=7)
break;//错误的次数已经到达了7次，直接跳出循环
}
int flag=1;
map<char,int>::iterator it;//定义一个迭代器，用来遍历map中的每一个元素
for(it=ma.begin();it!=ma.end();it++)
{
if(it->second)//如果存在一个it->second不为0，即说明没有猜完
{
flag=0;
break;
}
}
printf("Round %d\n",n);//这个很坑，一开始就没注意到，注意看题目的输出格式！！！
if(flag)//flag为1说明在7次错误以内已经猜对了所有字符，所以输出You win.            puts("You win.");
else//否则对错误次数进行判定，这个里面挺好懂的，就不做详细解释了
{
if(p<7)
puts("You chickened out.");
else
puts("You lose.");
}
}
return 0;
}


#include <stdio.h>
#include <string.h>
#define maxn 100
int left,chance;
char s[maxn],s2[maxn];
int win,lose;
void guess(char ch)
{
for(int i=0;i<strlen(s);i++)
if(s[i]==ch)
{
left--;
s[i]=' ';
}
if(!chance)  lose=1;
if(!left)  win=1;
}
int main()
{
int rnd;
while(scanf("%d%s%s",&rnd,s,s2)==3&&rnd!=-1)
{
printf("Round %d\n",rnd);
win=lose=0;
left=strlen(s);
chance=7;
for(int i=0;i<strlen(s2);i++)
{
guess(s2[i]);
if(win||lose)
break;
}
if(win)
printf("You win.\n");
else if(lose)
printf("You lose.\n");
else
printf("You chickened out.\n");
}
return 0;
}