Hello Daniweb,

I am working on a program, a basic poker game. I've run into a problem I can't seem to solve for the life of me, after hours of googling I thought I'd ask here.

I have a Dealer class that basically controls the back end of the came, and it calls various methods in pokerGUI to update the display when it computes the flop and computer player actions. However, when it is the human player's turn, It calls a method in the pokerGUI to display certain buttons depending on the available options (ie, if no one has bet it displays a check button and bet button or if someone has bet, call button/fold button/raise button). Then the player ideally would click a button, and the GUI would pass back the info (including a double if the player placed a bet). My problem is that when the Dealer calls the GUI via getHumanAction(), it expects a double in return. But GUI does not immediately have a double to return, since it must wait for the player to decide and click the appropriate action. Below is the code in Dealer.java

Code:

public double getPlayerBet(double currentBet) { return board.getHumanAction(currentBet); }

and the code in the GUI just initializes some buttons and graphics and adds appropriate action listeners, then finally returns the value of the players choice. The problem is again that getHumanAction doesn't immediately return a double. What I want to do is somehow put a pause in getHumanAction just before it returns the double, and then have it continue when a button is clicked (and the double has the users input). So basically getHumanAction would stop....then when the player clicks a button (for example, check), the ActionEvent would cause getHumanAction would continue, returning the info the player provided when it clicked the button. How do I pause the method without freezing my entire program?
I've tried:
-Wait/notify: Regardless of where I put these, as soon as it hits wait() the entire GUI blanks out because it freezes the EVT (I think?)
-sleep() also freezes the entire GUI
-latch freezes the GUI
all these thread manipulating methods I cannot seem to implement in a way that produces my desired result, they freeze the GUI or do nothing. I tried
putting the wait functions in their own separate threads, but I just can't seem to not freeze the GUI.


Thanks in advance for any help, I'm dying here.

Recommended Answers

All 3 Replies

Change your program so that instead of doing something like getHumanAction, you simply don't do anything until the player takes an action. In other words, if you have a listener that does the getHumanAction when the player clicks his button, then your game will work fine. For example, write an action listener for your buttons. Once you correctly write the action listener for your buttons, when the player clicks the button, the actionPerformed method is called, and you can then do something from within that method. So basically, instead of using getHumanAction to wait for a human action, you can wait for a human action simply by doing nothing until actionPerformed is called. Then, from within actionPerformed, you can do whatever is necessary to continue the game. (Like take the player's turn, then call whatever method takes the computer's turn).

If that didn't make sense, hopefully this small program (not written with absolutely correct code, I'm just demonstrating here) will help:

public static void main(String[] args){
//set up JFrame and JPanels or whatever else is on the GUI here
//Computer goes first (in my example anyway)
takeComputerTurn();

//Now add the buttons onto the GUI so the user can go
JButton button = new JButton("Click to take your turn!");
button.addActionListener(new ActionListener(){

actionPerformed(Event e){
takePlayerTurn(); //player just went since to get in this method, they had to click the button!
}

});
}

Hastily thrown together example but I hope that clarifies somewhat. If not, feel free to ask more questions.

Thanks for the input! we considered that but the structure of our program doesn't really allow it to be done that way. Dealer.java handles turns and controls thus GUI, thus it must tell the GUI to show certain buttons at a certain time (ie when the AI players are done returning actions) and then wait for the player before continuing. I think i figured it out though. I made the actual game function of the dealer a thread, and then made the human interaction with the GUI a different thread. With synchronize, wait(), and join(), I was able to have the dealer thread hold off any more processing until the GUI button was clicked. I guess before I was making the entire program wait(), and thus blanking the GUI

Thanks again!

No problem but just letting you know: your way sounds like it could easily lead to errors, and it is logically unnecessary. The way I suggested would allow you to do everything you mentioned, with no extra Threads. In fact, you'd barely have to change anything, since you presumably already have a method created which handles adding/removing all the buttons and such when the turn switches.

One more note; I think you can set JButtons to invisible with setVisible(false) but I'm not positive. But if so, doing so would be better than removing them from the UI completely.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.