So I’ve coded a C++ hangman game, And I wanted to share it with the community (As they have helped with a lot of bugs and errors) here is the code:
// 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 clue; // Makes the word easier to guess.
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;
//Input the clue:
cout << player1 << " Please input a clue for " << player2 << ":\n";
cout << "> ";
cin >> clue;
//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)) //Our guess loop
{
//IF YOU COMPLETE THE WORD
if(soFar == THE_WORD) {
cout << "YOU WIN!\n";
cout << "Type: ./play to start the game again...\n";
won = true;
play == false;
return 0;
}
//Start Game.
else if(wrong == 0) {
cout << "The word So Far: \n";
cout << soFar << "\n\n";
cout << "Clue: " << clue << "\n";
}
//Draw hangman
else if(wrong==1) {
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n\n";
cout << "Clue: " << clue << "\n";
}
else if(wrong==2) {
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n\n";
cout << "Clue: " << clue << "\n";
}
else if(wrong==3) {
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n\n";
cout << "Clue: " << clue << "\n";
}
else if(wrong==4) {
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n\n";
cout << "Clue: " << clue << "\n";
}
else if(wrong==5) {
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n\n";
cout << "Clue: " << clue << "\n";
}
else if(wrong==6) {
cout << "|---" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "The word So Far: \n";
cout << soFar << "\n\n";
cout << "Clue: " << clue << "\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\n";
cout << "Clue: " << clue << "\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\n";
cout << "Clue: " << clue << "\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\n";
cout << "Clue: " << clue << "\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\n";
cout << "Clue: " << clue << "\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\n";
cout << "Clue: " << clue << "\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;
cout << "Type: ./play to start the game again...\n";
won = false;
return 0; //End Loop
} //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;
}
}
Please note that I made a swear word detector in the code, but I deleted it before posting to the forums
(As there may be young learners, also I don’t wanna get in trouble…)
Also feel free to use the code for your own projects!
(NOTE: if you want to run the code paste it into a C++ Compile and Execute course,
As bash is not available for projects, as much as I wish it was. )
If you can think of ways to improve it feel free to do so! Hey and maybe you can post YOUR
code here? I hope we can all learn from each other and improve our coding!
And remember: Happy Coding!