Next Cycle

Previous Cycle
Back to Exercise Overview

Cycle 5: Remove player who dropped out

When the player's hand is empty, the player is happy to abandon the game because he/she is not the loser. Because each player holds the whole game state information, each player can determine, if one of the player drops out. But the server has to know it also, because it must skip the dropped out player when giving the next turn to a player. The current player that moves the last card to his/her stock transmits this information to the server by the additional command in Command.java:

int DROP_OUT = 9;

Drop out may happen in two situations: Either when the current player gets a card and he can transfer all cards to the stock or when a waiting player delivers his last card to the current player.

To handle the first case, we add almost at the end of the CardTable's leftDoubleClicked() callback (just after he announces that all card are in the stock) a few lines to send the DROP_OUT information to the server and display the drop out state. We enclose the player id of the player, so that the server can extract this information.

public void leftDoubleClicked(Card card)
{
  ...
  if (hands[0].getNumberOfCards() == 0)
  {
    agent.sendCommand("", Command.DROP_OUT, myPlayerId);
    setStatusText("Dropped out");
  }
  ...
}

For the second situation, we add the corresponding lines at the end of the Command.TRANSFER_TO_PLAYER case in the Player's dataReceived() callback:

if (cardTable.getMyNumberOfCards() == 0)
{
  sendCommand("", Command.DROP_OUT, myPlayerId);
  cardTable.setStatusText("Dropped out");
}

It is your work to handle the drop out on the server side. First you must get and store the player id when a player drops out. To do this, include in Server.java an Integer ArrayList that contains the numbers of these player ids:

private ArrayList<Integer> droppedOutList = new ArrayList<Integer>();

and add the numbers in an additional case in the pipeRequest() callback. Using this information the server is now able to skip dropped out players when giving the next turn in the giveTurn() method. This is quite simple by testing if a currentPlayerIndex is part of the droppedOutList: droppedOutList.contains(currentPlayerId).

For testing player drop out, it is necessary to have at least 3 players in the game room. Therefore we change the number of players in SchwarzPeter.java to 3:

public static void main(String[] args)
{
  nbPlayers = debug ? 3
    : requestNumber("Enter number of players (2..4):");
  ...
}

Execute the solution you should obtain.

Next Cycle
Previous Cycle
Back to Exercise Overview