Hang Man

shouvik 0 Tallied Votes 547 Views Share

Hi people.
well after I started learning C++ I thought why not try out my skills in some programming so I made my first programme. It is a very basic game commonly known as "HANGMAN" to many. It still might have some errors but it works. Hope you can modify it and also add new words and increase it vocab............

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
static void playGame();
static void printMistakes(int n);
static int const MAXGUESS=8;
static int const MAXLETTER= 'z'-'a'+1;
static char String[][15]= {"apple", "banana", "cat", "dog", "elephant", "fish", "giraffe", "hippopotamus", "ice cream","jelly", "kangaroo", "lion", "monkey", "noodles", "octopus", "parrot", "queen", "rabbit", "sun", "tree", "umbrella", "van", "window", "x-ray", "yellow", "zebra"};
enum boolean {false, true};
void main()
{
boolean again;
char answer;
clrscr();
cout<<"\n\n\tThis is the word guessing game called Hangman.";
cout<<"\n\tThe computer will pick-up a word and you must try to";
cout<<"\n\tguess the lettrs in the word. If you guess the";
cout<<"\n\tword with less than"<<MAXGUESS<<"mistake, then you win the game!!!";
do
{
playGame();
cout<<"\n\nWould you like to play another game?(Y or N)";
cin>>answer;
if (answer == 'y'|| answer == 'Y')
again=true;
else if (answer == 'n'|| answer == 'N')
again=false;
else
{
again=false;
cout<<"\n Your answer\""<<answer<<"\"should be Y or N.";
}
}
while (again);
cout<<"\nThank you for playing Hangman!";
}
static void playGame()
{
boolean*already;
char c;
boolean*display;
int found;
int i;
char inputChar;
char inputString[15];
int length;
int letters;
int mistakes,score,chanscor;
boolean playing;
char word[15];
mistakes=0;
score=0;
randomize();
strcpy(word,String[(int)(rand()%26)]);
length= strlen(word);
display= new boolean[length];
letters=0;
for (i=0; i<length; i++ )
{
c=word[i];
if ((c>='a')&&(c<='z'))
{
display[i]=false;
letters++;
}
else
display[i]=true;
}
cout<<"\n\nI am thinking of a word. ";
if(letters==1)
cout<<"the word has one letter.";
else
cout<<"the word has"<<letters<<"letters.";
already= new boolean[MAXLETTER];
for (i=0;i<MAXLETTER;i++)
already[i]=false;

playing = true;
while(playing)
{ chanscor=100/(MAXGUESS-mistakes);
cout<<"\nThe word is:";
for (i=0; i<length; i++)
{
cout<<" ";
if (display[i])
cout<<word[i];
else
cout<<"*";
}
cout<<"\nWhat is your guess? ";
if ((MAXGUESS-mistakes) <= 1 )
cout<<"this is your last guess?";
else
cout<<"you have"<<(MAXGUESS-mistakes)<<"guessing remaining.";
gets(inputString);
strlwr(inputString);
if (strlen(inputString)== length)
{
if(strcmp(word,inputString)==0)
{
cout<<"Yes the word is\""<<word<<"\"!";
printMistakes(mistakes);
playing=false;
}
else
{
cout<<"Sorry, the word is not\""<<inputString
<<"\".";
mistakes++;
}
}
else if (strlen(inputString)!=1)
{
cout<<"Your guess\""<<inputString<<"\"must be one letter or the entire word.";
}
else
{
inputChar=inputString[0];
if((inputChar<'a')||(inputChar>'z'))
{
cout<<"Your guess\""<<inputChar<<"\"must be a letter from A to Z, or the entire word.";
}
else if (already[inputChar-'a'])
{
cout<<"You have already guessed the following letters:";
for(i=0; i<MAXLETTER; i++)

cout<<" "<<(char)(i+'a');
cout<<"\n";
}
else
{
already[inputChar-'a']= true;
found=0;
for (i=0; i<length; i++)
if (word[i]==inputChar)
{ display[i]= true;
found++;
}
if (found <= 0)
{ cout<<"Sorry, the letter\""<<inputChar<<"\"is not in the word.";
mistakes++;
}
else
{
score=score+chanscor*(MAXGUESS-mistakes);
cout<<"\n\t Your score now is"<<score<<endl;
if (found==1)
cout<<"there is one letter\""<<inputChar
<<"\"in the word.";
else
cout<<"yes there are"<<found<<"letters\""<<inputChar<<"\"in the word.";
playing= false;
for(i=0; i< length; i++)
playing=playing || !display[i];
if (!playing)
{
cout<<"You found all the letters of the word\""<<word<<"\".";
printMistakes(mistakes);
}
}
}
}
if (mistakes>= MAXGUESS)
{
cout<<"the word was\"" << word << "\".";
playing= false;
}
}
}
static void printMistakes(int n)
{
if (n<= 0)
cout<<"You did not make any mistake. Excellent!";
else if (n==1)
cout<<"you made only one mistake. Great!";
else
cout<<"you made only"<<n<<"mistakes. Better luck next time!!!";
getch();
}
marceta 0 Posting Whiz in Training

seems to have a few errors. Im using MS Visual C++ Enterprise v6.0 :s

--------------------Configuration: Hangman - Win32 Debug--------------------
Compiling...
hangmanmain.cpp
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(11) : error C2059: syntax error : 'constant'
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(11) : error C2143: syntax error : missing ';' before '}'
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(11) : error C2143: syntax error : missing ';' before '}'
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(16) : error C2065: 'clrscr' : undeclared identifier
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(27) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(29) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(32) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(55) : error C2065: 'randomize' : undeclared identifier
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(65) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(69) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(78) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(80) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(99) : warning C4018: '==' : signed/unsigned mismatch
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(105) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(135) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(139) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(155) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(157) : error C2440: '=' : cannot convert from 'bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\peter\my documents\c++\hangman\hangman\hangmanmain.cpp(169) : error C2440: '=' : cannot convert from 'const bool' to 'enum boolean'
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
Error executing cl.exe.

hangmanmain.obj - 18 error(s), 1 warning(s)

deleti0n 0 Newbie Poster

Free of errors: just make sure your returns are proper. Usually you can fix those by backing the line up to the previous one then hitting the enter key..

#include "stdafx.h"

#include <iostream>
#include <iomanip>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::string;
using namespace std;

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

static void playGame();
static void printMistakes( int n );
static int const MAXGUESS = 8;
static int const MAXLETTER = 'z' - 'a' +1;
static char String[][15]= {"apple", "banana", "cat", "dog", "elephant", "fish", "giraffe", "hippopotamus", "ice cream","jelly", "kangaroo", "lion", "monkey", "noodles", "octopus", "parrot", "queen", "rabbit", "sun", "tree", "umbrella", "van", "window", "x-ray", "yellow", "zebra"};

//{
//	enum{false, true} bool;
//}

void main()
{
	bool again;
	char answer;
	//clrscr();
	cout << "\n\n\tThis is the word guessing game called Hangman.";
	cout << "\n\tThe computer will pick-up a word and you must try to";
	cout << "\n\tguess the lettrs in the word. If you guess the";
	cout << "\n\tword with less than" << MAXGUESS << "mistake, then you win the game!!!";
	
	do
	{
		playGame();
		cout << "\n\nWould you like to play another game?(Y or N)";
		cin >> answer;
		
		if ( answer == 'y' || answer == 'Y' )
			again = true;
		else if ( answer == 'n' || answer == 'N' )
			again = false;
		else
		{
			again = false;
			cout << "\n Your answer\"" << answer << "\"should be Y or N.";
		}
	}
	while (again);
	cout << "\nThank you for playing Hangman!";
}

static void playGame()
{
	bool * already;
	char c;
	bool * display;
	
	int found;
	int i;
	char inputChar;
	char inputString[ 15 ];
	int length;
	int letters;
	int mistakes, score, chanscor;
	bool playing;
	
	char word[ 15 ];
	mistakes = 0;
	score = 0;
	//randomize();
	
	strcpy( word, String[ (int)(rand()%26) ] );
	length = strlen( word );
	display = new bool[ length ];
	letters = 0;
	
	for (i=0; i<length; i++ )
	{
		c=word[i];
		if ((c>='a')&&(c<='z'))
		{
			display[i]=false;
			letters++;
		}
		else
			display[i]=true;
	}
	
	cout<<"\n\nI am thinking of a word. ";
	
	if(letters==1)
		cout<<"the word has one letter.";
	else
		cout<<"the word has"<<letters<<"letters.";
	already= new bool[MAXLETTER];
	
	for ( i = 0; i < MAXLETTER; i++ )
		already[i] = false;
	playing = true;
	
	while(playing)
	{
		chanscor = 100 / ( MAXGUESS - mistakes );
		cout << "\nThe word is:";
		
		for ( i = 0; i < length; i++ )
		{
			cout << " ";
			
			if ( display[i] )
				cout << word[i];
			else
				cout << "*";
		}
		
		cout << "\nWhat is your guess? ";
		
		if (( MAXGUESS-mistakes ) - 1 )
			cout << "this is your last guess?";
		else
			cout << "you have" << ( MAXGUESS - mistakes ) << "guessing remaining.";
		gets( inputString );
		strlwr( inputString );
		
		if ( strlen(inputString) == length)
		{
			if( strcmp(word, inputString) == 0 )
			{
				cout<<"Yes the word is\""<<word<<"\"!";
				printMistakes(mistakes);
				playing=false;
			}
			else
			{
				cout<<"Sorry, the word is not\""<<inputString
					<<"\".";
				mistakes++;
			}
		}
		else if (strlen(inputString)!=1)
		{
			cout<<"Your guess\""<<inputString<<"\"must be one letter or the entire word.";
		}
		else
		{
			inputChar=inputString[0];
			if((inputChar<'a')||(inputChar>'z'))
			{
				cout<<"Your guess\""<<inputChar<<"\"must be a letter from A to Z, or the entire word.";
			}
			else if (already[inputChar-'a'])
			{
				cout<<"You have already guessed the following letters:";
				
				for(i=0; i<MAXLETTER; i++)
					cout<<" "<<(char)(i+'a');
				cout<<"\n";
			}
			else
			{
				already[inputChar-'a']= true;
				found=0;
				
				for (i=0; i<length; i++)
					
					if (word[i]==inputChar)
					{
						display[i]= true;
						found++;
					}
					
					if (found <= 0)
					{
						cout<<"Sorry, the letter\""<<inputChar<<"\"is not in the word.";
						mistakes++;
					}
					else
					{
						score=score+chanscor*(MAXGUESS-mistakes);
						cout<<"\n\t Your score now is"<<score<<endl;
						
						if (found==1)
							cout<<"there is one letter\""<<inputChar
							<<"\"in the word.";
						else
							cout<<"yes there are"<<found<<"letters\""<<inputChar<<"\"in the word.";
						playing= false;
						for(i=0; i< length; i++)
							playing = playing || !display[i];
						if (!playing)
						{
							cout<<"You found all the letters of the word\""<<word<<"\".";
							printMistakes(mistakes);
						}
					}
			}
		}
		
		if (mistakes>= MAXGUESS)
		{
			cout<<"the word was\"" << word << "\".";
			playing = false;
		}
}
}

static void printMistakes(int n)
{
	if (n<= 0)
		cout<<"You did not make any mistake. Excellent!";
	else if (n==1)
		cout<<"you made only one mistake. Great!";
	else
		cout<<"you made only"<<n<<"mistakes. Better luck next time!!!";
	getch();
}
ashamnz 0 Newbie Poster

could you convert it to c

raptr_dflo 48 Posting Pro

Yes, but the question is "can -you- convert it to C?" ;)

Please don't post into old threads. The corrected code was posted well over a year ago. Instead, start a new thread, including your specific questions and your own code. You can provide a link to the old thread if that will help readers answer your question.

Also, DaniWeb is not a "do it for you" environment. We're here to help you become a better programmer, but you have to do the work. There's a C forum here, if that's the most appropriate place for your questions....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.