C++ Hangman Game Guide

Jul 9, 2025

Overview

This lecture explains how to create a letter-guessing game in C++, similar to Hangman, covering random phrase selection, user interaction, and optional enhancements like ASCII art and color.

Initial Setup

  • Include <iostream> for input/output and <time.h> for random number generation.
  • Seed the random number generator at the beginning of the program.
  • Start with a simple intro screen, but focus on core game functionality first.

Selecting a Random Secret Phrase

  • Create a string array (e.g., phrases[]) with several secret phrases to choose from.
  • To pick a random phrase: use phrases[rand() % number_of_phrases].
  • Test the phrase selection by printing the chosen secret phrase (remove in the final version).

Preparing the Guess Phrase

  • Define a string guessPhrase initialized as secretPhrase.
  • Loop through each character; replace letters with placeholders (e.g., periods '.') except for spaces.
  • Display guessPhrase alongside secretPhrase for testing alignment (remove debug prints later).

Game Variables and Loop

  • Use an integer badGuesses to count incorrect guesses, starting at 0.
  • Set a maximum allowed badGuesses (e.g., 6, like traditional Hangman).
  • Store user input letter in a string variable letter.
  • Loop continues while guessPhrase != secretPhrase and badGuesses is less than the limit.

Core Game Logic

  • Show graphics or ASCII art based on the value of badGuesses (add after core logic).
  • Display letters remaining using a string (e.g., lettersRemaining initialized to alphabet).
  • Remove guessed letters from lettersRemaining after each guess.
  • Output both lettersRemaining and the current guessPhrase clearly for the user.
  • Prompt user to enter a letter and update the game state accordingly.

Guess Handling

  • If the guessed letter is not in lettersRemaining, skip processing.
  • Remove guessed letter from lettersRemaining.
  • If the guessed letter is in secretPhrase, update guessPhrase at all matching positions.
  • If not present, increment badGuesses.

End of Game

  • After the loop, check if the user has won (guessed all letters) or lost (reached max bad guesses).
  • Display a winning or losing message and show the completed phrase.
  • Recommend moving display screens (intro, win, lose) to separate functions for cleaner code.

Enhancements ("Bells and Whistles")

  • Add ASCII art for graphics using conditional statements on badGuesses.
  • Change text color using ANSI escape sequences for better visuals.
  • Use CLS escape sequence to clear the screen between updates.
  • Consider using online ASCII text generators for custom title screens.
  • Use cin.ignore() to pause on the intro screen until the user presses Enter.
  • Declare function prototypes for any additional display or screen functions at the top.

Key Terms & Definitions

  • Secret Phrase — The randomly chosen phrase the user is trying to guess.
  • Guess Phrase — A masked version of the secret phrase showing placeholders and spaces.
  • Bad Guesses — Counter for the number of incorrect guesses made by the player.
  • Letters Remaining — String showing which alphabet letters are still available for guessing.
  • ASCII Art — Visual art created using text characters, often used for graphics in console games.
  • ANSI Escape Sequences — Codes for manipulating text color and formatting in terminals.

Action Items / Next Steps

  • Test your game thoroughly without enhancements before adding graphics or colors.
  • Refactor intro, win, and lose screens into separate functions for better structure.
  • Add custom phrases, ASCII art, and theme elements after basic logic works.
  • Read up on ANSI escape sequences if you want colored output and screen clearing.