Need help with hangman game code

The computers in my college's computer lab have borland C++ and MS visual studio. At home I have a mac.


Here is the assignment:
For this assignment your are to write a program, using strings (from the string class) that plays
the popular game of Hangman. See the Wikipedia definition if
you have never played the game: http://en.wikipedia.org/wiki/Hangman_(game).

Here is how the program should work:
The word, phrase or sentence will be read in from a file called hangman.txt.
There will be only one word, phrase or sentence per line. The user guesses
will be done via the keyboard.
(1) The program should display the line by hiding all letters (a-z, A-Z)
with a '-' (dash). All other characters should be displayed normally.
(2) The program should ask the player to make a guess.
(3) If the player makes a correct guess then the program should
redisplay the word, phrase or sentence with the letter displayed and ask
the player to make another guess
(4) If the palyer makes an incorrect guess then the program should tell
the player the guess was incorrect, subtract a guess and tell the user how
many guesses remain.
(5) If no more guesses remain then the program should print a message stating
that the game is over and displaying the original word, phrase or sentence.
(6) If more guesses remain then the program should ask the player to guess
again.


We are not a homework service.

End of story.
Ah, hangman. What a good game.

Yeah, we don't do homework. However, what we will help you with is code - we love it over here. Try to get something done from it; we will help you if you are completely stuck on anything.
This is what I have so-far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream.h>
#include <string.h>
#include <fstream.h>

int main()
{
char name[20];
char q[1]; //The User's Guess


ifstream myfile;
myfile.open ("hangman.txt");

myfile >> name;

cout << name;


if ( q<=0)
{
cout<<"Game Over"<<endl;
cout<<"The word was "<<name<<endl;
}
else
{
cout<<"You have more guesses"<<endl;
}



return 0;
}


I am now stuck and confused how to do the rest. Like how to replace the letters in the word with dashes.

The word in the file is college.

Also how can I make this code work in orwell Dev-C++ portable?
Last edited on
If this is a homework I'd recommend you don't go asking for code before you get something else going. This is far from over, you barely even started. So, what does a hangman need? Well first of all needs words right...you got that one already. So what you need to do is pretty simple. You'll load the word into that char array, and then check whenever a letter is given until either the word is found or the hangman dies. This is just the concept of the game, but in it is pretty easy to see what you got to do.
While(not over)
{
ask for letter
check letter
if its present write it on screen
else add 1 more part to hangman
check if word was found
check for dead
}
@shywolf91

How complex do you want your Hangman game to be? Getting one word from a file and asking the user to guess what it is, isn't Hangman :P

I actually think it would be better if you created a .txt file with about 30 lines worth of names(Each word separated by a line break). Try making your words lengthy and varied. What I mean by this is that, don't have all your words made of 4 letters, and don't have words under one category like Food. That would be too simplistic and it would probably make the player think your game is a fail :P. So after you've created your file with all these nice random words, its time to start coding the Hangman Game!

Firstly, before you get started you should have some practice with basic input and output, functions, and most importantly, you should know what a file extension is, and how to read and write to a file through some code. Lets make a short little diagram of how we want our program to flow ,eh?

// Menu's are not required but I like having them anyway :D
Textual Menu -> Game -> Exit -> How To Play

Textual Menu has 3 Options For The User To Choose: Game(Start Game), Exit(Duh), How To Play(Instructions).

Game -> GameOver -> GameWin -> Textual Menu -> Play Again

When the user goes to the Game-Screen, they can Lose(GameOver), they can Win(Game Win), and they can retreat back to the Textual Menu, or they can Play Again if they want to.

How To Play -> Menu

How To Play is just some text on a screen, that explains to the user what Hangman is and how to play it. Nothing special here. The user can then return to the Menu when he/she is done reading the How-To-Play page.

I apologize if this sounds confusing to you D:

So now that you've sketched out some of your game-flow we can begin coding the game.

Some Functions You Will Need:

I would suggest you try to make some sort of DetermineWord function at the start of each new game session the user initiates. What this function will do is:

Read File. Select A Random Line Within The Max Number Of Lines Your File Has . Make Random Line = To Some String(eg. string guess).

After you've gotten that function written out, you then have to make some function that displays the '-' marks that indicate how many letter the word has(as well as how many letters the user has guessed correctly).

This function can be called DisplayTicks, and what this function will do is this:

Parameter Takes 1 String. Returns String Size. Make a for that displays a dash to the console window for every character the string has.

And your done with that function :D.

The rest is pretty straight-forward, let me know if you need help with the rest of your game.
Last edited on

EDIT:
Here is my near completed code. Can anyone please help me with the finishing touches?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//hangman
//Dylan Metz
#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <cstring.h>

int main()
{
int const SIZE=20;
//int const s=7;
//char other[s];
char name[20];
char dash[SIZE]={"                    "};
char q; //The User's Guess
int NumOfGuess=6; //Number of guesses
int flag;

//Open text file called hangman.txt
ifstream myfile;
myfile.open ("hangman.txt");

myfile>>name;

for (int index = 0; index<SIZE && name[index]!=' '; index++)
{
	if (toupper(name[index])>='A' && toupper(name[index])<='Z')
	{
	 dash[index]= '-' ;
	}
}

for (NumOfGuess=6; NumOfGuess>0; NumOfGuess--)
{
cout<<"The Word is ";
for(int x=0;x<strlen(name);x++)
{
cout<<dash[x];
}

cout<<" "<<endl;

//Have the User make a guess
cout<<"Please make a guess: "<<endl;
cin>>q; //The User's Guess
cout<<" "<<endl;

flag=0;

for (int a=0; a<strlen(name); a++)
{
	if (q==name[a])
	 {
	 cout<<"Your guess was correct"<<endl;
	 cout<<" "<<endl;
	 //name[a]=dash[SIZE];
	 name[a]=dash[index];
	 NumOfGuess++;
	 flag=1;
	 }

}
if (flag==0)
{
cout<<"Your guess was wrong."<<endl;
}
} //End of Guess counter



if(NumOfGuess>0)
{
cout<<"Game Over"<<endl;
cout<<"The word was "<<name<<endl;
}
else
{
cout<<"You Won"<<endl;
}


return 0;
}
Last edited on
Topic archived. No new replies allowed.