Advertisement
kahrn

hangman.cpp rev2

Mar 21st, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. /*
  2. *  Classic game of hangman / 25th May 2008
  3. *  Programming by krazekaveman <brandonkite92@gmail.com> and Ricky Hewitt [kahrn] <kahrny@gmail.com>
  4. *  Free to use/edit/republish without restriction
  5. *
  6. *  Make sure you have generated a wordlist.
  7. *  A wordlist is a list of words, seperated by a newline (e.g. word1\nword2\nword3\n).
  8. *
  9. *  22.03.2012:
  10. *    - Updated to prevent SIGV exit when wordlist was not found, display error instead. <ricky.hewitt@gmail.com>
  11. *
  12. *  25.05.2008:
  13. *    - Initial release <brandonkite92@gmail.com> <ricky.hewitt@gmail.com>
  14. */
  15.  
  16. #include <iostream>
  17. #include <fstream>
  18. #include <string>
  19. #include <vector>
  20. #include <algorithm>
  21. #include <ctime>
  22. #include <cctype>
  23. #include <cstdlib> // for exit()
  24.  
  25. using namespace std;
  26.  
  27. // The location of the wordlist
  28. const char *WORDLIST_LOCATION = "wordlist.txt";
  29.  
  30. // Function declarations
  31. void call_hangman();
  32. vector<string> generate_wordlist();
  33.  
  34.  
  35. int main()
  36. {
  37.     cout << "Welcome to Hangman! Good luck\n";
  38.  
  39.     call_hangman(); // This calls hangman, initialises the game and stuff.
  40.  
  41.     // While loop is to determine whether or not to play again.
  42.     while (1) {
  43.         // Prompt for playing again, act upon..
  44.         char cPlayAgain = '!';
  45.         cout << "\nPlay again? (Y/N): ";
  46.         cin.getline(&cPlayAgain, 2);
  47.  
  48.         // Play the game again.
  49.         if (cPlayAgain == 'y' or cPlayAgain == 'Y') {
  50.             call_hangman();
  51.             cout << endl; // Create new line (for fancy output)
  52.         }
  53.  
  54.         // Exit
  55.         if (cPlayAgain == 'n' or cPlayAgain == 'N') {
  56.             break;
  57.         }
  58.     }
  59.  
  60.     return 0;
  61. }
  62.  
  63.  
  64. vector<string> generate_wordlist()
  65. {
  66.     // Initialise vector
  67.     vector<string> words;
  68.  
  69.     // Read from file and retrieve word list
  70.     ifstream wordfile;
  71.     string line;
  72.  
  73.     wordfile.open(WORDLIST_LOCATION);
  74.     if (wordfile.is_open()) {
  75.         while (!wordfile.eof()) {
  76.             getline(wordfile, line);
  77.             // Discard empty lines and comment lines
  78.             if (line != "" and line[0] != '#') {
  79.                 // Convert string to uppercase
  80.                 for (int i = 0; i < line.length(); i++) {
  81.                     line[i] = toupper(line[i]);
  82.                 }
  83.  
  84.                 // Add converted string to string vector.
  85.                 words.push_back(line);
  86.             }
  87.         }
  88.         wordfile.close();
  89.     }
  90.     else {
  91.         cout << "Could not open " << WORDLIST_LOCATION << endl;
  92.         exit(0);
  93.     }
  94.  
  95.     return words;
  96. }
  97.  
  98.  
  99. void call_hangman()
  100. {
  101.     // Setup
  102.     const int MAX_WRONG = 8;
  103.     int wrong=0;
  104.  
  105.     // Generate random word
  106.     vector<string> words = generate_wordlist();
  107.  
  108.     srand(time(0));
  109.     random_shuffle(words.begin(), words.end());
  110.    
  111.     string THE_WORD = words[0]; // The generated random word
  112.     string soFar(THE_WORD.size(),'-'); // The guess so far by the user
  113.     string used=""; // Characters that have been guessed.
  114.    
  115.     // Check if the player gets to keep guessing
  116.     while ((wrong<MAX_WRONG) && (soFar!=THE_WORD))
  117.     {
  118.           cout << "\n\nYou Have " << (MAX_WRONG-wrong) << " incorrect guesses left.\n";
  119.           cout << "\nYou've used the following letters:\n" << used << endl;
  120.           cout << "\nSo far, the word is:\n" << soFar << endl;
  121.  
  122.           // Get the players guess
  123.           char guess;
  124.           cout << "\n\nEnter your guess: ";
  125.           cin >> guess; // bug here
  126.  
  127.           guess = toupper(guess); // make uppercase
  128.           while (used.find(guess) != string::npos)
  129.           {
  130.                 cout << "\n You've already guessed: "<<guess<<".\n";
  131.                 cout << "\n\nEnter your guess: ";
  132.                 cin >> guess;
  133.                 guess = toupper(guess); // make uppercase
  134.           }
  135.  
  136.           used += guess;
  137.  
  138.           // If the user guesses a correct character.
  139.           if (THE_WORD.find(guess) != string::npos)
  140.           {
  141.              cout << "That's right! " << guess << " is in the word\n";
  142.  
  143.              //update soFar to include the newly guessed number
  144.              for (unsigned int i=0; i<THE_WORD.length(); ++i)
  145.              {
  146.                  if (THE_WORD[i]==guess)
  147.                  {
  148.                   soFar[i]=guess;
  149.                  }
  150.              }
  151.           }
  152.           else
  153.           {
  154.               cout << "Sorry " << guess << " isn't in the word";
  155.               ++wrong;
  156.           }
  157.     }
  158.  
  159.     // End of the game
  160.     if (wrong == MAX_WRONG) {
  161.         cout << "\nYou have been HANGED!\n";
  162.         cout << "\b" << endl;
  163.     }
  164.  
  165.     else {
  166.         cout << "\nYou guessed it!\n";
  167.     }
  168.  
  169.     cout << "The word was " << THE_WORD << endl;
  170.  
  171.     getchar();
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement