C++ Program to Build a Hangman Word Guessing Game on Command Line

In this article, We are going to talk about “C++ Program to Build a Hangman Word Guessing Game on Command Line”. Below, We can see the possibilities to perform this task. Let’s move.

C++ Program to Build a Hangman Word Guessing Game on Command Line

C++ Program to Build a Hangman Word Guessing Game on Command Line

Program Code

#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

const int MAXLENGTH=80;
const int MAX_TRIES=5;
const int MAXROW=7;

int letterFill (char, char[], char[]);
void initUnknown (char[], char[]);

int main ()
{
	char unknown [MAXLENGTH];
	char letter;
	int num_of_wrong_guesses=0;
	char word[MAXLENGTH];
	char words[][MAXLENGTH] =
	{
		“Afghanistan”,
“Albania”,
“Algeria”,
“America”,
“Andorra”,
“Angola”,
“Antigua”,
“Argentina”,
“Armenia”,
“Australia”,
“Austria”,
“Azerbaijan”,
“Bahamas”,
“Bahrain”,
“Bangladesh”,
“Barbados”,
“Belarus”,
“Belgium”,
“Belize”,
“Benin”,
“Bhutan”,
“Bissau”,
“Bolivia”,
“Bosnia”,
“Botswana”,
“Brazil”,
“British”,
“Brunei”,
“Bulgaria”,
“Burkina”,
“Burma”,
“Burundi”,
“Cambodia”,
“Cameroon”,
“Canada”,
“Cape Verde”,
“Central African Republic”,
“Chad”,
“Chile”,
“China”,
“Colombia”,
“Comoros”,
“Congo”,
“Costa Rica”,
“Croatia”,
“Cuba”,
“Cyprus”,
“Czech”,
“Denmark”,
“Djibouti”,
“Dominica”,
“East Timor”,
“Ecuador”,
“Egypt”,
“El Salvador”,
“Emirates”,
“England”,
“Eritrea”,
“Estonia”,
“Ethiopia”,
“Fiji”,
“Finland”,
“France”,
“Gabon”,
“Gambia”,
“Georgia”,
“Germany”,
“Ghana”,
“Great Britain”,
“Greece”,
“Grenada”,
“Grenadines”,
“Guatemala”,
“Guinea”,
“Guyana”,
“Haiti”,
“Herzegovina”,
“Honduras”,
“Hungary”,
“Iceland”,
“India”,
“Indonesia”,
“Iran”,
“Iraq”,
“Ireland”,
“Israel”,
“Italy”,
“Ivory Coast”,
“Jamaica”,
“Japan”,
“Jordan”,
“Kazakhstan”,
“Kenya”,
“Kiribati”,
“Korea”,
“Kosovo”,
“Kuwait”,
“Kyrgyzstan”,
“Laos”,
“Latvia”,
“Lebanon”,
“Lesotho”,
“Liberia”,
“Libya”,
“Liechtenstein”,
“Lithuania”,
“Luxembourg”,
“Macedonia”,
“Madagascar”,
“Malawi”,
“Malaysia”,
“Maldives”,
“Mali”,
“Malta”,
“Marshall”,
“Mauritania”,
“Mauritius”,
“Mexico”,
“Micronesia”,
“Moldova”,
“Monaco”,
“Mongolia”,
“Montenegro”,
“Morocco”,
“Mozambique”,
“Myanmar”,
“Namibia”,
“Nauru”,
“Nepal”,
“Netherlands”,
“New Zealand”,
“Nicaragua”,
“Niger”,
“Nigeria”,
“Norway”,
“Oman”,
“Pakistan”,
“Palau”,
“Panama”,
“Papua”,
“Paraguay”,
“Peru”,
“Philippines”,
“Poland”,
“Portugal”,
“Qatar”,
“Romania”,
“Russia”,
“Rwanda”,
“Samoa”,
“San Marino”,
“Sao Tome”,
“Saudi Arabia”,
“Scotland”,
“Senegal”,
“Serbia”,
“Seychelles”,
“Sierra Leone”,
“Singapore”,
“Slovakia”,
“Slovenia”,
“Solomon”,
“Somalia”,
“South Africa”,
“South Sudan”,
“Spain”,
“Sri Lanka”,
“St. Kitts”,
“St. Lucia”,
“Sudan”,
“Suriname”,
“Swaziland”,
“Sweden”,
“Switzerland”,
“Syria”,
“Taiwan”,
“Tajikistan”,
“Tanzania”,
“Thailand”,
“Tobago”,
“Togo”,
“Tonga”,
“Trinidad”,
“Tunisia”,
“Turkey”,
“Turkmenistan”,
“Tuvalu”,
“Uganda”,
“Ukraine”,
“United Kingdom”,
“United States”,
“Uruguay”,
“USA”,
“Uzbekistan”,
“Vanuatu”,
“Vatican”,
“Venezuela”,
“Vietnam”,
“wales”,
“Yemen”,
“Zambia”,
“Zimbabwe”,
	};

	//choose and copy a word from array of words randomly
	randomize();
	int n=random(10);
	strcpy(word,words[n]);
	
    	// Initialize the secret word with the * character.
	initUnknown(word, unknown);

	// welcome the user
	cout << "nnWelcome to hangman...Guess a country Name";
	cout << "nnEach letter is represented by a star.";
	cout << "nnYou have to type only one letter in one try";
	cout << "nnYou have " << MAX_TRIES << " tries to try and guess the word.";
	cout << "n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

	// Loop until the guesses are used up
	while (num_of_wrong_guesses < MAX_TRIES)
	{
		cout << "nn" << unknown;
		cout << "nnGuess a letter: ";
		cin >> letter;
		// Fill secret word with letter if the guess is correct,
		// otherwise increment the number of wrong guesses.
		if (letterFill(letter, word, unknown)==0)
		{
			cout << endl << "Whoops! That letter isn't in there!" << endl;
			num_of_wrong_guesses++;
		}
		else
		{
			cout << endl << "You found a letter! Isn't that exciting!" << endl;
		}
		// Tell user how many guesses has left.
		cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
		cout << " guesses left." << endl;
		// Check if they guessed the word.
		if (strcmp(word, unknown) == 0)
		{
			cout << word << endl;
			cout << "Yeah! You got it!";
			break;
		}
	}
	if(num_of_wrong_guesses == MAX_TRIES)
	{
		cout << "nSorry, you lose...you've been hanged." << endl;
		cout << "The word was : " << word << endl;
	}
	getch();
	return 0;
}

/* Take a one character guess and the secret word, and fill in the
 unfinished guessword. Returns number of characters matched.
 Also, returns zero if the character is already guessed. */

int letterFill (char guess, char secretword[], char guessword[])
{
	int i;
	int matches=0;
	for (i = 0; secretword[i]!=''; i++)
	{
		// Did we already match this letter in a previous guess?
		if (guess == guessword[i])
			return 0;
		// Is the guess in the secret word?
		if (guess == secretword[i])
		{
			guessword[i] = guess;
			matches++;
		}
	}
	return matches;
}


// Initialize the unknown word

void initUnknown (char word[], char unknown[])
{
	int i;
	int length = strlen(word);
	for (i = 0; i < length; i++)
		unknown[i]='*';
	unknown[i]='';
}


// Project ends here

Conclusion

I hope this article helps you to know about “C++ Program to Build a Hangman Word Guessing Game on Command Line“. If you face any issues please let me know via the comment section. Share this article with other C/C++ developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment