Cycle 4: Manage user's turn One of the server's tasks is to supervise the correct order of the player's moves. In almost all card games only one of the players can make the next draw and the other players have to wait. It is very important that all user actions of the waiting players (except quitting the game) are disabled until the new game state is completely established. To handle this situation it is appropriate to introduce a state flag isMyTurn that indicates whether the player has the next turn or not. Every player gets the information from the server if he/she can make the next draw or if he/she has to wait. To exchange this information we add two more commands to the command list in Command.java: int MY_TURN = 7; In the class CardTable we define a boolean instance variable private boolean isMyTurn = false; and initialize it to false. Whenever the player makes his/her move, the callback leftDoubleClick() is invoked. To inhibit any further actions we immediately set the flag to false: public void leftDoubleClicked(Card card) In order to set the game table to the desired state we define two methods. We want also to improve the user interface by displaying state information in the status bar. protected void setMyTurn() It is the server's responsability to determine which player has the next move by sending a MY_TURN command to one of the players and a OTHER_TURN to the rest. Before doing so, all players have to be in a stable state. They inform the server about this by sending a READY_TO_PLAY command. The server counts these messages and waits for further actions until the count reaches the number of players. To do so, we modify the READY_TO_PLAY case in the pipeRequest() callback by using a integer instance variable nbReady: case Command.READY_TO_PLAY: The method giveTurn() will be called when all players are ready. private void giveTurn() The integer instance variable currentPlayerId indicates which player has the next move. It is run through circularly. At the program start currentPlayerId is initialized to nbPlayers - 1, so the next player will have playerId = 0 (the first player that entered the game room): private int currentPlayerId = nbPlayers - 1; Now to your job. Find out, where exactly each player needs to send the READY_TO_PLAY. Two hints that may help you:
Finally you may add some more status information, e.g. "Dealing out" during the deal out process. Execute the solution you should obtain. | ||