Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.69 KB | None | 0 0
  1. #include <iostream>   //Input/Output objects
  2. #include <string>     //String objects
  3. #include <fstream>    //File objects
  4. #include <cstdlib>    //For rand and srand
  5. #include <ctime>      //For time function
  6.  
  7. using namespace std;  //Name-space used in the System Library
  8.  
  9. //User Libraries
  10.  
  11. //Global Constants
  12.  
  13. //Function prototypes
  14. void onePlayer();
  15. void twoPlayer();
  16.  
  17. //Execution Begins Here!
  18. int main(int argc, char** argv) {
  19.     //Declaration of Variables
  20.     unsigned char select;
  21.    
  22.     //Input values
  23.     //Main menu selection with input validation
  24.     cout <<  " , ,  _   ,  ,  _,  , , _   ,  ,      \n"
  25.              " |_|,'|\\  |\\ | / _ |\\/|'|\\  |\\ | \n"
  26.              "'| |  |-\\ |'\\|'\\_|`| `| |-\\ |'\\| \n"
  27.              " ' `  '  `'  `  _| '  ` '  `'  `      \n"
  28.              "               '                        " << endl;
  29.            
  30.     cout << "Welcome to Hangman!";
  31.     cout << "\n   1. One Player!"
  32.             "\n   2. Two Player!!"
  33.             "\n   X. Quit :( " << endl;
  34.    
  35.     do {
  36.         cin >> select; cin.ignore();//Player input
  37.     } while(!(select == '1' || select == '2' || select == 'X' || select == 'x'));
  38.    
  39.     switch (select) {
  40.         case '1': onePlayer(); break;
  41.         case '2': twoPlayer(); break;
  42.         case 'x':
  43.         case 'X': ;
  44.     }
  45.    
  46.     cout << "\nThanks for playing!\n";
  47.    
  48.     //Exit Program
  49.     return 0;
  50. }
  51.  
  52. void onePlayer(){
  53.     //Variable declaration
  54.     unsigned char sizeLst= 0;  //Size of word list
  55.     char guess;                //Player's guess
  56.     string diff,               //Difficulty
  57.            word,               //Word used for hangman
  58.            blnk = "",          //Blank lines for unknown letters
  59.            guessed = "";       //Already guessed letters
  60.     unsigned seed = time(0);   //Create seed from system time
  61.     unsigned short lives = 9;  //Player's life
  62.     bool loop = true,          //Used as condition for while loop
  63.          correct = true,       //If guess was wrong, take a life away
  64.          repeat = false;       //A check if letter has been guessed
  65.     srand(seed);               //Seed the random number generator
  66.     ifstream inDiff;           //Input file for difficulty
  67.    
  68.     diff:
  69.     //Player chooses difficulty with input validation
  70.     do {
  71.         cout << "\nWhich difficulty would you like to play, easy(1), moderate(2) "
  72.                 "or hard(3)?" << endl;
  73.         cin >> diff; cin.ignore();
  74.     } while (!(diff == "easy" || diff == "1" || diff == "moderate" || diff == "2" || diff == "hard" || diff == "3"));
  75.     cout << endl;
  76.    
  77.     //Depending on player's choice of difficulty, load text file containing custom # of words
  78.     //If file couldn't load, return to difficulty menu
  79.     if (diff == "easy" || diff == "1") {
  80.         inDiff.open("easy.txt");
  81.     sizeLst = 200;
  82.     } else if (diff == "moderate" || diff == "2") {
  83.         inDiff.open("moderate.txt");
  84.     sizeLst = 97;
  85.     } else if (diff == "hard" || diff == "3") {
  86.         inDiff.open("hard.txt");
  87.         sizeLst = 100;
  88.     } else cout << "Error." << endl;
  89.    
  90.     string wordLst[sizeLst];
  91.    
  92.     if (inDiff) {
  93.         //cout << "\nFile successfully loaded.\n\n";
  94.        
  95.         for (int i = 0; i < sizeLst; i++) {
  96.             inDiff >> wordLst[i];
  97.         }
  98.     } else {
  99.         cout << "File couldn't load. :(" << endl;
  100.         goto diff;
  101.     }
  102.    
  103.     word = wordLst[rand() % sizeLst]; //Assign random word from wordLst to variable 'word'
  104.    
  105.     cout << "Word: " << word << endl;
  106.     cout << "Length of word: " << word.length() << "\n\n";
  107.    
  108.     //For every letter in word, add another underscore _ to variable 'blnk'
  109.     for (int i = 0; i < word.length(); i++) {
  110.         blnk += "_";
  111.     }
  112.    
  113.     //GAME MECHANICS
  114.     //While loops as long as word hasn't been guessed OR player's live is not 0
  115.     //It shows mystery word and player inputs guess with input validation
  116.     //If guess was incorrect, display deducted lives count
  117.     //
  118.     while (loop) {
  119.         correct = true;
  120.        
  121.         cout << blnk << "   Guessed: " << guessed << "\n";
  122.  
  123.         //Enter guess
  124.         do {
  125.             repeat = false;
  126.             cout << "Guess: ";
  127.             cin >> guess; cin.ignore();
  128.        
  129.             //If you guessed an already used letter
  130.             for (int i = 0; i < guessed.length(); i++) {
  131.                 if (guess == guessed[i]) {
  132.                     repeat = true; //if the guess is already guessed then set to true
  133.                     cout << "You've already guessed this letter.\n";
  134.                 }
  135.             }
  136.        
  137.         //Validation - Check if guess is a lowercase letter that hasn't yet been guessed
  138.         } while ((guess < 96 || guess > 123) && !repeat);
  139.        
  140.         //Only add player's guess in variable 'guessed' if it hasn't been guessed yet
  141.         if (guessed.find(guess) == string::npos) guessed += guess;
  142.        
  143.         //If you didn't guess right
  144.         for (int i = 0; i < word.length(); i++) {
  145.             if (word.find(guess) == string::npos) correct = false;
  146.         }
  147.        
  148.        
  149.        
  150.         if (correct == false) cout << "Your guess was wrong. You have " << --lives << " lives left.\n\n";
  151.        
  152.         if (lives == 0){
  153.             cout << "You lose. The word was " << word << ".\n\n";
  154.             loop = false;
  155.         }    
  156.        
  157.         //If you guessed right
  158.         for (int i = 0; i < word.length(); i++) {
  159.             if (guess == word[i]) {
  160.                 blnk[i] = guess;
  161.             }
  162.         }
  163.        
  164.         if (word == blnk) {
  165.             cout << "You win! The word was " << word << ".\n\n";
  166.         }
  167.     }
  168. }
  169.  
  170. void twoPlayer() {
  171.     //Variable declaration
  172.     char guess;                //Player's guess
  173.     string word = "",          //Word used for hangman
  174.            blnk = "",          //Blank lines for unknown letters
  175.            guessed = "";       //Already guessed letters
  176.     unsigned short lives = 9;  //Player's life
  177.     bool loop = true,          //Used as condition for while loop
  178.          correct = true,       //If guess was wrong, take a life away
  179.          repeat = false;       //A check if letter has been guessed
  180.    
  181.     //Player Two inputs word for Player One to guess
  182.     cout << "\nInput the word, Player Two! ";
  183.     cin >> word; cin.ignore();
  184.    
  185.     //Converts user's capitalized word to all lowercase
  186.     for (int i=0; i< word.length(); i++) {
  187.          word[i] = tolower(word[i]);
  188.     }
  189.    
  190.     cout << string( 100, '\n' ); //Clear screen
  191.    
  192.     //cout << "Word: " << word << endl;
  193.     cout << "Length of word: " << word.length() << "\n\n";
  194.    
  195.     //For every letter in word, add another underscore _ to variable 'blnk'
  196.     for (int i = 0; i < word.length(); i++) {
  197.         blnk += "_";
  198.     }
  199.    
  200.     //GAME MECHANICS
  201.     //While loops as long as word hasn't been guessed OR player's live is not 0
  202.     //It shows mystery word and player inputs guess with input validation
  203.     //If guess was incorrect, display deducted lives count
  204.     //
  205.     while (loop) {
  206.         correct = true;
  207.        
  208.         cout << blnk << "   Guessed: " << guessed << "\n";
  209.  
  210.         //Enter guess
  211.         do {
  212.             repeat = false;
  213.             cout << "Guess: ";
  214.             cin >> guess; cin.ignore();
  215.        
  216.             //If you guessed an already used letter
  217.             for (int i = 0; i < guessed.length(); i++) {
  218.                 if (guess == guessed[i]) {
  219.                     repeat = true; //if the guess is already guessed then set to true;
  220.                     cout << "You've already guessed this letter.\n";
  221.                 }
  222.             }
  223.        
  224.         //Validation - Check if guess is a lowercase letter that hasn't yet been guessed
  225.         } while ((guess < 96 || guess > 123) && !repeat);
  226.        
  227.         //Only add player's guess in variable 'guessed' if it hasn't been guessed yet
  228.         if (guessed.find(guess) == string::npos) guessed += guess;
  229.        
  230.         //If you didn't guess right
  231.         for (int i = 0; i < word.length(); i++) {
  232.             if (word.find(guess) == string::npos) correct = false;
  233.         }
  234.        
  235.         if (correct == false) cout << "Your guess was wrong. You have " << --lives << " lives left.\n\n";
  236.        
  237.         if (lives == 0){
  238.             cout << "You lose. The word was " << word << ".\n\n";
  239.             loop = false;
  240.             }    
  241.        
  242.        
  243.         //If you guessed right
  244.         for (int i = 0; i < word.length(); i++) {
  245.             if (guess == word[i]) {
  246.                 blnk[i] = guess;
  247.             }
  248.         }
  249.        
  250.         if (word == blnk) {
  251.             cout << "You win! The word was " << word << ".\n";
  252.             break;
  253.         }
  254.     }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement