UVA489:

Hangman Judge

#include <iostream>
#include <string>
#include <algorithm>


using namespace std;

//returns true if word is solved
bool solved(string s){
	bool b = true;
	for(int i = 0; i < s.length(); i++){
		if(s[i] != '!'){
			b = false;
			break;
		}
	}

	return b;
}


string removeDuplicate(string s){
	int length = s.length();

	for(int i = 0; i < length; i++){
		for(int j = i+1; j < length;){
			if(s[i] == s[j]){
				s[j] = s[--length];
			} else {
				j++;
			}

		}
	}

	s = s.substr(0, length);
	
	return s;


}

int main(){
	
	int n;
	while(cin >> n){
		
		if(n == -1){
			break;
		}

		string word, guess;
		cin >> word >> guess;


		// cout << "before removing " << guess << endl;
		guess = removeDuplicate(guess);

		// cout << "after removing " << guess << endl;


		int len_word = word.length();
		int len_guess = guess.length();
		// cout << len_guess << endl;

		for(int i = 0; i < len_guess; i++){
			for(int j = 0; j < len_word; j++){
				if(guess[i] == word[j]){
					word[j] = '!';
				}
			}
		}

		cout << "Round " << n << endl;
		if(len_guess < 7 && solved(word)){
			cout << "You win." << endl;
		} else if (len_guess >= 7 && !solved(word)){
			cout << "You lose." << endl;
		} else {
			cout << "You chickened out." << endl;
		}

		// cout << "Guess " << guess << endl;
		// cout << "Word " << word << endl;

	}
	
	return 0;

}
[Back to Listing Page]

Home

Posts

Projects

Github

Contact

janzzen

Developed by Janzzen Ang powered by Vercel