If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

word guessing game applet help

My problem is:To write an applet to play the game "hangman", according to the following requirements.

The applet runs through the following two stages:

1)After the applet starts (and after restart, see below), a new word can be input, and the default maximum number of error can be changed. Then click "OK" to start the game.
After the game stars, the user can enter one letter at a time at the bottom row.
The "restart" button can be pressed at any time to restart a new game.

I have the layout of the applet set up how it needs to be and two of my buttons work correctly. If you run the code yourself, you will see what it looks like. The only button I can't figure out is the "Guess" button. It is supposed to take the character entered in the textfield next to it(guessField) and search the word in the text field that the original word was entered in and replace any letters that match the letter entered.
Here is my code so far:

Ultimately the problem is that you've overwritten the actualWord with that of the guessed word after the first guess action.
Within actionPerformed, you want to split it up logically into what its source is first. So branch for each statement available. You currently have a check for guess and restart, but not for initialize (the okay button). So all the work the okay button would be doing executes every time an event handle is passed to the actionPerformed. This has an assignment of actualWord = wordField.getText();, so after your first guess you then end up with the actualWord being that of the replaced first guess (so if my word is test and I guess 't', than I'd have actualWord being t**t). Group all that logic into the okay branch.
You'll need to fix up the incorrect guesses as well. Currently you check within the loop during char iteration, so if I guessed 't' on 'test', than I'd still end up with 2 incorrect guesses (the e and the s). You may want to implement case insensitive compare as well. The error count can be easily fixed by adding a boolean within the iteration loop. So long as it matches one result you have bMatches=true, and after the loop check for !bMatches and then increment the incorrect count.
Looks good otherwise. It's missing error handling, but I won't suggest adding it unless you have learned that. You'll also want to implement some type of functionality in the guess that allows you to indicate that the word is fully guessed and relay that to the user.

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

Users who have thanked Fou-Lu for this post:

Ultimately the problem is that you've overwritten the actualWord with that of the guessed word after the first guess action.
Within actionPerformed, you want to split it up logically into what its source is first. So branch for each statement available. You currently have a check for guess and restart, but not for initialize (the okay button). So all the work the okay button would be doing executes every time an event handle is passed to the actionPerformed. This has an assignment of actualWord = wordField.getText();, so after your first guess you then end up with the actualWord being that of the replaced first guess (so if my word is test and I guess 't', than I'd have actualWord being t**t). Group all that logic into the okay branch.

Does that mean that if the code is not within one of my sources( if(source==okay)) that anytime any source is used, all of the code that is not in any source brackets will also be executed?

That's correct. Since it doesn't have a particular branch to go down, that is executed all the time and then it determines which branch to go down (guess or restart).
You can likely group both the restart and okay buttons together though, the functionality sounds like it'd be the same.

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

Users who have thanked Fou-Lu for this post:

That's correct. Since it doesn't have a particular branch to go down, that is executed all the time and then it determines which branch to go down (guess or restart).
You can likely group both the restart and okay buttons together though, the functionality sounds like it'd be the same.

Thank you so much for your help so far!

I improved my code a bit now and it almost works. The problem now is if the word is "test" and you guess 't' it comes up "t**t" but then if you guess s after that, it comes up "**s*". I know the issue is with the for loop resetting the the array every time the guess button is pushed but I can't think of what to do so that isn't done every time the button is pressed. You've been a big help so far, any other help will be greatly appreciated.
Here is my updated code(just the actionPerformed section):

Okay, so look at the loop within the guess branch. If you mach the char, you show the char, otherwise you replace it with *. This isn't what you want to do; you've already created the *'s within the okay source branch, so you've already got the string you need. All you need to do is go through the actualWord char[], and check to see if this current character is found. If it is, replace the same offset of the wordField's text with the replaced value from actual word.
Do the actual write back after the loop, not within it. So, create a new char array from the wordField, foreach of the chars within the actual word check to see if char = entered char, and if so replace the new char's value with the same char from the actualWord in the corresponding offset. Then when you've checked all chars, write the new char array back into the wordField. I suggest taking from the actualWord instead of the guessed char since if you want to make an case insensitive replacement, it's simply a matter of altering the comparison.
Make sense?

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

All you need to do is go through the actualWord char[], and check to see if this current character is found. If it is, replace the same offset of the wordField's text with the replaced value from actual word.
Do the actual write back after the loop, not within it. So, create a new char array from the wordField, foreach of the chars within the actual word check to see if char = entered char, and if so replace the new char's value with the same char from the actualWord in the corresponding offset. Then when you've checked all chars, write the new char array back into the wordField.
Make sense?

I kinda understand what you were saying but I'm still a bit confused. I'm sorry for being so difficult but I have edited my code further and retried but I'm still having the same issue. I created a new array and didnt define any elements. Now I added an if/else statement so if the guessed char matches a char in the actual word, it replaces that char in the new array. Also, since not every character in the array was defined, I have it look for empty elements and fill them with *'s. I also moved the write back to after the loop.

This is what I have now(just the guess section since I didnt edit anything else:

You're still doing a bit too much work. You're letting the guess decide if it should flip each character, when this operation has already been done with the initialization of the word. All you need to do is flip it if it matches:

Like that. If you want to insensitively compare, you can do so by using the static Character class methods .toUpperCase or .toLowerCase on each of the chars during comparison. That way 'T' and 't' will both match 'T' and 't'.
After this, you just need to deal with the win / lose functionality based on the error counter.

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)