soc.game
Class SOCPlayerNumbers

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

public class SOCPlayerNumbers
extends java.lang.Object

This class tracks what dice-roll numbers a player's pieces are touching, and the probability of getting resources based on their hex types.

Calls each touched hex's SOCBoard.getHexTypeFromCoord(int), ignores all hex types besides the usual SOCBoard.CLAY_HEX through SOCBoard.WOOD_HEX and SOCBoardLarge.GOLD_HEX.

Author:
Robert S. Thomas

Field Summary
 boolean hasSeaBoard
          Is this game played on the SOCBoardLarge large board / sea board? If true, the board's SOCBoard.getBoardEncodingFormat() must be SOCBoard.BOARD_ENCODING_LARGE.
private  int[] landHexCoords
          Reference to either SOCBoard.HEXCOORDS_LAND_V1 or SOCBoard.HEXCOORDS_LAND_V2.
private  java.util.Hashtable<java.lang.Integer,java.util.Vector<IntPair>> numberAndResourceForHex
          Hex dice-roll resource information, by land-hex coordinate ID.
private  java.util.Vector<java.lang.Integer>[] numbersForResource
          Dice roll numbers which yield this resource.
private  java.util.Vector<java.lang.Integer>[] resourcesForNumber
          Resources on dice roll numbers; uses indexes 2-12.
 
Constructor Summary
SOCPlayerNumbers(SOCBoard board)
          the constructor for a player's dice-resource numbers.
SOCPlayerNumbers(SOCPlayerNumbers numbers)
          make a copy of the player numbers
 
Method Summary
 void addNumberForResource(int diceNum, int resource, int hex)
          add a number to the list of dice numbers for a resource
 void clear()
          clear all of the data
 java.util.Vector<IntPair> getNumberResourcePairsForHex(int hexCoord)
           
 java.util.Vector<java.lang.Integer> getNumbersForResource(int resource)
          Get the numbers for a resource type.
 java.util.Vector<java.lang.Integer> getNumbersForResource(int resource, int robberHex)
          Get the numbers for a resource type.
 java.util.Vector<java.lang.Integer> getResourcesForNumber(int diceNum)
          Get this player's resources gained when a dice number is rolled.
 java.util.Vector<java.lang.Integer> getResourcesForNumber(int diceNum, int robberHex)
          Get this player's resources gained when a dice number is rolled.
 boolean hasNoResourcesForHex(int hexCoord)
          Do we receive no resources at all from this hex on any dice rolls?
 boolean hasNumber(int number)
          return true if this player is touching the requested number
 void setLandHexCoordinates(int[] landHex)
          Set the land hex coordinates, once the board layout is known.
 java.lang.String toString()
          return a human readable form of this object
 void undoAddNumberForResource(int number, int resource, int hex)
          remove a number for a resource do this when you take back a piece
 void undoUpdateNumbers(int coord, SOCBoard board)
          undo the updating of the numbers data given a node coordinate and a board, remove numbers for this player from the list
 void undoUpdateNumbers(SOCPlayingPiece piece, SOCBoard board)
          undo the updating of the numbers data given a piece and a board, remove numbers for this player from the list
 void updateNumbers(int nodeCoord, SOCBoard board)
          update the numbers data, based on placing a settlement or upgrading to a city at a node.
 void updateNumbers(SOCPlayingPiece piece, SOCBoard board)
          Update the numbers data, based on placing a settlement or upgrading to a city at a node.
 int updateNumbersAndProbability(int nodeCoord, SOCBoard board, int[] numProb, java.lang.StringBuffer sb)
          Update the numbers data, based on placing a settlement or upgrading to a city at a node, and total the probability for those dice numbers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

numbersForResource

private java.util.Vector<java.lang.Integer>[] numbersForResource
Dice roll numbers which yield this resource. Uses indexes in range SOCResourceConstants.CLAY to SOCResourceConstants.WOOD. Each element contains Integers for the dice numbers.

SOCBoardLarge.GOLD_HEX is handled by adding the dice number to all resource types in addNumberForResource(int, int, int).


resourcesForNumber

private java.util.Vector<java.lang.Integer>[] resourcesForNumber
Resources on dice roll numbers; uses indexes 2-12. Each element contains Integers for the resource(s), in range SOCResourceConstants.CLAY to SOCResourceConstants.WOOD. If the number yields more than one of that resource type (a city, or multiple pieces on the hex), there will be more than one Integer here with that resource type.

SOCBoardLarge.GOLD_HEX is handled by adding all resource types to the dice number in addNumberForResource(int, int, int).


numberAndResourceForHex

private java.util.Hashtable<java.lang.Integer,java.util.Vector<IntPair>> numberAndResourceForHex
Hex dice-roll resource information, by land-hex coordinate ID. Key = Integer hexCoord; value = Vector. Each hex coordinate's vector contains 0 or more IntPair(diceNum, resource). If hasSeaBoard, the resource type may be SOCBoardLarge.GOLD_HEX.

Until addNumberForResource(int, int, int) or updateNumbers(int, SOCBoard) is called, any land hex's entry in this hashtable may be null.


landHexCoords

private int[] landHexCoords
Reference to either SOCBoard.HEXCOORDS_LAND_V1 or SOCBoard.HEXCOORDS_LAND_V2. Hex coordinates for each land hex on the board, via SOCBoard.getLandHexCoords(). In SOCBoard.BOARD_ENCODING_LARGE, if the game hasn't yet called SOCBoard.makeNewBoard(Map), this may be null.

Since:
1.1.08

hasSeaBoard

public final boolean hasSeaBoard
Is this game played on the SOCBoardLarge large board / sea board? If true, the board's SOCBoard.getBoardEncodingFormat() must be SOCBoard.BOARD_ENCODING_LARGE.

When hasSeaBoard, SOCBoardLarge.GOLD_HEX is tracked. When false, it's ignored because the same numeric value in the previous encoding formats is water (SOCBoard.MISC_PORT_HEX).

Since:
2.0.00
Constructor Detail

SOCPlayerNumbers

public SOCPlayerNumbers(SOCPlayerNumbers numbers)
make a copy of the player numbers

Parameters:
numbers - the player numbers to copy

SOCPlayerNumbers

public SOCPlayerNumbers(SOCBoard board)
                 throws java.lang.IllegalArgumentException
the constructor for a player's dice-resource numbers.

If using SOCBoard.BOARD_ENCODING_LARGE, and this is the start of a game that hasn't yet created the layout: The land hex coordinates will need to be updated later when the board layout is created and sent; call setLandHexCoordinates(int[]) at that time.

Parameters:
board - The game board; used only for SOCBoard.getBoardEncodingFormat() and SOCBoard.getLandHexCoords().
Throws:
java.lang.IllegalArgumentException - If boardEncodingFormat value is unknown to this class
Method Detail

clear

public void clear()
clear all of the data


setLandHexCoordinates

public void setLandHexCoordinates(int[] landHex)
Set the land hex coordinates, once the board layout is known.

Parameters:
landHex - Array of hex coordinates for all land hexes
Since:
2.0.00

updateNumbers

public void updateNumbers(SOCPlayingPiece piece,
                          SOCBoard board)
Update the numbers data, based on placing a settlement or upgrading to a city at a node.

Given a piece and a board, add numbers for this player to the list: Calls addNumberForResource(int, int, int) for each dice number and resource on the piece node's adjacent hexes.

Parameters:
piece - the playing piece, used only for its node coordinate
board - the game board

updateNumbers

public void updateNumbers(int nodeCoord,
                          SOCBoard board)
update the numbers data, based on placing a settlement or upgrading to a city at a node.

Given a node coordinate and a board, add numbers for this player to the list: Calls addNumberForResource(int, int, int) for each dice number and resource on the node's adjacent hexes.

Parameters:
nodeCoord - the node coordinate
board - the game board
See Also:
updateNumbersAndProbability(int, SOCBoard, int[], StringBuffer)

updateNumbersAndProbability

public int updateNumbersAndProbability(int nodeCoord,
                                       SOCBoard board,
                                       int[] numProb,
                                       java.lang.StringBuffer sb)
Update the numbers data, based on placing a settlement or upgrading to a city at a node, and total the probability for those dice numbers.

Given a node coordinate and a board, add numbers for this player to the list: Calls addNumberForResource(int, int, int) for each dice number and resource on the node's adjacent hexes. Hexes are ignored if their SOCBoard.getNumberOnHexFromCoord(int) < 1.

Parameters:
nodeCoord - the settlement or city's node coordinate
board - the game board
numProb - probability factor for each dice number, as integers between 0 and 100 (percentage). numProb[i] is the percentage chance of rolling i.
sb - if not null, a StringBuffer to append each adjacent dice number into for debugging
Returns:
Total probability, based on numProb for each adjacent dice number
Since:
2.0.00
See Also:
updateNumbers(int, SOCBoard)

getResourcesForNumber

public java.util.Vector<java.lang.Integer> getResourcesForNumber(int diceNum)
Get this player's resources gained when a dice number is rolled.

SOCBoardLarge.GOLD_HEX is handled by adding all resource types to the dice number, if hasSeaBoard. So, gold hex numbers will have 5 resources in the Vector (10 for cities on gold).

Parameters:
diceNum - the dice number, 2-12
Returns:
the resources for a number; contains Integers for the resource(s), in range SOCResourceConstants.CLAY to SOCResourceConstants.WOOD. If the number yields more than one of that resource type (a city, or multiple pieces on the hex), there will be more than one Integer here with that resource type.
See Also:
getResourcesForNumber(int, int)

getNumbersForResource

public java.util.Vector<java.lang.Integer> getNumbersForResource(int resource)
Get the numbers for a resource type.

SOCBoardLarge.GOLD_HEX is handled by adding the dice number to all resource types, if hasSeaBoard.

Parameters:
resource - the resource, in range SOCResourceConstants.CLAY to SOCResourceConstants.WOOD
Returns:
the numbers for a resource, as Integers
See Also:
getNumbersForResource(int, int)

getNumberResourcePairsForHex

public java.util.Vector<IntPair> getNumberResourcePairsForHex(int hexCoord)
Parameters:
hexCoord - the hex coord
Returns:
the number-resource pairs for a hex; a Vector of 0 or more IntPair(diceNum, resource). May be null if hexCoord has no resources for us, or is not a valid land hex. If the number yields more than one of that resource type (a city, or multiple pieces on the hex), there will be more than one Integer here with that resource type. If hasSeaBoard, a resource type may be SOCBoardLarge.GOLD_HEX.
See Also:
hasNoResourcesForHex(int)

hasNoResourcesForHex

public boolean hasNoResourcesForHex(int hexCoord)
Do we receive no resources at all from this hex on any dice rolls?

Parameters:
hexCoord - the hex coordinate
Returns:
True if getNumberResourcePairsForHex(int) is empty; False if we do receive resources from this hex
Since:
2.0.00

getNumbersForResource

public java.util.Vector<java.lang.Integer> getNumbersForResource(int resource,
                                                                 int robberHex)
Get the numbers for a resource type.

SOCBoardLarge.GOLD_HEX is handled by adding the dice number to all resource types, if hasSeaBoard.

Parameters:
resource - the resource, in range SOCResourceConstants.CLAY to SOCResourceConstants.WOOD
robberHex - the robber hex
Returns:
the dice numbers for a resource (as Integers), taking the robber into account; if this resource is on two 8s (for example), there will be two Integer(8) in the returned vector.
See Also:
getNumbersForResource(int)

getResourcesForNumber

public java.util.Vector<java.lang.Integer> getResourcesForNumber(int diceNum,
                                                                 int robberHex)
Get this player's resources gained when a dice number is rolled.

SOCBoardLarge.GOLD_HEX is handled by adding all resource types to the dice number, if hasSeaBoard. So, gold hex numbers will have 5 resources in the Vector (10 for cities on gold).

Parameters:
diceNum - the dice roll, 2 - 12
robberHex - the robber hex coordinate
Returns:
the resources for a dice number, taking the robber into account; Integers in range SOCResourceConstants.CLAY to SOCResourceConstants.WOOD. If the number yields more than one of that resource type (a city, or multiple pieces on the hex), there will be more than one Integer here with that resource type.
See Also:
getResourcesForNumber(int)

addNumberForResource

public void addNumberForResource(int diceNum,
                                 int resource,
                                 int hex)
add a number to the list of dice numbers for a resource

Parameters:
diceNum - the dice-roll number
resource - the resource, in range SOCResourceConstants.CLAY to SOCResourceConstants.WOOD; resources outside this range are ignored. If hasSeaBoard, can be SOCBoardLarge.GOLD_HEX as returned from SOCBoardLarge.getHexTypeFromCoord(int).
hex - the hex coordinate ID

undoUpdateNumbers

public void undoUpdateNumbers(SOCPlayingPiece piece,
                              SOCBoard board)
undo the updating of the numbers data given a piece and a board, remove numbers for this player from the list

Parameters:
piece - the playing piece
board - the game board

undoUpdateNumbers

public void undoUpdateNumbers(int coord,
                              SOCBoard board)
undo the updating of the numbers data given a node coordinate and a board, remove numbers for this player from the list

Parameters:
coord - the node coordinate
board - the game board

undoAddNumberForResource

public void undoAddNumberForResource(int number,
                                     int resource,
                                     int hex)
remove a number for a resource do this when you take back a piece

Parameters:
number - the dice-roll number
resource - the resource, in range SOCResourceConstants.CLAY to SOCResourceConstants.WOOD, from SOCBoard.getHexTypeFromCoord(int). If hasSeaBoard, can be SOCBoardLarge.GOLD_HEX.
hex - the hex coordinate ID

hasNumber

public boolean hasNumber(int number)
return true if this player is touching the requested number

Parameters:
number - the dice-roll number
Returns:
true if the player has the number

toString

public java.lang.String toString()
return a human readable form of this object

Overrides:
toString in class java.lang.Object