soc.game
Class SOCInventory

java.lang.Object
  extended by soc.game.SOCInventory

public class SOCInventory
extends java.lang.Object

This represents a collection of development cards, and occasional scenario-specific items. Players can have 0, 1, or more of any card type or item type. Each item's current state can be New to be played soon; Playable; or Kept in hand until the end of the game (Victory Point cards, which are never New).

For use in loops, age constants and inventory-item state constant ranges are each contiguous:
OLD == 0, NEW == 1.
NEW == 1, PLAYABLE == 2, KEPT == 3.

Before v2.0.00, this class was named SOCDevCardSet.


Field Summary
private  java.util.List<SOCInventoryItem> kept
          Current set of the items having 1 of 3 possible states (New and not playable yet; Playable; Kept until end of game).
static int KEPT
          Item state constant: Kept in hand until end of game (not PLAYABLE, was never NEW).
static int NEW
          Age constant, item state constant: Recently bought card, playable next turn.
Other possible age is OLD.
Other possible states are PLAYABLE and KEPT.
private  java.util.List<SOCInventoryItem> news
          Current set of the items having 1 of 3 possible states (New and not playable yet; Playable; Kept until end of game).
static int OLD
          Age constant: An old item can either be played this turn (state PLAYABLE) or is kept in hand until the end of the game (state KEPT) such as a Victory Point card.
static int PLAYABLE
          Item state constant: Playable this turn (not NEW or KEPT).
private  java.util.List<SOCInventoryItem> playables
          Current set of the items having 1 of 3 possible states (New and not playable yet; Playable; Kept until end of game).
 
Constructor Summary
SOCInventory()
          Make an empty dev card and inventory item set.
SOCInventory(SOCInventory set)
          Make a copy of a dev card and inventory item set.
 
Method Summary
 void addDevCard(int amt, int age, int ctype)
          Add an amount to a type of dev card.
 void addItem(SOCInventoryItem item)
          Add a special item or dev card to this set.
 void clear()
          set the total number of dev cards to zero
 int getAmount(int ctype)
          Get the amount of a dev card type or special item in the set.
 int getAmount(int age, int ctype)
          Get the amount of a dev card type of certain age in the set.
 int getAmountByState(int state, int itype)
          Get the amount of dev cards or special items by state and type.
 java.util.List<SOCInventoryItem> getByState(int cState)
          Get the cards and items, if any, having this state.
 int getNumUnplayed()
          Some card types stay in your hand after being played.
 int getNumVPItems()
          Get the number of Victory Point cards and VP items in this set: All cards and items returning true for SOCInventoryItem.isVPItem().
 int getTotal()
           
 boolean hasPlayable(int ctype)
          Does this set contain 1 or more playable cards or items of this type? (Playable this turn: Not new, not already played and then kept.)
 boolean keepPlayedItem(int itype)
          Keep a played item: Change its state from PLAYABLE to KEPT.
 void newToOld()
          Change all the new cards and items to old ones.
 void removeDevCard(int age, int ctype)
          Remove one dev card of a type from the set.
 SOCInventoryItem removeItem(int state, int itype)
          Remove a special item or card with a certain state from this set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OLD

public static final int OLD
Age constant: An old item can either be played this turn (state PLAYABLE) or is kept in hand until the end of the game (state KEPT) such as a Victory Point card.

See Also:
Constant Field Values

NEW

public static final int NEW
Age constant, item state constant: Recently bought card, playable next turn.
Other possible age is OLD.
Other possible states are PLAYABLE and KEPT.

See Also:
Constant Field Values

PLAYABLE

public static final int PLAYABLE
Item state constant: Playable this turn (not NEW or KEPT).

Since:
2.0.00
See Also:
Constant Field Values

KEPT

public static final int KEPT
Item state constant: Kept in hand until end of game (not PLAYABLE, was never NEW).

Since:
2.0.00
See Also:
Constant Field Values

news

private final java.util.List<SOCInventoryItem> news
Current set of the items having 1 of 3 possible states (New and not playable yet; Playable; Kept until end of game). If an item's type has SOCDevCard.isVPCard(ctype) it is placed in kept, never in news.

This implementation assumes players will have only a few cards or items at a time, so linear searching for a SOCInventoryItem.itype type is acceptable.

Since:
2.0.00

playables

private final java.util.List<SOCInventoryItem> playables
Current set of the items having 1 of 3 possible states (New and not playable yet; Playable; Kept until end of game). If an item's type has SOCDevCard.isVPCard(ctype) it is placed in kept, never in news.

This implementation assumes players will have only a few cards or items at a time, so linear searching for a SOCInventoryItem.itype type is acceptable.

Since:
2.0.00

kept

private final java.util.List<SOCInventoryItem> kept
Current set of the items having 1 of 3 possible states (New and not playable yet; Playable; Kept until end of game). If an item's type has SOCDevCard.isVPCard(ctype) it is placed in kept, never in news.

This implementation assumes players will have only a few cards or items at a time, so linear searching for a SOCInventoryItem.itype type is acceptable.

Since:
2.0.00
Constructor Detail

SOCInventory

public SOCInventory()
Make an empty dev card and inventory item set.


SOCInventory

public SOCInventory(SOCInventory set)
             throws java.lang.CloneNotSupportedException
Make a copy of a dev card and inventory item set. The copy is deep: all contained SOCDevCard/SOCInventoryItem objects are cloned.

