Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************************************
- * AUTHOR : Summer Huang
- * Assignment #6 : Hangman
- * CLASS : CS 002
- * SECTION : MTWR: 7:30a – 12:30p
- * Due Date : 1/22/2018
- *****************************************************************/
- #include <iostream>
- #include <string>
- #include <cctype>
- #include <cstdlib>
- using namespace std;
- // function declarations
- string setupNotSolved(string);
- char getGuess(string);
- string updateNotSolved(string phrase,string unsolved,char guess);
- string prevGuesses;
- string phrase;
- void clearscreen();
- // global variables to maintain the game status
- int incorrectGuesses=0;
- bool isFinished = false;
- bool validGuess = false;
- int main()
- {
- string unsolved;
- int a;
- char ch;
- // prompt the first player to enter a phrase
- cout << "Enter phrase: ";
- getline(cin, phrase);
- unsolved = setupNotSolved(phrase);
- system("CLS");
- // start the game
- while (!isFinished && incorrectGuesses < 7)
- {
- // display the unsolved phrase
- cout << endl << "Phrase: ";
- cout << unsolved << endl;
- // display correctly guessed characters up until now
- cout << "Guessed so far: " << prevGuesses << endl;
- // prompt the user to enter a guess
- ch = getGuess(prevGuesses);
- unsolved = updateNotSolved(phrase, unsolved, ch);
- cout << "Wrong guesses left: " << 7 - incorrectGuesses << endl;
- }
- system("CLS");
- // display game result
- if (isFinished)
- {
- cout << "Congratulations!! You won!" << endl;
- }
- if (incorrectGuesses >= 7)
- {
- cout << "You lost!" << endl;
- }
- return 0;
- }
- ///@brief Puts dashes in place of alphabetic characters in the phrase.
- ///@param phrase the phrase to be solved
- ///@return the phrase with all alphabetic characters converted to dashes
- string setupNotSolved(string phrase)
- {
- // creating unsolved phrase
- for(int k = 0;k < phrase.length(); k++)
- {
- if (isalpha(phrase.at(k)))
- {
- phrase.at(k) = '-';
- }
- }
- return phrase;
- }
- ///@brief Replaces the dashes with the guessed character.
- ///@param phrase the phrase to be solved
- ///@param unsolved the phrase with dashes for all unsolved characters
- ///@param guess the char containing the current guessed character
- ///@return the new un solved string with dashes replaced by new guess
- string updateNotSolved(string phrase,string unsolved,char guess)
- {
- int i;
- int j;
- int len;
- len = 0;
- validGuess=false;
- // pre processing the phrase
- for(i = 0; i < unsolved.length(); i++)
- {
- if(unsolved.at(i)!='-')
- {
- phrase.at(i)='-';
- }
- }
- for(j = 0;j < phrase.length(); j++)
- {
- //if valid guess
- if(guess == phrase.at(j))
- {
- validGuess = true;
- unsolved.at(j) = guess;
- phrase.at(j) = '-';
- prevGuesses.append(string(1,guess));
- break;
- }
- }
- // update incorrect guesses count, if the guess is not a valid one
- if(!validGuess)
- {
- incorrectGuesses++;
- }
- // update the status of the game as finished,
- // if the phrase contains only '_'s
- for(i = 0;i < phrase.length(); i++)
- {
- if(phrase.at(i) == '-')
- {
- len++;
- }
- }
- if(len == phrase.length())
- {
- isFinished=true;
- }
- return unsolved;
- }
- ///@brief Gets validguess as input.
- ///
- /// A guess is taken as input as a character. It is valid if
- /// 1)it is an alphabetic character; and
- /// 2)the character has not already been guessed
- ///
- ///@param prevGuesses the string containing all characters guessed so far
- ///@return a valid guess and only a valid guess as a character
- char getGuess(string prevGuesses)
- {
- char ch;
- prevGuesses = "";
- // prompt user to enter a valid guess
- cout << "Enter a guess: ";
- cin >> ch;
- cout << endl;
- if(isalpha(ch))
- {
- return ch;
- }
- else
- {
- while(!isalpha(ch))
- {
- cout << "Invalid guess! Please re-enter a guess: ";
- cin >> ch;
- cout << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement