Exemplo n.º 1
0
// ----------------------------------------------------------------------------
// takeGuess(string guessInput)
//
// Creates new guess from provided input string and then determines if guess
// was a letter or word and runs corresponding guest function.
// If guess was correct and solves puzzle returns 1, if guess was correct and
// puzzle is not solved returns 0, if guess was incorrect updates pieces of
// man and returns 0.
// ----------------------------------------------------------------------------
bool Game::takeGuess(string guessInput)
{
	// Create new guess from input
	Guess guess(guessInput);
	// Store guess in players guess list
	player.addGuess(guess);

	// Flag for any correct letters guessed
	int correct = 0;

	// If input guess length was equal to 1
	if (guess.getGuess().length() == 1)
		// Attempt to guess a letter
	if (guessLetter(guess))
		// Flag if guess was correct
		++correct;

	// If input guess length was greater than 1
	if (guess.getGuess().length() > 1)
		// Attempt to guess answer with input
	if (guessWord(guess))
		return 1;

	// If player.partialAnswerString now equals answer return 1
	if (guessWord(player.getPartialAnswerString()))
		return 1;

	// Add a piece to man if guess was incorrect
	if (!correct)
		man.addPiece();

	return 0;

}
Exemplo n.º 2
0
Arquivo: main.cpp Projeto: cedsam/Cpp
int main () // "Guess the word" game in OCR lessons.
{
		std::string wannaPlay;
		int gameAmounts=0;
		int gameWin=0;
		int defeats=0;
		int attempts=0;

		do 
		{
			bool isGuessedResult = false;
			int numberAttempts=0;
			std::string const dictionnaryDirectory("C:/Users/user/dico.txt");
			std::string word(pickOneWord(dictionnaryDirectory));
			std::string mixedWord (mixtureWord(word));
			std::string guessWord (askUser ("Guess the following word in the correct order: " + mixedWord + " \n"));
			gameAmounts++;

			while (isGuessedResult == false)//int guessResult = 0; ==> wasn't clear enough, replace with isGuessedResult
			{
				if ((guessWord!=word) && (numberAttempts!=4))
				{
					attempts++;
					numberAttempts++;
					guessWord=askUser("You are wrong, try again ");
				}
				else if (guessWord==word)
				{
					gameWin++;
					isGuessedResult = true;
					wannaPlay = (askUser("Wanna play again? [yes/no] "));
				}
				else if ((guessWord!=word) && (numberAttempts==4))
				{
					defeats++;
					isGuessedResult  = true;
					wannaPlay = (askUser("The solution was " + word + ". \n" "Wanna play again? [yes/no] "));
					
					while ((wannaPlay != "no") && (wannaPlay != "n") && (wannaPlay != "yes") && (wannaPlay != "y"))
					{
						wannaPlay = (askUser("Wanna play again? [yes/no] "));
					} 
				}
			}

			word.clear();
			mixedWord.clear();
			guessWord.clear();

			isGuessedResult = true;

		} while ((wannaPlay != "no") && (wannaPlay != "n"));
		gameStatistics(gameAmounts,gameWin,defeats,attempts);

		//clearing all variable.
		wannaPlay.clear();
		gameAmounts = 0;
		gameWin = 0;
		defeats = 0;
		attempts = 0;

		//end of program
		exit(EXIT_SUCCESS);
}