Bukz

n00b hangman

Apr 12th, 2011
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <cctype>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // Function prototypes
  8. bool guess(string word, string &curWord, char c);
  9. bool hasChar(string word, char c);
  10. bool wordComplete(string curWord);
  11. string updateWord(string word, string &curWord, char c);
  12. string hideWord(string word);
  13.  
  14. int main()
  15. {
  16.     char a, curGuess;
  17.     int playCtr = 0;
  18.     const int maxSkip = 1000;
  19.  
  20.     do
  21.     {
  22.         int tries = 6;
  23.         const int maxHistory = tries + 1; // cout << maxHistory // Output: 7
  24.         char guessHistory[maxHistory]; // expected constant expression, cannot allocate array of constant size 0, 'guessHistory' : unknown size
  25.         bool gameFinished = false;
  26.  
  27.         string word = "AssaultCube";
  28.         string curWord = hideWord(word);
  29.  
  30.         cout << "\nWelcome to Hangman! Good luck!\n\n";
  31.         cout << "The word is " << word.length() << " characters long.\n";
  32.  
  33.         // Game loop
  34.         while(!gameFinished)
  35.         {
  36.             // Ensure that alphabetical input is read
  37.             curGuess = ' ';
  38.  
  39.             while (!isalpha(curGuess))
  40.             {
  41.                 cout << "\nEnter your guess (an alphabetical character): ";
  42.                 cin.get(curGuess);
  43.                 curGuess = tolower(curGuess);
  44.                 cin.ignore(maxSkip, '\n');
  45.             }
  46.            
  47.             // If the guess was incorrect, decrement the tries var and notify the player
  48.             if (!guess(word, curWord, curGuess))
  49.             {
  50.                 tries--;
  51.                 cout << "You have " << tries << " guesses left.\n";
  52.             }
  53.  
  54.             // Break out of the loop if the player wins or loses
  55.             if (wordComplete(curWord))
  56.             {
  57.                 cout << "\nGAME OVER - Congratulations, you won!\n";
  58.                 gameFinished = true; // break
  59.             }
  60.             else
  61.             {
  62.                 if (tries < 1)
  63.                 {
  64.                     cout << "\nGAME OVER - Sorry, you lost!\n\n";
  65.                     cout << "The word was " << word << "\n";
  66.                     gameFinished = true; // break
  67.                 }
  68.             }
  69.         }
  70.  
  71.         playCtr++;
  72.  
  73.         // Ensure that y or n input is read
  74.         a = '\0';
  75.  
  76.         while(tolower(a) != 'n' && tolower(a) != 'y')
  77.         {
  78.             cout << "\a\nPlay again? y/n? ";
  79.             cin.get(a);
  80.             cin.ignore(maxSkip, '\n');
  81.         }
  82.  
  83.     } while(tolower(a) == 'y');
  84.  
  85.     if (playCtr < 2)
  86.     {
  87.         cout << "\nGood game, bye!\n";
  88.     }
  89.     else
  90.     {
  91.         cout << "\nGood games, bye!\n";
  92.     }
  93.     return 0;
  94. }
  95.  
  96. // hideWord() - Initially masks the chosen word with dashes "-"
  97. string hideWord(string word)
  98. {
  99.     string output = "";
  100.     for (unsigned int i = 0; i < word.length(); i++)
  101.     {
  102.         output += "-";
  103.     }
  104.     return output;
  105. }
  106.  
  107. // updateWord() - updates the temporarily masked word upon correct guesses
  108. string updateWord(string word, string &curWord, char c)
  109. {
  110.     string output = "";
  111.     for (unsigned int i = 0; i < word.length(); i++)
  112.     {
  113.         char letter = word.at(i);
  114.         if (tolower(letter) == c)
  115.         {
  116.             output += c;
  117.         }
  118.         else
  119.         {
  120.             letter = curWord.at(i);
  121.             if (tolower(letter) != '-')
  122.             {
  123.                 output += letter;
  124.             }
  125.             else
  126.             {
  127.                 output += "-";
  128.             }
  129.         }
  130.     }
  131.     curWord = output;
  132.     return output;
  133. }
  134.  
  135. // hasChar() - Determines if the user's guess was found in the word
  136. bool hasChar(string word, char c)
  137. {
  138.     for (unsigned int i = 0; i < word.length(); i++)
  139.     {
  140.         char letter = word.at(i);
  141.         if (tolower(letter) == c)
  142.         {
  143.             return true;
  144.         }
  145.     }
  146.     return false;
  147. }
  148.  
  149. // guess() - Handles the guesses users enter
  150. bool guess(string word, string &curWord, char c)
  151. {
  152.     if (hasChar(word, c))
  153.     {
  154.         cout << "\nCorrect! The updated word is ";
  155.         cout << updateWord(word, curWord, c) << "\n";
  156.         return true;
  157.     }
  158.     else
  159.     {
  160.         cout << "\nIncorrect! The word is " << curWord << "\n\n";
  161.         return false;
  162.     }
  163. }
  164.  
  165. // wordComplete() - Determines if the temporarily masked word has been completely unveiled
  166. bool wordComplete(string curWord)
  167. {
  168.     for (unsigned int i = 0; i < curWord.length(); i++)
  169.     {
  170.         char letter = curWord.at(i);
  171.         if (letter == '-')
  172.         {
  173.             return false;
  174.         }
  175.     }
  176.     return true;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment