File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Hangman game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hangman game" Watch "Hangman game" New topic

Hangman game

Nickesha Lindsay

Joined: Dec 01, 2003
Posts: 6
Okay, I wanted to work on a hangman game...

1.) First, the game has to have an array of words or phrases from which it picks one at random.
2.) It prints out rows of dashes which indicate each letter (if there is more than one word, there should be a space.)
The user must guess a letter one at a time. The program will then diplay the row of dashes with the letter showing if its is containted within the word. The user has 10 chances, after that, game over...If they are asked if they want to play again, and the program randomly selects another word.
The program must keep track of letters used already, so that the letters are not reused.
Its something like this:

Hang man: ---
Guess a letter: A
Letters used: A
Hangman: ---
Guess a letter: o
Letters used: A, O
Hangman: -o-
Guess a letter: Y
Letters used: A, O,Y
Guess a letter: t
Letters used: A,O,T,Y
Get the jist of it? Well...I know I have to declare some kind of array and it has to be in some loop, a while loop perhaps. And the 10 turns must be declared as an int variable, but after that I'm sketchy with how to replace the dashes with the correct letters used and how to keep track of the letters the user inputs.
What classes and methods are helpful? I'm in a beginner's programming class and this is an assignment. I'm pretty new at this Java stuff, so PULLLEEEASSEE can anyone give me a hand with some sort of code as to how to go about this program?
Any help will be greatly appreciated. Thank you very much...
Please respond as soon as possible...
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

"Noneyo Biznezz",
Sorry, but fictitious names are not allowed at the Ranch.
Please edit your profile so that your display name is valid. It will also help you get better responses.
Accounts with invalid display names get deleted without warning, but what may be worse in your case is that people won't take you seriously. Your display name must be two words: your first name, a space, then your last name, and fictitious names are not allowed.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Start breaking your problem down into steps and write code that looks like the steps. I think this captures the rules of the game:

Now that won't compile yet, but just comment it out for a sec. Can you imagine what might be in some of those other methods? A neat modern practice is to write tests before you even write code. What if the first thing you wrote in main() was:

See how you can verify the little methods are working before you try to put them together? Writing little bitty methods that work is a lot of fun. Writing a lot of them to get your program done is even more fun. Hope that helps!
BTW: If you really love this way of building programs, look at JUnit is a framework for writing tests like I showed above. It helps you get the tests out of your main() into a nice structure, automates running bunches of tests and displays the results nicely. It's not day-one beginner stuff, but it is a cool way of working.
[ December 02, 2003: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Hangman game
It's not a secret anymore!