1. 程式人生 > >Hangman Judge UVA

Hangman Judge UVA

#include <iostream>
#include <string>

using namespace std;

int win, lose;
int length, chance, rnd;
string s1, s2;

void guess(char);

int main() {
	while ((cin >> rnd >> s1 >> s2).good() && rnd != -1) {
		printf("Round %d\n", rnd);
		win = lose = 0;
		length = s1.length();
		chance = 7;
		for (int i = 0; i < s2.length(); i++) {
			guess(s2[i]);
			if (win || lose)
				break;
		}
		if (win)
			puts("You win.");
		else if (lose)
			puts("You lose.");
		else
			puts("You chickened out.");
	}
	return 0;
}

void guess(char ch) {
	int worse = 1;
	for (int i = 0; i < s1.length(); i++)
		if (s1[i] == ch) {
			length--;
			s1[i] = ' ';
			worse = 0;
		}
	if (worse)
		chance--;
	if (!chance)
		lose = 1;
	if (!length)
		win = 1;
}