Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Program Name: hangman
- Author: Eros Mendoza
- Date : 16 March 2018
- IDE : MS Visual Studio 2015 Enterprise
- Description : A program named hangman which prompts Player 1 types in a secret word for player
- 2 to guess and player 2 tries to guess the word 1 letter at a time. Player 2 has a maximum
- number of wrong guesses at which point the game is over and player 1 wins. At the end of the game
- the winner is printed.
- */
- #include <iostream>
- #include <string>
- #include <cstring>
- using namespace std;
- const int MAX_WRONG_GUESSES = 6;
- const int MAX_SECRET_WORD_LENGTH = 15;
- void welcome();
- void getWord(char secretWord[]);
- int main() {
- char secretWord[MAX_SECRET_WORD_LENGTH + 1] = {};
- string guesses = "joe";
- char secretWordClone[MAX_SECRET_WORD_LENGTH + 1] = {};
- int numGuesses = 0;
- int wrongGuesses = 0;
- char underscore[1] = { 95 };
- bool containsLetters = false;
- bool player2Wins = false;
- welcome();
- getWord(secretWord);
- for (int x = 0; x < strlen(secretWord); x++)
- secretWordClone[x] = underscore[0];
- while (!player2Wins) {
- if (wrongGuesses == MAX_WRONG_GUESSES) {
- player2Wins = false;
- 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;
- break;
- }
- if (secretWord == secretWordClone) {
- player2Wins = true;
- cout << "You guessed the word! Player 2 wins and player 1 loses. The secret word was: " << secretWord << endl << endl;
- break;
- }
- cout << "Secret Word: " << secretWordClone << endl << endl;
- cout << "Player 2, please guess a letter: ";
- cin >> guesses;
- containsLetters = false;
- for (int x = 0; x < strlen(secretWord); x++) {
- if (secretWord[x] == guesses[0]) {
- secretWordClone[x] = guesses[0];
- containsLetters = true;
- }
- }
- if (!containsLetters) {
- wrongGuesses = wrongGuesses + 1;
- containsLetters = false;
- }
- }
- return 0;
- }
- void welcome() {
- 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 ";
- 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. ";
- cout << endl << endl;
- }
- void getWord(char secretWord[]) {
- cout << "Player 1, please enter a secret word (must be " << MAX_SECRET_WORD_LENGTH << " or less characters): ";
- cin >> secretWord;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement