Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // HangmanGame.cpp
- // Assignment2Project1
- //
- // Created by Hayden Lueck on 2015-01-29.
- // Copyright (c) 2015 Hayden Lueck. All rights reserved.
- //
- // Create a hangman game that allows the user to input word, and amount of guesses before the game begins
- //
- #include <iostream>
- #include <string>
- using namespace std;
- /** FUNCTION PROTOTYPES **/
- void hangmanGame();
- void getWordAndGuesses(string&, int&);
- int main() {
- hangmanGame(); //call to hangman game function
- return 0;
- }
- void hangmanGame()
- {
- /** VARIABLE DECLARATIONS **/
- string userWord, solution ="", lettersGuessed = ""; //strings for storing full word, and for displaying current guess
- int guesses, counter = 0, length; //amount of guesses for game, counter for loop, length of word
- char guess = '\0'; //current guess made by user playing
- bool correctGuess = false, alreadyGuessed = false; //booleans for if a guess is correct or not, and to check if new guess was given
- getWordAndGuesses(userWord, guesses); //get word to be guessed, as well as amount of guesses
- length = int(userWord.length()); //set length equal to amount of characters the word has
- for(int i = 0; i < length; i++){ //go through loop as many times as the word has characters
- solution += '*'; //create a string of asterisks the length of the word to be guessed
- }
- /** BEGIN MAIN GAME LOOP **/
- while(counter < guesses) // (should never be false since game exits through breaks, but if something goes wrong somehow this is here to prevent infinite looping)
- {
- do{
- cout << "Guess a letter (you have " << guesses - counter << " tries left): "; //display remaining guesses
- cin >> guess; //get next guess from user
- alreadyGuessed = false; //set boolean flag for if the letter was already guessed to false
- for(int i = 0; i < lettersGuessed.length(); i++) //for the amount of unique letters guessed
- {
- if(lettersGuessed[i] == guess) //if a letter already guessed is equal to the current guess
- {
- cout << "'" << guess << "' has already been used. Try again" << endl; //prompt user to guess new letter
- alreadyGuessed = true; //set flag to true to make loop go through again
- }
- }
- }while(alreadyGuessed); //while the user guesses letters already guessed
- /** SCAN WORD FOR CORRECT LETTERS **/
- for(int i = 0; i < length; i++) //go through loop once for every letter in the word
- {
- if(userWord[i] == guess) //if current letter in word is the same as the user's guess
- {
- correctGuess = true; //set boolean flag for a correct guess to true
- solution[i] = guess; //change asterisk at that point in the word to the guessed character
- }
- }
- if(correctGuess) //if the guess was correct
- {
- if(solution == userWord) //if user has guessed all of the contained letters
- {
- cout << "You win!" << endl << "The word was: " << userWord << endl; //tell user they won, show them full word
- break; //break from main loop
- }
- cout << "Right! "; //tell user they are correct
- }
- else if(counter == guesses - 1) //if user isn't correct, and it was their last guess
- {
- cout << "You lose!" << endl << "The word was: " << userWord << endl; //tell user they lost, reveal word to them
- break; //break from main loop
- }
- else //user was incorrect, but they have more guesses
- {
- cout << "Wrong! Try again. "; //prompt to try again
- }
- cout << "Word so far: " << solution << endl; //display word with current guesses
- lettersGuessed += guess; //add letter to string of letters guessed
- correctGuess = false; //set boolean flag for correct guess back to false (default)
- counter++; //increment counter
- }
- /** END MAIN GAME LOOP **/
- }
- void getWordAndGuesses(string& word, int& guesses)
- {
- cout << "Enter the word to be guessed: "; //prompt for word
- cin >> word; //get word from user
- cout << "Enter the max number of tries: "; //prompt for amoun of guesses
- cin >> guesses; //get amount of guesses from user
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement