package edu.villanova.studs.poker.transport;
import edu.villanova.studs.poker.exceptions.PokerMessages;
import edu.villanova.studs.poker.exceptions.TransportException;
import java.io.Serializable;
import java.util.ArrayList;
/**
* A concrete implementation of the TableDataInferface and
* abstract superclass TableDataImpl.
* Represents a poker games that use community cards:
* Specifically Texas or Omaha styles.
* @unit TableDataCommunity.java
* @package edu.villanova.studs.poker.transport
* @author Mike Bellfy
* @date 4 Nov 2008
*/
public class TableDataCommunity extends TableDataImpl implements Serializable {
/*------------------------------------------------------------------------*/
/*---------------------------PRIVATE CLASS MEMBERS------------------------*/
/*------------------------------------------------------------------------*/
//Constant for the maximum # of players allowed
private final int GAME_TYPE_HOLDEM = 2;
private final int GAME_TYPE_OMAHA = 4;
//The table of community cards
private Table gameTable;
//Int that indicates the type of game
private int gameType;
/*------------------------------------------------------------------------*/
/*----------------------------METHOD CONSTRUCTORS-------------------------*/
/*------------------------------------------------------------------------*/
/**
* This is the TableDataCommunity constructor. It creates new instances of
* the private class members and defaults the game to Texas Hold'em.
*/
public TableDataCommunity(){
gameTable = new Table();
gameType = GAME_TYPE_HOLDEM;
}
/**
* This is the TableDataCommunity constructor. It creates new instances of
* the private class members.
* @param type is the type of game to be setup
*/
public TableDataCommunity( int type ) {
gameTable = new Table();
//if param type is not valid, set to holdem game
if (type == GAME_TYPE_OMAHA)
gameType = type;
else
gameType = GAME_TYPE_HOLDEM;
}
/*------------------------------------------------------------------------*/
/*----------------------------PUBLIC CLASS METHODS------------------------*/
/*------------------------------------------------------------------------*/
/**
* This method is used to update the current Table of community cards with
* an entirely new one.
* @param tab is the new Table to assign
*/
public void setGameTable( Table tab ) {
gameTable = tab;
}
/**
* This method is used to set the game type to be played.
* @param type is the value for for the type of game
*/
public void setGameType( int type ) {
//if param type is not valid, set to holdem game
if (type == GAME_TYPE_OMAHA)
gameType = type;
else
gameType = GAME_TYPE_HOLDEM;
}
/**
* This method adds a player to the list of players.
* In order for a player to be added, the player's hand must
* contain a valid number of cards relative to the type of game
* being played. Also, the table cannot be full.
* @param p is the Player to be added to the table
* @throws TransportException if the new list size exceeds the max allowable,
* or if the player does not have the appropriate number of cards.
*/
public void addPlayer( Player p ) throws TransportException {
if ( verifyPlayer(p) ) {
if ( players.size() < MAX_PLAYERS )
players.add(p);
else
//Max exceeded
throw new TransportException( PokerMessages.PLAYER_SIZE,
new Object[] {MAX_PLAYERS} );
}
else
//Player has invalid number of cards in hand
throw new TransportException( PokerMessages.PLAYER_CARDS,
new Object[] { players.size(),
p.numOfCardsInHand(),
gameType} );
}
/**
* This method is used to update the current list of Players with an
* entirely new one. The size of the new player list should be verified
* prior to making this call. If the list is too big an exception is thrown.
* Also, each player must have the appropriate number of cards in thier hand
* relative to the type of game being played.
* @param plist is the new list of Players to assign
* @throws TransportException if the new list size exceeds the max allowable,
* or if a player does not have the appropriate number of cards.
*/
public void setPlayers( ArrayList<Player> plist ) throws TransportException {
if ( ( plist.size() + players.size() ) > MAX_PLAYERS ) {
//Max exceeded
throw new TransportException( PokerMessages.PLAYERS_SIZE,
new Object[] { players.size(),
plist.size(),
MAX_PLAYERS} );
}
// Loop through list and make sure each player is valid before adding
for ( int i=0; i < plist.size(); i++ ) {
Player p = plist.get(i);
if ( !verifyPlayer( p ) ) {
//Player has invalid number of cards in hand
throw new TransportException( PokerMessages.PLAYER_CARDS,
new Object[] { i,
p.numOfCardsInHand(),
gameType} );
}
}
//No exception thrown, new list is valid, over write old players
players = plist;
}
/**
* This method gets the Table containing the community cards
* @return the Table with the community Card list
*/
public Table getGameTable() {
return gameTable;
}
/**
* This method gets the game type indicator
* @return the int indicating the game type
*/
public int getGameType() {
return gameType;
}
/*------------------------------------------------------------------------*/
/*---------------------------PRIVATE CLASS METHODS------------------------*/
/*------------------------------------------------------------------------*/
/**
* This verifies that a player has the appropriate amount of cards in
* his/her hand for the type of poker game being played.
* @param p is the Player whose hand is to be analyzed
* @return
*/
private boolean verifyPlayer( Player p ) {
if ( p.isActive() )
//Active players need to have the correct # of cards for the game
return p.numOfCardsInHand() == gameType;
else
//inactive players are always considered valid - it is assumed they
//are used a player holders for hand calculations
return true;
}
}