Guest User

Hangman

a guest
Mar 18th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.02 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <vector>
  7. #include <cctype>
  8.  
  9.  
  10. //I did not make this, and have no idea what it does
  11. template<typename T, size_t N>
  12. T * end(T (&ra)[N]) {
  13.     return ra + N;
  14. }
  15.  
  16. //word list
  17. const char * wordArray[] = {"sheep", "balloon", "sled", "jetpack", "airplane", "computer", "television", "word", "hung", "snow", "cookie", "rythym", "moon", "pencil", "zebra", "dictionary", "interesting", "programming", "apple", "microwave", "lightbulb", "science"};
  18.  
  19. const std::vector<std::string> wordList(wordArray, end(wordArray));
  20.  
  21. //depreciated
  22. /*int getRandomIndex()
  23. {
  24.     std::srand(time(0));
  25.  
  26.     return rand() % words.size();
  27. }*/
  28.  
  29. std::string getBlankWord(std::vector<std::string> words, int currentWordIndex)
  30. {
  31.     std::string blankString;
  32.  
  33.     for (int i = 0; i < words[currentWordIndex].size(); ++i)
  34.         blankString.append("_");
  35.  
  36.     return blankString;
  37. }
  38.  
  39. //gets correct input
  40. std::string getGuess(std::string guessingWord)
  41. {
  42.     std::string input;
  43.  
  44.     while (true)
  45.     {
  46.         std::cout << "Enter your guess\n";
  47.  
  48.         std::getline(std::cin, input);
  49.  
  50.         if (input.length() != 1 && input.length() != guessingWord.size())
  51.         {
  52.             std::cout << "\nInvalid answer, try again.\n\n\n";
  53.  
  54.             continue;
  55.         }
  56.  
  57.        
  58.         if (input.length() == 1)
  59.         {
  60.             if (!isalpha(tolower(input[0])))
  61.             {
  62.                 std::cout << "\nInvalid answer, try again.\n\n\n";
  63.  
  64.                 continue;
  65.             }
  66.         }
  67.         else
  68.         {
  69.             bool quit = false;
  70.  
  71.             for (int i = 0; i < input.length(); ++i)
  72.                 if (!isalpha(input[i]))
  73.                 {
  74.                     quit = true;
  75.  
  76.                     break;
  77.                 }
  78.  
  79.             if (quit)
  80.             {
  81.                 std::cout << "\nInvalid answer, try again.\n\n\n";
  82.  
  83.                 continue;
  84.             }
  85.         }
  86.  
  87.         for (int i = 0; i < input.length(); ++i)
  88.             input[i] = tolower(input[i]);
  89.  
  90.         return input;
  91.     }
  92. }
  93.  
  94.  
  95. //gets y/n answer
  96. bool getYesNo()
  97. {
  98.     std::string input;
  99.  
  100.     while (true)
  101.     {
  102.         std::cout << "Enter 'y' or 'n'\n";
  103.  
  104.         std::getline(std::cin, input);
  105.  
  106.         std::cout << "\n\n";
  107.  
  108.         if (input.length() != 1)
  109.         {
  110.             std::cout << "Invalid input, try again\n\n";
  111.            
  112.             continue;
  113.         }
  114.         else if (input.length() == 1)
  115.             if (tolower(input[0]) == 'y')
  116.                 return true;
  117.             else if (tolower(input[0]) == 'n')
  118.                 return false;
  119.             else
  120.             {
  121.                 std::cout << "Invalid input, try again\n\n";
  122.                
  123.                 continue;
  124.             }
  125.     }
  126. }
  127.  
  128. //checks if guess is correct
  129. bool checkGuess(std::string input, std::vector<std::string> words, int currentWordIndex)
  130. {
  131.     if (input.length() == 1)
  132.         for (int i = 0; i < words[currentWordIndex].length(); ++i)
  133.             if (words[currentWordIndex][i] == input[0])
  134.                 return true;
  135.  
  136.     if (input.length() > 1)
  137.     {
  138.         if (input == words[currentWordIndex])
  139.             return true;
  140.         else
  141.             return false;
  142.     }
  143.        
  144.     return false;
  145. }
  146.  
  147. //fills in the new string with letter guess
  148. void fillInLetters(std::string input, std::vector<std::string> words, int currentWordIndex, std::string  & currentGuessingWord)
  149. {
  150.     for (int i = 0; i < words[currentWordIndex].length(); ++i)
  151.         if (words[currentWordIndex][i] == input[0])
  152.             currentGuessingWord[i] = input[0];
  153. }
  154.  
  155. //checks if the guessing string is equal to the word list string
  156. bool checkForWin(std::string currentGuessingString, std::vector<std::string> words, int currentWordIndex)
  157. {
  158.     return currentGuessingString == words[currentWordIndex];
  159. }
  160.  
  161.  
  162. void printHealthAndWord(int health, std::string currentGuessingWord)
  163. {
  164.     std::cout << "You have " << health << " attempts left before you are hung. ";
  165.  
  166.     std::cout << "Here is the word:\n ";
  167.  
  168.     for (int i = 0; i < currentGuessingWord.length();  ++i)
  169.         std::cout << currentGuessingWord[i] << " ";
  170.  
  171.     std::cout << "\n\n";
  172. }
  173.  
  174. //returns if input is new, based on past attempts list
  175. bool newInput(std::string input, std::vector<std::string> inputs)
  176. {
  177.     for (int i = 0; i < inputs.size(); ++i)
  178.         if (input == inputs[i])
  179.             return false;
  180.  
  181.     return true;
  182. }
  183.  
  184. //generates a random sequence of words
  185. std::vector<std::string> getRandomizedWordList()
  186. {
  187.     std::vector<std::string> words;
  188.     std::vector<std::string> wordListCopy = wordList;
  189.  
  190.     while (wordListCopy.size() > 0)
  191.     {
  192.         int random = rand() % wordListCopy.size();
  193.  
  194.         words.push_back(wordListCopy[random]);
  195.  
  196.         wordListCopy.erase(wordListCopy.begin() + random);
  197.     }
  198.  
  199.     return words;
  200. }
  201.  
  202.  
  203. //startup announcements
  204. void startUpText()
  205. {
  206.     std::cout << "~~~~~Hangman Test V0.1!~~~~~\n\n";
  207.  
  208.     std::cout << "For some reason not yet determined...\nyou are going to be hung - but don't worry.\n\n";
  209.  
  210.     std::cout << "You will not be hung if you can manage to guess a word within a certain amount  of tries.\n\n";
  211.  
  212.     std::cout << "You will guess letters in an attempt to solve the word, or guess the entire word\n\n";
  213.  
  214.     std::cout << "Words will be randomly selected. To guess, type the letter or entire word, and  press enter. It is not case sensitive.\n\n\n\n";
  215. }
  216.  
  217. int main(int argc, const char * argv[])
  218. {
  219.     startUpText();
  220.  
  221.     //random
  222.     srand(time(0));
  223.  
  224.     std::string currentGuess;
  225.  
  226.     //string edited during guesses, starts out blank and blanks are filled in with actual letters
  227.     std::string currentGuessingString;
  228.  
  229.     //index of string in words that is the word being guessed
  230.     int index = 0;
  231.  
  232.     //"health", or whatever counts till hangman dies
  233.     int health;
  234.  
  235.     //list of inputs, so you do not repeat an input
  236.     std::vector<std::string> inputs;
  237.  
  238.     //word array everything draws from
  239.     std::vector<std::string> words = getRandomizedWordList();
  240.  
  241.    
  242.    
  243.  
  244.     //main game loop
  245.     while (true)
  246.     {  
  247.         currentGuessingString = getBlankWord(words, index);
  248.  
  249.         health = 6;
  250.  
  251.         inputs.clear();
  252.        
  253.         while (true)
  254.         {
  255.             printHealthAndWord(health, currentGuessingString);
  256.  
  257.             currentGuess = getGuess(words[index]);
  258.  
  259.             std::cout << "\n\n";
  260.  
  261.             if (!newInput(currentGuess, inputs))
  262.             {
  263.                 std::cout << "You have tried to guess that before. Try again\n\n";
  264.  
  265.                 continue;
  266.             }
  267.             else
  268.                 inputs.push_back(currentGuess);
  269.  
  270.             if (checkGuess(currentGuess, words, index))
  271.             {
  272.                 if (currentGuess.length() > 1)
  273.                 {
  274.                     std::cout << "You have correctly guessed the word! You will not be hung!\n";
  275.  
  276.                     std::cout << "Do you want to play again?\n\n";
  277.  
  278.                     if (getYesNo())
  279.                         break;
  280.                     else
  281.                         return 0;
  282.                 }
  283.                 else
  284.                 {
  285.                     fillInLetters(currentGuess, words, index, currentGuessingString);
  286.  
  287.                     std::cout << "Your have guessed correctly!\n\n";
  288.                 }
  289.             }
  290.             else
  291.             {
  292.                 --health;
  293.  
  294.                 std::cout << "You have guessed incorrectly!\n\n";
  295.  
  296.                 //death
  297.                 if (health == 0)
  298.                 {
  299.                     std::cout << "You have been hung.\n\n";
  300.  
  301.                     std::cout << "The word was " << words[index] << "\n\n";
  302.  
  303.                     std::cout << "Would you like to play again?\n\n";
  304.  
  305.                     if (getYesNo())
  306.                         break;
  307.                     else
  308.                         return 0;
  309.                 }
  310.             }
  311.            
  312.             if (checkForWin(currentGuessingString, words, index))
  313.             {
  314.                 std::cout << "Congratulations, you have solved the word and will not be hung!\n\n";
  315.  
  316.                 std::cout << "Would you like to play again?\n\n";
  317.  
  318.                 if (getYesNo())
  319.                         break;
  320.                     else
  321.                         return 0;
  322.             }
  323.         }
  324.        
  325.         ++index;
  326.  
  327.         index %= words.size();
  328.  
  329.     }
  330.    
  331.  
  332.  
  333.  
  334.  
  335. }
Advertisement
Add Comment
Please, Sign In to add comment