File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
Author

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

Wray Ferrell
Greenhorn

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
hack.

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
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
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
Greenhorn

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
object.

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uno: Returning control to the Uno object from a player object