Advertisement
tsinclai2002

Week 14 - C++ - Hangman

Apr 11th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  week_14_cplus
  4. //
  5. //  Created by Thomas Sinclair on 4/11/14.
  6. //  Copyright (c) 2014 Cynomys Consulting LLC. All rights reserved.
  7. //
  8.  
  9. // Hangman
  10. // The classic game of hangman
  11.  
  12. #include <iostream>
  13. #include <string>
  14. #include <vector>
  15. #include <algorithm>
  16. #include <ctime>
  17. #include <cctype>
  18.  
  19. using namespace std;
  20.  
  21. int main()
  22. {
  23.     // set-up
  24.     const int MAX_WRONG = 8;  // maximum number of incorrect guesses allowed
  25.    
  26.     vector<string> words;  // collection of possible words to guess
  27.     words.push_back("HELLO");
  28.     words.push_back("WORLD");
  29.     words.push_back("DIFFICULT");
  30.    
  31.     srand(time(0));
  32.     random_shuffle(words.begin(), words.end());
  33.     const string THE_WORD = words[0];            // word to guess
  34.     int wrong = 0;                               // number of incorrect guesses
  35.     string soFar(THE_WORD.size(), '-');          // word guessed so far
  36.     string used = "";                            // letters already guessed
  37.    
  38.     cout << "Welcome to Hangman.  Good luck!\n";
  39.    
  40.     // main loop
  41.     while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
  42.     {
  43.         cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
  44.         cout << "\nYou've used the following letters:\n" << used << endl;
  45.         cout << "\nSo far, the word is:\n" << soFar << endl;
  46.        
  47.         char guess;
  48.         cout << "\n\nEnter your guess: ";
  49.         cin >> guess;
  50.         guess = toupper(guess); //make uppercase since secret word in uppercase
  51.         while (used.find(guess) != string::npos)
  52.         {
  53.             cout << "\nYou've already guessed " << guess << endl;
  54.             cout << "Enter your guess: ";
  55.             cin >> guess;
  56.             guess = toupper(guess);
  57.         }
  58.        
  59.         used += guess;
  60.        
  61.         if (THE_WORD.find(guess) != string::npos)
  62.         {
  63.             cout << "That's right! " << guess << " is in the word.\n";
  64.            
  65.             // update soFar to include newly guessed letter
  66.             for (int i = 0; i < THE_WORD.length(); ++i)
  67.                 if (THE_WORD[i] == guess)
  68.                     soFar[i] = guess;
  69.         }
  70.         else
  71.         {
  72.             cout << "Sorry, " << guess << " isn't in the word.\n";
  73.             ++wrong;
  74.         }
  75.     }
  76.    
  77.     // shut down
  78.     if (wrong == MAX_WRONG)
  79.         cout << "\nYou've been hanged!";
  80.     else
  81.         cout << "\nYou guessed it!";
  82.    
  83.     cout << "\nThe word was " << THE_WORD << endl;
  84.    
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement