This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm working on a Computing project that involves creating a GUI-based Hangman game. I've got most of it done, but it seems that I made a couple of logical errors on the way, and I can't seem to locate them. I was hoping that I could get some help as to finding these errors, using your impressive java skills.
Here is the code of all the classes: I'm using a BlueJ IDE
MainFrame class (main GUI):
UseMainFrame class(basically creates an instance of the MainFrame class and sets its properties)
MenuGUI class (the menu GUI that basically offers a choice of playing or exiting)
UseMenuGUI class (basically creates an instance of MenuGUI and sets its properties)
and the GameEngine class(contains many methods that will be used throughout the game)
It basically works by running UseMenuGUI and the rest follows.
What does or does not work according to your expectations? In other words, what does it do now, and how does that deviate from what you expect it to do?
There's at least one empty catch block, which is generally not a good idea. At least write something to the console so you'll know a problem occurred.
By the way, you can make the code a lot shorter if you keep the JButtons in an array, instead of in separate variables.
Joined: Mar 03, 2011
Ulf Dittmer wrote:What does or does not work according to your expectations? In other words, what does it do now, and how does that deviate from what you expect it to do?
--> The area of the word to be guessed is not being updated every time a button is pressed and a guess is right
--> The area of the wrong letters is not being updated every time a button is pressed and the guess is wrong
-->I got mixed up on the for loops, and it seems to be outputting the message more than once and updating the hangman model more than once per button click
--> the hangman model resets per button click
That's not object-oriented programming. You should be adding information to the buttons, then using the button to supply the information. None of this if e.getSource() == aButton . . . else if e.getSource() == bButton . . .
And you should have an array of HangManPart objects; for every wrong letter you move one along the array, and if you get to the size of the array (ie one place beyond its end), the game is lost.
Joined: Mar 03, 2011
Oh wow, I really messed it up...how can I implement such procedures? Do I need to pass the char letter to the buttons as parameters?
Regarding the array, say I'm using 6 guesses, should i declare having 6 spaces and say that if it reaches arr.length()-1, the game is lost?
Just making sure I'm taking it in the right approach. How would I attach the array to the GUI? Should I do it in the same way, using a StringBuffer?