Program: Hangman


This is a simple Hangman game for the Raspberry Pi, Apple Mac or Ubuntu PC.


The program chooses a secret word at random from an array of string values called words. (For brevity, I have only included a measly ten words. Feel free to add your own!)

The user is repeatedly asked to guess a letter from the secret word. A simple for loop is used to examine the secret word for the new letter. If the letter is in the secret word, it is added in to the guessed word so far.

If the letter has been found to be in the secret word, the program checks whether the entire word has now been guessed, signalling the end of the game.

To try out the program, select and copy the C++ source-code at the bottom of this post.

Paste into a text editor, such as Nano or Geany.

Then save the new file, ending in .cpp

I used  hangman.cpp


To compile from the command-line:


    g++ -o hangman hangman.cpp


To run from the command-line:


    ./hangman


Here's the code:

#include <iostream>  // For screen and keyboard
#include <cstdlib>  // To use random() function
#include <ctime>  // To seed random function using time()

using namespace std;  // Using cout, cin, endl

int main()
{
    string words[] = { "buzzard", "pasty", "owl", "cow", "cookie", "rotten", "frisbee", "alien", "blizzard", "bonkers" };
    
    const int NUM_WORDS = 10;
    
    // Seed the randome number process using current time
    srandom( time( 0 ) );
    
    cout << "H A N G M A N" << endl;
    cout << endl;
    
    // Choose secret word at random from the words array
    int ran = random() % NUM_WORDS;
    string secret = words[ran];

    // Find out how many letters secret word contains
    int len = secret.length();
    
    // Will record correctly guessed letters
    string guess( len, '-' );
    
    // Indicates whether guessed the word yet
    bool gotIt = false;
    
    int lives = 5;

    char letter;
    
    // Keep guessing while still have chances left and not guessed whole word
    while ( lives > 0  and  not gotIt )
    {
        // Display letters guessed so far
        cout << guess << endl << endl;
        
        // Take a new guess
        cout << "Guess a letter: ";
        cin >> letter;
        letter = tolower(letter);
        
        // Determine whether letter is in secret word
        bool letterIsInWord = false;
        
        for ( int i = 0;  i < len;  i++ )
        {
            
            if ( secret.at(i) == letter )
            {
                guess.at(i) = letter;
                letterIsInWord = true;
            }  // end of if
            
        }  // end of for
        
        if ( letterIsInWord )
            cout << letter << " is in the word." << endl;
        else
        {
            cout << "Sorry... not in the word." << endl;
            lives--;
            cout << lives << " incorrect guesses left." << endl;
        }  // end of else
        
        // Determine whether word has now been correctly guessed or not
        bool foundAnyDashes = false;
        int i = 0;
        while ( i < len  and  not foundAnyDashes )
        {
            if ( guess.at(i) == '-' )
                foundAnyDashes = true;

            i++;
        }  // end of inner while
        
        gotIt = not foundAnyDashes;
        
    }  // end of outer while
    
    // End of the game
    if ( gotIt )
        cout << "Well done! It's " << secret << "." << endl;
    else
        cout << "Too bad! The word was " << secret << "." << endl;

}

More programs and details in the book - available now on Amazon for £14.95 (UK) or $16.50 (USA)