Hangman game is a popular and simple game in which the player has to guess the word based on the given hint. Hangman is a word puzzle game that involves:
- A secret word: A word is selected and its number of characters is displayed to the user. The player then has to guess each letter of the word.
- Limited attempts: The guessing player has a fixed number of tries (usually six) to guess the word.
- Display: The game displays the partially guessed word with blanks for unguessed letters.
- Winning/Losing: The guessing player wins if they think is the word within the allowed attempts; otherwise, they lose.
There is a graphical representation of a person who keeps getting closer to hanging whenever a wrong guess is made hence the name "Hangman"

The game challenges players' word-guessing skills and deductive reasoning.
Logic Behind the Game
The Hangman program randomly selects a secret word and asks the player to guess it one letter at a time. The player has a limited number of incorrect attempts to guess the entire word.
The program maintains the following information:
- Secret Word: The word that the player has to guess.
- Guessed Word: Stores the current state of the word, with
_representing unguessed letters. - Guessed Letters: Tracks the letters already guessed to prevent duplicate guesses.
- Wrong Guesses: Counts the number of incorrect attempts made by the player.
Game Flow
- Start the game by selecting a random secret word and displaying its hint.
- Initialize the guessed word with
_for each character of the secret word. - Display the current state of the guessed word and the Hangman drawing.
- Prompt the player to enter a letter.
- Check if the entered letter has already been guessed.
- If the letter is correct, update all matching positions in the guessed word.
- If the letter is incorrect, increment the wrong guess count and update the Hangman drawing.
- Repeat the process until the player guesses the complete word or reaches the maximum number of wrong guesses.
- Display a winning message if the word is guessed correctly; otherwise, reveal the secret word and end the game.
Here is a more concise explanation of the logic:
while (not guessed and not out of guesses):
display word to be guessed
get player guess
if guess is not already guessed:
check if guess is in secret word
if guess is in secret word:
update guessed word
else:
increment number of wrong guesses
This logic can be implemented in any programming language. Here we use the C programming language.
Implementation
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_WORD_LENGTH 50
#define MAX_TRIES 6
// Structure to hold a word and its hint
struct WordWithHint {
char word[MAX_WORD_LENGTH];
char hint[MAX_WORD_LENGTH];
};
// Function declarations
void displayWord(const char guessedWord[]);
void drawHangman(int tries);
int main()
{
// Seed the random number generator
srand(time(NULL));
// Array of words with hints
struct WordWithHint wordList[] = {
{"geeksforgeeks", "Computer coding"},
{"elephant", "A large mammal with a trunk"},
{"pizza", "A popular Italian dish"},
{"beach", "Sandy shore by the sea"}
};
// Select a random word
int wordIndex = rand() % 4;
const char *secretWord = wordList[wordIndex].word;
const char *hint = wordList[wordIndex].hint;
int wordLength = strlen(secretWord);
// Store the current guessed word
char guessedWord[MAX_WORD_LENGTH];
// Track guessed letters
bool guessedLetters[26] = {false};
// Initialize guessedWord with '_'
for (int i = 0; i < wordLength; i++) {
guessedWord[i] = '_';
}
guessedWord[wordLength] = '\0';
printf("Welcome to Hangman!\n");
printf("Hint: %s\n", hint);
int tries = 0;
while (tries < MAX_TRIES) {
printf("\n");
displayWord(guessedWord);
drawHangman(tries);
char guess;
printf("Enter a letter: ");
scanf(" %c", &guess);
guess = tolower(guess);
// Validate input
if (guess < 'a' || guess > 'z') {
printf("Please enter a valid alphabet.\n");
continue;
}
// Check duplicate guess
if (guessedLetters[guess - 'a']) {
printf("You've already guessed that letter. Try again.\n");
continue;
}
guessedLetters[guess - 'a'] = true;
bool found = false;
// Search the guessed letter
for (int i = 0; i < wordLength; i++) {
if (secretWord[i] == guess) {
guessedWord[i] = guess;
found = true;
}
}
if (found) {
printf("Good guess!\n");
} else {
printf("Sorry, the letter '%c' is not in the word.\n", guess);
tries++;
}
// Check win condition
if (strcmp(secretWord, guessedWord) == 0) {
printf("\n");
displayWord(guessedWord);
printf("\nCongratulations! You've guessed the word: %s\n",
secretWord);
break;
}
}
// Check lose condition
if (tries >= MAX_TRIES) {
drawHangman(tries);
printf("\nSorry, you've run out of tries.\n");
printf("The word was: %s\n", secretWord);
}
return 0;
}
// Function to display the guessed word
void displayWord(const char guessedWord[])
{
printf("Word: ");
for (int i = 0; guessedWord[i] != '\0'; i++) {
printf("%c ", guessedWord[i]);
}
printf("\n");
}
// Function to draw the Hangman
void drawHangman(int tries)
{
const char *hangmanParts[] = {
" _________",
" | |",
" | O",
" | /|\\",
" | / \\",
" |"
};
printf("\n");
for (int i = 0; i <= tries && i < 6; i++) {
printf("%s\n", hangmanParts[i]);
}
}
Output 1
Welcome to Hangman!
Hint: Computer coding
Word: _ _ _ _ _ _ _ _ _ _ _ _ _ _
_________
Enter a letter: g
Good guess!
Word: g _ _ _ _ _ _ _ _ _ _ _ g _
_________
Enter a letter: e
Good guess!
Word: g e e _ _ _ _ _ _ e e _ g _
_________
Enter a letter: f
Good guess!
Word: g e e _ _ f _ _ _ e e _ g _
_________
Enter a letter: o
Good guess!
Word: g e e _ _ f o _ _ e e _ g _
_________
Enter a letter: r
Good guess!
Word: g e e _ _ f o r g e e _ _ _
_________
Enter a letter: s
Good guess!
Word: g e e _ s f o r g e e _ s _
_________
Enter a letter: k
Good guess!
Word: g e e k s f o r g e e k s
Congratulations! You've guessed the word: geeksforgeeks
Output 2
Welcome to Hangman!
Hint: A large mammal with a trunk
Word: _ _ _ _ _ _ _ _
_________
Enter a letter: g
Sorry, the letter 'g' is not in the word.
Word: _ _ _ _ _ _ _ _
_________
| |
Enter a letter: r
Sorry, the letter 'r' is not in the word.
Word: _ _ _ _ _ _ _ _
_________
| |
| O
Enter a letter: a
Good guess!
Word: _ _ _ _ _ a _ _
_________
| |
| O
Enter a letter: i
Sorry, the letter 'i' is not in the word.
Word: _ _ _ _ _ a _ _
_________
| |
| O
| /|\
Enter a letter: f
Sorry, the letter 'f' is not in the word.
Word: _ _ _ _ _ a _ _
_________
| |
| O
| /|\
| / \
Enter a letter: v
Sorry, the letter 'v' is not in the word.
Word: _ _ _ _ _ a _ _
_________
| |
| O
| /|\
| / \
|
Enter a letter: x
Sorry, the letter 'x' is not in the word.
Sorry, you've run out of tries.
The word was: elephant