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 ran across a problem last night and was wondering what is the "correct" way
to solve it. Just to test out my knowledge of Java I am writing a program to
play a simple game.
I have the game object and it has an ArrayList of the players in the game.
When it is a player's turn, the game object calls a method to allow the current
player to take a their turn.
The issue is the things a player can do on their turn is not fixed so there is
a Finished button they click when done. Now at this point I want to return
control back to the game object, but ActionListener returns void so while
I can do some cleanup work for the player instance, I can't get back to
the game object.
I could make the game object static since there is only one game at a
time and thus call Game.playerDone(), but that seems like a major
I could give a reference to the game instance when creating a player
instance and store that reference in an instance variable.
new player(this). Is that considered "correct" OO programming?
Or does this just point out a flaw in my OO design?
Please use a thread title which tells us what the thread is about.
Please supply more information; it is difficult to be sure from what you have told us. Making members of a class static because of difficulties sounds a very ugly hack to me. You might have a problem with the ActionListener on the Player class rather than on the Game class. Or you might find it correct to pass a this reference to the Game object constructor, as you suggested.
I think this question is too difficult for us beginners, so I shall move it.
Joined: Aug 10, 2006
Thanks for the move, wasn't sure where to put it.
Ok, I will try to give more detail without drowning everyone in the details of the code
I am coding up Uno, my boys favorite card game. The class Uno handles the game
state - determining who is the current player, if the card played by a player is valid,
etc... and has an arrayList of Player objects, one object per player playing the game.
The player class handles all the information about a player including name, hand of
cards, playing a card, drawing a card, etc...
When it is a player's turn, the Uno object will activate that player's JComboBox allowing them to scroll through the cards in their hand and click on one to play.
Now the ActionListener method in the player class gets called because a card
has been clicked on and I cast the result of getSelectedItem to Card. I now have
the card object that the player wants to play.
However I now need to called the method validateCardPlay in the Uno class which
I can't do since ActionListener returns void and I have no reference to the Uno
So is storing a reference to the Uno object in the Player class considered good
Or is this an indication that perhaps the player Class should determine if the
card picked is valid?