Right! so here’s the deal: In my own C++ project: HANGMAN, I’ve achieved the following:
-
TAKE USER INPUT
-
STORE THE_WORD IN A VARIABLE AND HAVE PLAYER 2 GUESS IT
-
DRAW HANGMAN IF PLAYER 2 STARTS GETTING IT WRONG
-
HAVE THE PLAYER GUESS INDIVIDUAL LETTERS
-
LET PLAYER WIN IF THEY GUESS ALL THE LETTERS
And my current step is: DEBUGGING
But there is one bug in which the answer has eluded me…
IF you win, the game still displays the text: Please make your input:
It shouldn’t, but it does, anyhow, here is the FULL CODE for reference.
(Feel free to use the code for your own project. )
(Code is commented more, so you can understand it a bit more… )
// Hangman
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
//Declare our variables
string THE_WORD; // word to guess
int wrong; // amount of times you get the word wrong.
string soFar; // The correct letters So Far.
string used; // the letters you've used.
string player1; // Player 1's name.
string player2; // Player 2's name.
bool play = true; // Ends the game and prevents a really bad glitch.
bool won = false; //Works as a backup if play doesn't work.
bool match(char letter, string word);
char askGuess(string usedLettersStr); //tells the compiler of method askGuess
bool playAgain();
int main()
{
srand(time(0));
cout << "Welcome to Hangman!\n\n";
//Ask the users to input their names using std::cin, Then we store the names into the player variables so we can use them later.
cout << "Please input Player one's name: ";
cin >> player1;
cout << "\n\n";
cout << "Please input Player two's name: ";
cin >> player2;
cout << "\n\n\n";
//Ask player 1 to input the word that player 2 must guess...
cout << player1 << " Please input the word you want " << player2 << " to guess (NOTE PLEASE MAKE IT ALL CAPS OR THE CODE WILL GLITCH)\n";
cout << "> ";
cin >> THE_WORD;
//Create the space so that Player 2 can't see the word player 1 chose.
for (int x=0; x<30; x++){
cout << endl;
}//end: for(int x=0; x<30; x++)
// game loop starts here
bool w = false;
do
{
const int MAX_WRONG = 12; // maximum number of incorrect guesses allowed
soFar = string(THE_WORD.size(), '-'); // letters already guessed
while ((wrong <= MAX_WRONG) && (won == false))
{
//IF YOU COMPLETE THE WORD
if(soFar == THE_WORD) {
cout << "YOU WIN!\n";
won = true;
play == false;
}
//Start Game.
else if(wrong == 0) {
cout << "The word So Far: \n";
cout << soFar << "\n";
}
//Draw hangman
else if(wrong==1) {
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==2) {
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==3) {
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==4) {
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==5) {
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==6) {
cout << "|---" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==7) {
cout << "|---" << endl;
cout << "| O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==8) {
cout << "|---" << endl;
cout << "| O" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==9) {
cout << "|---" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==10) {
cout << "|---" << endl;
cout << "| O" << endl;
cout << "| /|\\" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==11) {
cout << "|---" << endl;
cout << "| O" << endl;
cout << "| /|\\" << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n";
}
else if(wrong==MAX_WRONG) {
cout << "|---" << endl;
cout << "| O" << endl;
cout << "| /|\\" << endl;
cout << "| / \\ "<< endl;
cout << "|" << endl;
cout << "YOU ARE DEAD\n";
cout << "Game over! The word was: " << THE_WORD <<endl;
won = true;
break;
} //endif
used += askGuess(used);
} // end of while ((wrong <= MAX_WRONG) && (won == false))
} while( play == true); //This is to prevent a bug in which the game makes you keep endlessly guessing the word...
return 0;
}
inline bool match(char letter, string word)
{
return ( word.find(letter) != string::npos );
}
char askGuess(string usedLettersStr)
{
//Player two guesses a letter.
char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess); //make uppercase Since word must be in uppercase
while (match(guess, used))
{
//If you already guessed that letter.
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
//If the letter you guessed is in the word
if (match(guess, THE_WORD))
{
cout << "That's right! " << guess << " is in the word.\n\n";
// update soFar to include newly guessed letter
for (int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;
}
//if not
else
{
cout << "Sorry, " << guess << " isn't in the word.\n\n";
++wrong;
}
}