C++ Hangman Game Project

 //**************************************************************************************************************//
//                     This code has been written by TutorialsArt.com
//          Distribution of the code is permitted if and only if you do not delete these lines.
//**************************************************************************************************************//
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

const int Max_length=80;
const int Maximum_tries=5;
const int max_rows=7;

int Letter_fill (char, char[], char[]);
void init_Unknown (char[], char[]);

int main ()
{
	char unknown_var [Max_length];
	char letter_var;
	int Nwrong_guess=0;
	char word_var[Max_length];
	char words_var[][Max_length] =
	{
		"America",
		"Australia",
		"Canada",
		"Germany",
		"Itally",
		"China",
		"Iran",
		"Malasia",
		"Pakistan",
		"Indonesia"
	};

	//choose and copy a word_var from array of words_var randomly
	randomize();
	int n=random(10);
	strcpy(word_var,words_var[n]);
	
    	// Initialize the secret word_var with the * character.
	init_Unknown(word_var, unknown_var);

	// welcome the user
	cout << "\n\nWelcome to hangman...Guess a country Name";
	cout << "\n\nEach letter_var is represented by a star.";
	cout << "\n\nYou have to type only one letter_var in one try";
	cout << "\n\nYou have " << Maximum_tries << " tries to try and guess the word_var.";
	cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

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

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

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


// Initialize the unknown_var word_var

void init_Unknown (char word_var[], char unknown_var[])
{
	int i;
	int length = strlen(word_var);
	for (i = 0; i < length; i++)
		unknown_var[i]='*';
	unknown_var[i]='\0';
}


// Project ends here



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

const int Maximum_tries=5;
int Letter_fill (char, string, string&);

int main ()
{
	string name;
	char letter_var;
	int Nwrong_guess=0;
	string word_var;
	string words_var[] =
	{
		"india",
		"pakistan",
		"nepal",
		"malaysia",
		"philippines",
		"australia",
		"iran",
		"ethiopia",
		"oman",
		"indonesia"
	};

	//choose and copy a word_var from array of words_var randomly
	srand(time(NULL));
	int n=rand()% 10;
	word_var=words_var[n];
    
	// Initialize the secret word_var with the * character.
	string unknown_var(word_var.length(),'*');

	// welcome the user
	cout << "\n\nWelcome to hangman...Guess a country Name";
	cout << "\n\nEach letter_var is represented by a star.";
	cout << "\n\nYou have to type only one letter_var in one try";
	cout << "\n\nYou have " << Maximum_tries << " tries to try and guess the word_var.";
	cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
    
	// Loop until the guesses are used up
	while (Nwrong_guess < Maximum_tries)
	{
		cout << "\n\n" << unknown_var;
		cout << "\n\nGuess a letter_var: ";
		cin >> letter_var;
		// Fill secret word_var with letter_var if the guess is correct,
		// otherwise increment the number of wrong guesses.
		if (Letter_fill(letter_var, word_var, unknown_var)==0)
		{
			cout << endl << "Whoops! That letter_var isn't in there!" << endl;
			Nwrong_guess++;
		}
		else
		{
			cout << endl << "You found a letter_var! Isn't that exciting!" << endl;
		}
		// Tell user how many guesses has left.
		cout << "You have " << Maximum_tries - Nwrong_guess;
		cout << " guesses left." << endl;
		// Check if user guessed the word_var.
		if (word_var==unknown_var)
		{
			cout << word_var << endl;
			cout << "Yeah! You got it!";
			break;
		}
	}
	if(Nwrong_guess == Maximum_tries)
	{
		cout << "\nSorry, you lose...you've been hanged." << endl;
		cout << "The word_var was : " << word_var << endl;
	}
	cin.ignore();
	cin.get();
	return 0;
}

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

int Letter_fill (char guess, string secretword, string &guessword)
{
	int i;
	int matches=0;
	int len=secretword.length();
	for (i = 0; i< len; i++)
	{
		// Did we already match this letter_var in a previous guess?
		if (guess == guessword[i])
			return 0;
		// Is the guess in the secret word_var?
		if (guess == secretword[i])
		{
			guessword[i] = guess;
			matches++;
		}
	}
	return matches;
}