Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cctype>
- #include <iostream>
- #include <string>
- using namespace std;
- // Function prototypes
- bool guess(string word, string &curWord, char c);
- bool hasChar(string word, char c);
- bool wordComplete(string curWord);
- string updateWord(string word, string &curWord, char c);
- string hideWord(string word);
- int main()
- {
- char a, curGuess;
- int playCtr = 0;
- const int maxSkip = 1000;
- do
- {
- int tries = 6;
- const int maxHistory = tries + 1; // cout << maxHistory // Output: 7
- char guessHistory[maxHistory]; // expected constant expression, cannot allocate array of constant size 0, 'guessHistory' : unknown size
- bool gameFinished = false;
- string word = "AssaultCube";
- string curWord = hideWord(word);
- cout << "\nWelcome to Hangman! Good luck!\n\n";
- cout << "The word is " << word.length() << " characters long.\n";
- // Game loop
- while(!gameFinished)
- {
- // Ensure that alphabetical input is read
- curGuess = ' ';
- while (!isalpha(curGuess))
- {
- cout << "\nEnter your guess (an alphabetical character): ";
- cin.get(curGuess);
- curGuess = tolower(curGuess);
- cin.ignore(maxSkip, '\n');
- }
- // If the guess was incorrect, decrement the tries var and notify the player
- if (!guess(word, curWord, curGuess))
- {
- tries--;
- cout << "You have " << tries << " guesses left.\n";
- }
- // Break out of the loop if the player wins or loses
- if (wordComplete(curWord))
- {
- cout << "\nGAME OVER - Congratulations, you won!\n";
- gameFinished = true; // break
- }
- else
- {
- if (tries < 1)
- {
- cout << "\nGAME OVER - Sorry, you lost!\n\n";
- cout << "The word was " << word << "\n";
- gameFinished = true; // break
- }
- }
- }
- playCtr++;
- // Ensure that y or n input is read
- a = '\0';
- while(tolower(a) != 'n' && tolower(a) != 'y')
- {
- cout << "\a\nPlay again? y/n? ";
- cin.get(a);
- cin.ignore(maxSkip, '\n');
- }
- } while(tolower(a) == 'y');
- if (playCtr < 2)
- {
- cout << "\nGood game, bye!\n";
- }
- else
- {
- cout << "\nGood games, bye!\n";
- }
- return 0;
- }
- // hideWord() - Initially masks the chosen word with dashes "-"
- string hideWord(string word)
- {
- string output = "";
- for (unsigned int i = 0; i < word.length(); i++)
- {
- output += "-";
- }
- return output;
- }
- // updateWord() - updates the temporarily masked word upon correct guesses
- string updateWord(string word, string &curWord, char c)
- {
- string output = "";
- for (unsigned int i = 0; i < word.length(); i++)
- {
- char letter = word.at(i);
- if (tolower(letter) == c)
- {
- output += c;
- }
- else
- {
- letter = curWord.at(i);
- if (tolower(letter) != '-')
- {
- output += letter;
- }
- else
- {
- output += "-";
- }
- }
- }
- curWord = output;
- return output;
- }
- // hasChar() - Determines if the user's guess was found in the word
- bool hasChar(string word, char c)
- {
- for (unsigned int i = 0; i < word.length(); i++)
- {
- char letter = word.at(i);
- if (tolower(letter) == c)
- {
- return true;
- }
- }
- return false;
- }
- // guess() - Handles the guesses users enter
- bool guess(string word, string &curWord, char c)
- {
- if (hasChar(word, c))
- {
- cout << "\nCorrect! The updated word is ";
- cout << updateWord(word, curWord, c) << "\n";
- return true;
- }
- else
- {
- cout << "\nIncorrect! The word is " << curWord << "\n\n";
- return false;
- }
- }
- // wordComplete() - Determines if the temporarily masked word has been completely unveiled
- bool wordComplete(string curWord)
- {
- for (unsigned int i = 0; i < curWord.length(); i++)
- {
- char letter = curWord.at(i);
- if (letter == '-')
- {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment