views:

89

answers:

2

Hi,

I have a poker framework for which I am trying to develop a "player" for. Basically I am implementing an object that implements a Player interface defined by the framework. I am trying to put a GUI on top of this player, the way that game play works is that the Dealer invokes the act() method on my player and expects a return type of Action. The problem I have is that once the act() method is invokes, I update the GUI (written using Swing) to display the options available, however I now need the method NOT to return until the player has chosen an option. The options are displayed as JButtons, which when clicked are handled by an actionListener object. How can I make the act() method not return until the user has acted? I need the thread to sleep/wait until it is woken up by the event being triggered, am unsure of the syntax and best way to do this. Any ideas?

Thanks,

Aly

+2  A: 

I think the approach is flawed. The Act method should not wait. Instead it should register for an event (lets call it the Acted event) on the Player instance. At the same time it should start a timer, of say 20 seconds, and if the Acted event is not raised before the timer runs out, the dealer should make the player fold (or check, depending on the situation) automatically and do the same for the next player in line.

That's just off the top of my head, but think about it.

klausbyskov
Using this method the Dealer will need a way to alert the player it is their turn to act, if this method doesnt return then players will then need a reference to the Dealer so that they can send the action performed to the Dealer. This would then create cyclic dependencies between our objects
Aly
+1  A: 

If I undestood your ploblem, you need to use an ActionListener for this.

The ActionListener is an interface implemented in the class you want to be warned when an event occurs. When an specific event is triggered in other part of your code, this class is warned by an abstract method of the Action Listener interface.

It isn't easy to show you with a short answer, but I got an hello world example that can help you.

http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html

HIH

marionmaiden