Parameters:
set - the inventory set to copy
Throws:
java.lang.CloneNotSupportedException - Should not occur; SOCInventoryItems should be Cloneable
Method Detail

clear

public void clear()
set the total number of dev cards to zero


getByState

public java.util.List<SOCInventoryItem> getByState(int cState)
                                            throws java.lang.IllegalArgumentException
Get the cards and items, if any, having this state. Please treat the returned list as read-only.

Parameters:
cState - Card/item state: NEW, PLAYABLE or KEPT
Returns:
Cards and items, or an empty list
Throws:
java.lang.IllegalArgumentException - if cState isn't one of the 3 item states
Since:
2.0.00
See Also:
hasPlayable(int)

hasPlayable

public boolean hasPlayable(int ctype)
Does this set contain 1 or more playable cards or items of this type? (Playable this turn: Not new, not already played and then kept.)

Parameters:
ctype - Type of development card from SOCDevCardConstants, or item type from SOCInventoryItem.itype
Returns:
True if has at least 1 playable card of this type
Since:
2.0.00
See Also:
getByState(int)

getAmount

public int getAmount(int ctype)
Get the amount of a dev card type or special item in the set.

Parameters:
ctype - Type of development card or item as described in SOCDevCardConstants and SOCInventoryItem.itype
Returns:
the number of new + of old cards/items of this type
Since:
2.0.00
See Also:
getAmount(int, int), getAmountByState(int, int)

getAmount

public int getAmount(int age,
                     int ctype)
Get the amount of a dev card type of certain age in the set. Does not count other types of inventory item, only SOCDevCard.

Parameters:
age - either OLD or NEW
ctype - the type of development card as described in SOCDevCardConstants
Returns:
the amount of a kind of development card
See Also:
getAmount(int), getAmountByState(int, int), hasPlayable(int), getByState(int)

getAmountByState

public int getAmountByState(int state,
                            int itype)
                     throws java.lang.IllegalArgumentException
Get the amount of dev cards or special items by state and type.

Parameters:
state - NEW, PLAYABLE, or KEPT
itype - Item type code, from SOCInventoryItem.itype or SOCDevCardConstants
Returns:
the number of special items or dev cards of this state and type
Throws:
java.lang.IllegalArgumentException - if state isn't one of the 3 item states
Since:
2.0.00
See Also:
getAmount(int, int)

getTotal

public int getTotal()
Returns:
the total number of development cards and special items
See Also:
getNumUnplayed(), getNumVPItems()

addItem

public void addItem(SOCInventoryItem item)
Add a special item or dev card to this set.

The item's SOCInventoryItem.isPlayable() or/and SOCInventoryItem.isKept() will be called to determine its initial state:

Parameters:
item - The special item or dev card being added
Since:
2.0.00
See Also:
addDevCard(int, int, int), removeItem(int, int), keepPlayedItem(int)

addDevCard

public void addDevCard(int amt,
                       int age,
                       int ctype)
Add an amount to a type of dev card. VP cards will be added with state KEPT. Otherwise, cards with age == OLD will have state PLAYABLE, new cards will have NEW.

Before v2.0.00, this method was add(amt, age, ctype).

Parameters:
age - either OLD or NEW
ctype - the type of development card, at least SOCDevCardConstants.MIN and less than SOCDevCardConstants.MAXPLUSONE
amt - the amount
See Also:
addItem(SOCInventoryItem), removeDevCard(int, int)

keepPlayedItem

public boolean keepPlayedItem(int itype)
Keep a played item: Change its state from PLAYABLE to KEPT.

Parameters:
itype - Item type code from SOCInventoryItem.itype
Returns:
true if kept, false if not found in Playable state
Since:
2.0.00

removeItem

public SOCInventoryItem removeItem(int state,
                                   int itype)
                            throws java.lang.IllegalArgumentException
Remove a special item or card with a certain state from this set. If its type isn't found, try to remove from SOCDevCardConstants.UNKNOWN instead.

Parameters:
state - Item state: NEW, PLAYABLE or KEPT
itype - Item type code from SOCInventoryItem.itype, or card type from SOCDevCardConstants
Returns:
item removed, or null if not found
Throws:
java.lang.IllegalArgumentException - if state isn't one of the 3 item states
Since:
2.0.00
See Also:
removeDevCard(int, int), keepPlayedItem(int)

removeDevCard

public void removeDevCard(int age,
                          int ctype)
Remove one dev card of a type from the set. If that type isn't available, remove from SOCDevCardConstants.UNKNOWN instead.

Before v2.0.00, this method was subtract(amt, age, ctype).

Parameters:
age - either OLD or NEW
ctype - the type of development card, at least SOCDevCardConstants.MIN and less than SOCDevCardConstants.MAXPLUSONE
See Also:
removeItem(int, int)

getNumVPItems

public int getNumVPItems()
Get the number of Victory Point cards and VP items in this set: All cards and items returning true for SOCInventoryItem.isVPItem().

Before v2.0.00, this was getNumVPCards().

Returns:
the number of victory point cards in this set
See Also:
getNumUnplayed(), getTotal(), getByState(int), SOCDevCard.isVPCard(int)

getNumUnplayed

public int getNumUnplayed()
Some card types stay in your hand after being played. Count only the unplayed ones (old or new); kept VP cards are skipped.

Returns:
the number of unplayed cards in this set
See Also:
getNumVPItems(), getTotal(), getByState(int)

newToOld

public void newToOld()
Change all the new cards and items to old ones. Each one's state NEW becomes PLAYABLE.