Hangman Game in C++
Posted by Samath
Last Updated: January 17, 2024

Hangman is a classic word guessing game for one or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers. A typical hangman would require time and effort to design and implement. Due to the complexity of such a project, I aim to demonstrate a simplified version of the program. I will be using the C++ programming language to demonstrate the project in the command prompt. In this project the user will be ask to guess the month of the year, the user will answer this by entering a letter they think is in the word. The program will display a message when the user correctly or incorrectly guesses a letter and display all previously guessed letters so the user doesn't guess them again. The user have three (3) tries to incorrectly guess the word, whenever the user run out of tries the game will end otherwise if they get it correct they will be congratulated.

As I said before this is a simplified hangman game so, no hangman is drawn.

Code:

#include <iostream>
#include <cstdlib>
#include<ctime>
#include <string>
using namespace std;

int NUM_TRY=3;
int checkGuess (char, string, string&);
void main_menu();
string message = "Play!";


int main(int argc, char *argv[])
{
	string name;
	char letter;
	string month;
	

    string months[] =
	{
		"january",
		"february",
		"march",
		"april",
		"may",
		"june",
		"july",
		"august",
		"september",
		"october",
		"november",
		"december"
	};
	
	srand(time(NULL));
	int n=rand()% 12;
	month=months[n];
    
	
	string hide_m(month.length(),'X');

	
    
	
	while (NUM_TRY!=0)
	{
		main_menu();
		cout << "\n\n\t\t\t\t" << hide_m;
		cout << "\n\n\t\t\t\tGuess a letter: ";
		cin >> letter;
		
		if (checkGuess(letter, month, hide_m)==0)
		{
			message = "Incorrect letter.";
			NUM_TRY = NUM_TRY - 1;
		}
		else
		{
			message = "NICE! You guess a letter";
		}


		
		if (month==hide_m)
		{
			message = "Congratulations! You got it!";
			main_menu();
			cout << "\n\t\t\t\tThe month is : " << month << endl;
			break;
		}
	}
	if(NUM_TRY == 0)
	{
		message = "NOOOOOOO!...you've been hanged.";
		main_menu();
		cout << "\n\t\t\t\tThe month was : " << month << endl;
	}
	cin.ignore();
	cin.get();
	return 0;
}


int checkGuess (char guess, string secretmonth, string &guessmonth)
{
	int i;
	int matches=0;
	int len=secretmonth.length();
	for (i = 0; i< len; i++)
	{
		
		if (guess == guessmonth[i])
			return 0;
		
		if (guess == secretmonth[i])
		{
			guessmonth[i] = guess;
			matches++;
		}
	}
	return matches;
}

void main_menu()
{
	 system("color 05");
                system("cls");
                 cout<<"\t\t\t\t*\t*";
 
  cout<<"\t\t\t\t**\t**";
   cout<<"\t\t\t\t***\t***";
   cout<<"\t\t\t\t****\t****";
   cout<<"\t\t\t\t*****\t*****";
    cout<<"\t\t\t\t******\t******";
   cout<<"\t\t\t\t*******\t*******";
   cout<<"\t\t\t\t*******\t*******";
   cout<<"\t\t\t\t******\t******";
   cout<<"\t\t\t\t*****\t*****";
   cout<<"\t\t\t\t****\t****";
   cout<<"\t\t\t\t***\t***";
   cout<<"\t\t\t\t**\t**";
   cout<<"\t\t\t\t*\t*";
 
 cout<<"\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
                cout<<"\n\t\t\t\tHangman Game!";
				cout << "\n\t\tYou have " << NUM_TRY << " tries to try and guess the month.";
				cout<<"\n\n\t\t\t\t"+message;
 cout<<"\n\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";

}

 

Related Content
Hangman Game using C#
Hangman Game using C#
Samath | Jan 17, 2024
The Matching Game in Java
The Matching Game in Java
Samath | Mar 20, 2015
Animal Game in Java
Animal Game in Java
gideonna | Apr 13, 2016
Number Guessing Game using C++
Number Guessing Game using C++
Samath | Jan 04, 2017
Blackjack Game in C++
Blackjack Game in C++
Samath | Jan 05, 2017
Checkers Game using C++
Checkers Game using C++
Samath | Jan 17, 2024
Minesweeper Game using C++
Minesweeper Game using C++
Samath | Jan 05, 2017
Craps Dice game in C++
Craps Dice game in C++
Samath | Jan 06, 2021
Paper Strip Game using Python
Paper Strip Game using Python
Samath | Jan 17, 2024
Tic Tac Toe Game using C#
Tic Tac Toe Game using C#
Samath | Jan 17, 2024
John Conway's Game of Life using C++
John Conway's Game of Life using C++
Samath | Jan 04, 2017
C++ Rock Paper Scissors Game
C++ Rock Paper Scissors Game
Samath | Jan 05, 2017
Tic Tac Toe Game using Python
Tic Tac Toe Game using Python
Samath | Jan 17, 2024
Casino Number Guessing Game in C++
Casino Number Guessing Game in C++
Samath | Jan 20, 2024