1

We are all tired of from our daily lives. So I suggest to sit back and
play this game. I present here a hangman game( text version ) for
your enjoyment. There might be some bugs as I haven't throughly
tested it, so sorry if you find it( I'm sure you can fix it ). Happy late
Chinese new year! P.S no cheating, and get your vocabs up...

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <ctime>

using namespace std;

//use if supported, else comment it out
#define SYSTEM_CLS_SUPPORTED 


class GameWords{
private:
	std::vector<string> listOfWords;
public:
	GameWords(){_init();}
	string getNext(){ 	
		if(!listOfWords.empty()){
			string str = listOfWords.back();
			listOfWords.pop_back();
			return str;
		}
		return "";
	}	
	bool empty(){ return listOfWords.empty(); }
private:
        //LOOK AWAY THIS IS PRIVATE!!!
	void _init(){
		listOfWords.push_back("forbidden");     listOfWords.push_back("needle");
		listOfWords.push_back("guessing");      listOfWords.push_back("gossips");
		listOfWords.push_back("justifiable");   listOfWords.push_back("complains");
		listOfWords.push_back("whatsoever");    listOfWords.push_back("specimen");
		listOfWords.push_back("isothermic");    listOfWords.push_back("uncounted");
		listOfWords.push_back("dabbler");       listOfWords.push_back("quaternion");
		listOfWords.push_back("laminated");     listOfWords.push_back("overwise");
		listOfWords.push_back("unwarranted");   listOfWords.push_back("containing ");
		listOfWords.push_back("sleeveless");    listOfWords.push_back("sequence ");
		listOfWords.push_back("asterisks");     listOfWords.push_back("alphabetical");
		listOfWords.push_back("numerical ");    listOfWords.push_back("according");
		listOfWords.push_back("instantitations");listOfWords.push_back("template");
		
		std::random_shuffle(listOfWords.begin(),listOfWords.end());

	}
};
template<typename Type>
void print(const Type& arg,const string& endWith = ""){ 
	cout << arg;
	if(!endWith.empty())
		cout << endWith;	
}
void print(){
	print('\n');
}
template<typename Type>
Type getType(){
	Type var;
	while(!(cin >> var)){
		cin.clear();
		while(cin.get() != '\n') continue;
		print("\nError inputted, try to input again :");
	}
	return var;
}
void clearScreen(const size_t scale = 25){
	for(size_t i = 0; i < scale; ++i)
		print('\n');
}
int main()
{		
	srand(time(0));

	GameWords words; 
	const char EMPTY_CHAR = '*';
	bool isGameOver = false;	
	bool playAgain = true;
	
	while(playAgain && !words.empty())
	{
		string usedLetters;
		string winningWord = words.getNext();
		string accumulatedLetters(winningWord.size(),EMPTY_CHAR);

		while(!isGameOver)
		{
			print("\nMystery Word : " + accumulatedLetters);
			print("\nGuessed letters : " );
			std::copy(usedLetters.begin(),usedLetters.end(),std::ostream_iterator<char>(cout,","));
			print("\nGuess number : ");
			print(usedLetters.size(),"\n");			
			print();

			print("Enter a letter : ");
			char letter = getType<char>();		
			while(!isalpha(letter)){
				print("\nNot Valid character, try again : ");
				letter = getType<char>();
			}
			
			while(usedLetters.find_first_of(letter) != string::npos){
				print("\nLetter already entered, try again : ");
				letter = getType<char>();
			}
			
			usedLetters.push_back(letter);

			while(winningWord.find_first_of(letter) != string::npos){
				size_t pos = winningWord.find_first_of(letter);
				winningWord[pos] = EMPTY_CHAR;
				accumulatedLetters[pos] = letter;
			}
			if(winningWord.find_first_not_of(EMPTY_CHAR) == string::npos) {
				isGameOver = true;
				break;
			}
			
		}

		print("\n\n");
		print("\n\n");
		print("It took you ");
		print(usedLetters.size());
		print(" guesses to guess the correct answer : \"" + accumulatedLetters + "\"\n");

		print("\n\n");
		print("Play Again<y,n> : ");
		char ch = 0;
		while(ch != 'y' && ch != 'n'){
			print("\nInvalid input, try again  : ");
			ch = tolower(getType<char>());		
		}
		if(ch == 'y'){
			isGameOver = false;
			clearScreen();
		#if defined(SYSTEM_CLS_SUPPORTED)
			system("cls"); 
		#endif
		}
		else
			playAgain = false;
		
	}

	print("\n\nThanks 4 playin...and get you vocabs up...\n\n");
	return 0;
}
2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by firstPerson
1

Some of your words have trailing spaces. This results in some un-fun!

Mystery Word : numerical*
Guessed letters : e,i,t,s,l,a,o,d,n,m,u,r,c,y,z,b,f,g,h,j,k,p,q,v,w,x,
Guess number : 26

Enter a letter :

I was having fun until I realized that numerical didn't fill the word. What begins with numerical and ends in one more letter? numericaly?

0

Oops your right, "numerical", "sequence" and "containing " have leading spaces. Hope mods can fix that. Thanks for pointing that out.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.