pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

kahrn private pastebin - collaborative debugging tool What's a private pastebin?


Posted by kahrn on Sat 12 Jul 02:47
report abuse | download | new post

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

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post