Advertisement
Ashanmaril

Hangman

Jan 30th, 2015
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. //
  2. //  HangmanGame.cpp
  3. //  Assignment2Project1
  4. //
  5. //  Created by Hayden Lueck on 2015-01-29.
  6. //  Copyright (c) 2015 Hayden Lueck. All rights reserved.
  7. //
  8. //  Create a hangman game that allows the user to input word, and amount of guesses before the game begins
  9. //
  10.  
  11. #include <iostream>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16. /** FUNCTION PROTOTYPES **/
  17. void hangmanGame();
  18. void getWordAndGuesses(string&, int&);
  19.  
  20. int main() {
  21.     hangmanGame(); //call to hangman game function
  22.     return 0;
  23. }
  24.  
  25. void hangmanGame()
  26. {
  27.     /** VARIABLE DECLARATIONS **/
  28.     string userWord, solution ="", lettersGuessed = ""; //strings for storing full word, and for displaying current guess
  29.     int guesses, counter = 0, length; //amount of guesses for game, counter for loop, length of word
  30.     char guess = '\0'; //current guess made by user playing
  31.     bool correctGuess = false, alreadyGuessed = false; //booleans for if a guess is correct or not, and to check if new guess was given
  32.    
  33.     getWordAndGuesses(userWord, guesses); //get word to be guessed, as well as amount of guesses
  34.     length = int(userWord.length()); //set length equal to amount of characters the word has
  35.    
  36.     for(int i = 0; i < length; i++){ //go through loop as many times as the word has characters
  37.         solution += '*'; //create a string of asterisks the length of the word to be guessed
  38.     }
  39.    
  40.    
  41.     /** BEGIN MAIN GAME LOOP **/
  42.     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)
  43.     {
  44.         do{
  45.             cout << "Guess a letter (you have " << guesses - counter << " tries left): "; //display remaining guesses
  46.             cin >> guess; //get next guess from user
  47.             alreadyGuessed = false; //set boolean flag for if the letter was already guessed to false
  48.             for(int i = 0; i < lettersGuessed.length(); i++) //for the amount of unique letters guessed
  49.             {
  50.                 if(lettersGuessed[i] == guess) //if a letter already guessed is equal to the current guess
  51.                 {
  52.                     cout << "'" << guess << "' has already been used. Try again" << endl; //prompt user to guess new letter
  53.                     alreadyGuessed = true; //set flag to true to make loop go through again
  54.                 }
  55.             }
  56.         }while(alreadyGuessed); //while the user guesses letters already guessed
  57.        
  58.         /** SCAN WORD FOR CORRECT LETTERS **/
  59.         for(int i = 0; i < length; i++) //go through loop once for every letter in the word
  60.         {
  61.             if(userWord[i] == guess) //if current letter in word is the same as the user's guess
  62.             {
  63.                 correctGuess = true; //set boolean flag for a correct guess to true
  64.                 solution[i] = guess; //change asterisk at that point in the word to the guessed character
  65.             }
  66.         }
  67.        
  68.         if(correctGuess) //if the guess was correct
  69.         {
  70.             if(solution == userWord) //if user has guessed all of the contained letters
  71.             {
  72.                 cout << "You win!" << endl << "The word was: " << userWord << endl; //tell user they won, show them full word
  73.                 break; //break from main loop
  74.             }
  75.             cout << "Right! "; //tell user they are correct
  76.         }
  77.         else if(counter == guesses - 1) //if user isn't correct, and it was their last guess
  78.         {
  79.             cout << "You lose!" << endl << "The word was: " << userWord << endl; //tell user they lost, reveal word to them
  80.             break; //break from main loop
  81.         }
  82.         else //user was incorrect, but they have more guesses
  83.         {
  84.             cout << "Wrong! Try again. "; //prompt to try again
  85.         }
  86.        
  87.         cout << "Word so far: " << solution << endl; //display word with current guesses
  88.        
  89.         lettersGuessed += guess; //add letter to string of letters guessed
  90.         correctGuess = false; //set boolean flag for correct guess back to false (default)
  91.         counter++; //increment counter
  92.     }
  93.     /** END MAIN GAME LOOP **/
  94. }
  95.  
  96. void getWordAndGuesses(string& word, int& guesses)
  97. {
  98.     cout << "Enter the word to be guessed: "; //prompt for word
  99.     cin >> word; //get word from user
  100.     cout << "Enter the max number of tries: "; //prompt for amoun of guesses
  101.     cin >> guesses; //get amount of guesses from user
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement