Programming a hangman game in C++

In this article, we will look at implementing a hangman game in C++ that applies the following programming concepts in the language:

The code has been documented clearly outlining how each block of code allows the game to function properly. If you are interested in getting stuck into video game programming, I recommend the book “Beginning C++ through game programming“. With this book, you instantly start getting into programming games with C++ and it was one of the few resources I understood when I was beginning with C++.

</pre>
<pre>/*
====================================================================
Hangman in C++
Made By Sahil Bora 
Made with Visual Studio 2010
====================================================================
*/

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Included files
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Main part of the game
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
int main()
{
 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 // SetUp of the game
 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 const int MAX_WRONG = 8; // Maxiumum number of incorrect guesses allowed

 vector<string> words; // collection of possible words to guess
 words.push_back("GUESS");
 words.push_back("HANGMAN");
 words.push_back("DIFFICULT");

 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 //Randomize the words
 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 srand(time(0));
 random_shuffle(words.begin(), words.end());
 const string THE_WORD = words[0]; // word to guess
 
 int wrong = 0; // number of incorrect guesses
 string soFar(THE_WORD.size(), '_'); // word guessed so far
 string used = ""; // letters already guessed

 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 // Main screen 
 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

 cout << "Welcome to Hangman. Good luck!\n";

 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 // Main Loop
 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 while((wrong < MAX_WRONG) && (soFar != THE_WORD))
 {
 cout << "\nYou have " << (MAX_WRONG − wrong) << "incorrect guesses left.\n";
 cout << "\nYou've used the following letters:\n" << used << endl;
 cout << "\So far, the word is:\n" << soFar << endl;

 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 // Getting the Player's Guess
 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 char guess;

 cout << "\nEnter your guess: ";
 cin >> guess;

 guess = toupper(guess); // make uppercase since secret word in uppercase

 while(used.find(guess) != string::npos)
 {
 cout << "\nYou have already guessed" << guess << endl;
 cout << "Ente your guess ";
 cin >> guess;

 guess = toupper(guess);
 }

 used += guess;

 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 // If it's the word or not
 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 if (THE_WORD.find(guess) != string::npos)
 {
 cout << "That's right! " << guess << " is in the world.\n";

 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 // Update soFar to included newly guessed letter
 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 for (int i = 0; i < THE_WORD.length(); ++i)
 if (THE_WORD[i] == guess)
 soFar[i] = guess;
 }
 else
 {
 cout << "Sorry " << guess << " isn't in the word.\n";
 ++wrong;
 }
 }

 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
 // Ending the Game
 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

 if (wrong == MAX_WRONG)
 cout << "\nYou're been hanged";
 else
 cout << "\nYou guessed it";

 cout << "\nThe word was " << THE_WORD << endl;

 return 0;
}</pre>
<pre>
Share this post