daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Mar 19th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Program Name: hangman
  2. Author: Eros Mendoza
  3. Date : 16 March 2018
  4. IDE : MS Visual Studio 2015 Enterprise
  5. Description :  A program named hangman which prompts Player 1 types in a secret word for player
  6. 2 to guess and player 2 tries to guess the word 1 letter at a time. Player 2 has a maximum
  7. number of wrong guesses at which point the game is over and player 1 wins. At the end of the game
  8. the winner is printed.
  9. */
  10.  
  11. #include <iostream>
  12. #include <string>
  13. #include <cstring>
  14.  
  15. using namespace std;
  16.  
  17. const int MAX_WRONG_GUESSES = 6;
  18. const int MAX_SECRET_WORD_LENGTH = 15;
  19. void welcome();
  20. void getWord(char secretWord[]);
  21.  
  22. int main() {
  23.  
  24.     char secretWord[MAX_SECRET_WORD_LENGTH + 1] = {};
  25.     string guesses = "joe";
  26.     char secretWordClone[MAX_SECRET_WORD_LENGTH + 1] = {};
  27.     int numGuesses = 0;
  28.     int wrongGuesses = 0;
  29.     char underscore[1] = { 95 };
  30.     bool containsLetters = false;
  31.     bool player2Wins = false;
  32.  
  33.     welcome();
  34.  
  35.     getWord(secretWord);
  36.  
  37.     for (int x = 0; x < strlen(secretWord); x++)
  38.         secretWordClone[x] = underscore[0];
  39.  
  40.  
  41.     while (!player2Wins) {
  42.  
  43.         if (wrongGuesses == MAX_WRONG_GUESSES) {
  44.             player2Wins = false;
  45.             cout << endl << "You have reached the maximum number of wrong guesses. Player 1 wins and Player 2 loses. The secret word was: " << secretWord << endl << endl;
  46.             break;
  47.         }
  48.  
  49.         if (secretWord == secretWordClone) {
  50.             player2Wins = true;
  51.             cout << "You guessed the word! Player 2 wins and player 1 loses. The secret word was: " << secretWord << endl << endl;
  52.             break;
  53.         }
  54.  
  55.         cout << "Secret Word: " << secretWordClone << endl << endl;
  56.  
  57.         cout << "Player 2, please guess a letter: ";
  58.         cin >> guesses;
  59.  
  60.         containsLetters = false;
  61.  
  62.         for (int x = 0; x < strlen(secretWord); x++) {
  63.             if (secretWord[x] == guesses[0]) {
  64.                 secretWordClone[x] = guesses[0];
  65.                 containsLetters = true;
  66.             }
  67.         }
  68.  
  69.         if (!containsLetters) {
  70.             wrongGuesses = wrongGuesses + 1;
  71.             containsLetters = false;
  72.         }
  73.  
  74.     }
  75. return 0;
  76. }
  77.  
  78. void welcome() {
  79.     cout << "Welcome to The Hang Man Game! The rules are simple. Player 1 types in a secret word for player 2 to guess and player 2 ";
  80.     cout << "tries to guess the word 1 letter at a time. Player 2 has a maximum number of " << MAX_SECRET_WORD_LENGTH << " wrong guesses at which point the game is over and player 1 wins. Good Luck. ";
  81.     cout << endl << endl;
  82. }
  83.  
  84. void getWord(char secretWord[]) {
  85.  
  86.     cout << "Player 1, please enter a secret word (must be " << MAX_SECRET_WORD_LENGTH << " or less characters): ";
  87.     cin >> secretWord;
  88.  
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top