Java Help in making Hangman Game

Hi, Everyone.
So I am starting a big project for my online class, and I need a little bit of help.
I need to create a couple of things, and I don’t know how to create some of these, even if I look in my textbook

  1. Create a private instance array of String references named possibleWords. Assign to possibleWords a populated array of strings of your choosing. This will be the list of words the program will randomly select from. Include at least three strings with at least five letters each.
  2. Create a private instance variable of type String named word. This variable will remember the word from the possibleWords array that will be randomly selected later.
  3. Create a private instance array of char named progress. After the game’s word is randomly selected, an array of char will be created with a number of elements equal to the length of the word. It will originally be filled with dash characters, but later, correctly guessed letters will replace the dashes.
  4. Create a private instance int variable named wrongCount and set it to zero. This will keep track of the number of incorrect guesses the user makes.

How do I do this? I’ve never done this before, nor have I been taught on how to do it.

Yes, you have if you think it through.

Everything there is talking about declaring variables and assigning values. The only difference there is talking about private vs public. A private variable is only accessible within a class. A public variable can be accessed outside a class. You declare a variable as public or private by using those terms

private obj privObjName = null // private instance of type obj named privObjName
public obj pubObjName = null;  // public instance of type obj named pubObjName

So go through that list and look at the different data types. Some of them have initial values to be put in them. NOTE: These ARE going to be outside your main method, but within the class declaration, hence why you need to add the private/public to it.

2 Likes

Hello. So I’m gonna be asking something similar to the Password Creation topic, but I’m not. I want to have certain letters and spaces for my game here. I saw the for statement for certain letters, but, I can’t really do that because my conditions basically have changed. So what would be the easiest way to do it, then? If statements, for statements, or should I do something else? I also want to keep track of how many times the person has guessed, then show it back to them.

Yes? You’re going to need a combination to do something like that…

Try to think this through.

  • What is the purpose of hangman? To guess a word or phrase while only getting N number of guesses wrong
  • How does the program know when is the game over? When the number of guesses hits the limit OR the phrase is correct

So the basic functionality is this (NOTE: This is call pseudo code. It’s not intended to work. Just basic ideas you can make into the logic of choice)

// set the end limits....
FinalAnswer = "Stormtrooper"
MaxWrongAnswers = 7 // (Head. Body. two arms, two legs)

// initialize current status
CurrentAnswer = "------------"
WrongAnswers = 0

// program logic here
While CurrentAnswer not equal to FinalAnswer AND WrongAnswers < MaxWrongAnswers {
  CurrentLetter = GetLetterFromUser()
  if CurrentLetter is in FinalAnswer, replace appropriate dashes in CurrentAnswer (ex if picked t, change CurrentAnswer to "-t---t------")
  otherwise, add 1 to WrongAnswers
}

// how did the user do?
Show either success or failure depending on which part became true....
2 Likes
  1. Use the appropriate method from the Arrays class to fill all the elements of progress with the dash character (’–’).
    How do I do this? I’m actually kinda confused now. I tried searching online, but I couldn’t find it

You’re going to need to use code similar to the code from your password validation exercise. Basic concept is this:

  1. Before the first guess, load the progress array with an appropriate number of dashes. For example, if the word is apple, the progress array will have five elements in in, all with dashes in it. If the word is elephant, there will be eight elements to the array with dashes in it.
  2. After each guess, loop through the word variable character by character (use the password exercise to get the logic). Check the guessed letter against the appropriate character in the word string.
    1. If there is a match, replace the appropriate element in the progress array with the letter. So, if the word is apple and the guess is the letter l, then progress[3] will be replaced with l. If p is guessed, the progress[1] and progress[2] will be changed to p.

useful things to search for

  • replaceAll() - use this to copy the correct word replacing characters with -
  • toCharArray() - use this to convert the correct word to the character array
  • String.valueOf() - use this to compare guessed word to correct word to see if you’re done.
  • indexOf() - use this to quickly see if the guessed letter is in the correct word

… 1 + 1 + 2 + 2 =… how many heads do you have, Dave?
:wink:

but yeah, this is exactly what it is. Take your project, and basically… lay it out in English. (Or your choice of language, obv.)

Then start to break it down into what it needs to do to make that happen. You will come to points where you have to add things further up from the current point. This is normal and natural. (“Well it needs to know if the game’s over. To do that it’ll need to know how many bad guesses there have been, so i’ll need a counter for bad guesses…” goes back and adds a bad guesses counter). You will end up with, essentially, the rules for Hangman, laid out, in order.

Eventually, you get to the pseudocode level that Dave’s started laying out for you. At that point, you start to plug in code, and make it all work. Note that, to this point, we havent even mentioned Java. Because before this point, it hasnt mattered; coding it in Java, C#, C++, Javascript… all of it’s going to follow the same structure as your spelled out design. The code is really just the last 5%, a language for your logic.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.