This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Game Development and the fly likes Uno: Returning control to the Uno object from a player object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Uno: Returning control to the Uno object from a player object" Watch "Uno: Returning control to the Uno object from a player object" New topic

Uno: Returning control to the Uno object from a player object

Wray Ferrell

Joined: Aug 10, 2006
Posts: 8
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?

Thanks, Wray
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44048
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.
Wray Ferrell

Joined: Aug 10, 2006
Posts: 8
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
OO design?

Or is this an indication that perhaps the player Class should determine if the
card picked is valid?
Have you checked out Aspose?
subject: Uno: Returning control to the Uno object from a player object
It's not a secret anymore!