Advertisement
kahrn

hangman.cpp rev2

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