soc.game
Class SOCBoard

java.lang.Object
  extended bysoc.game.SOCBoard
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class SOCBoard
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

This is a representation of the board in Settlers of Catan.

Other methods to examine the board: SOCGame.getPlayersOnHex(int), SOCGame.putPiece(SOCPlayingPiece), etc.

Coordinate system, as seen in appendix A of Robert S Thomas' dissertation:

Hexes (represented as coordinate of their centers), nodes (corners of hexes; where settlements/cities are placed), and edges (between nodes; where roads are placed), share the same grid of coordinates. Each hex is 2 units wide, in a 2-D coordinate system. The first axis runs northwest to southeast; the second runs southwest to northeast. Having six sides, hexes run in a straight line west to east, separated by vertical edges; both coordinates increase along a west-to-east line.

All coordinates are encoded as two-digit hex integers, one digit per axis (thus 00 to FF). Unfortunately this means the board can't be expanded without changing this encoding. The center hex is encoded as 77; see the dissertation appendix PDF for diagrams.

Author:
Robert S Thomas
See Also:
Serialized Form

Field Summary
private  int[] boardNum2Num
          Key to the numbers[] : 0 : 2 1 : 3 2 : 4 3 : 5 4 : 6 5 : 8 6 : 9 7 : 10 8 : 11 9 : 12
private  java.util.Vector cities
          cities on the board; Vector of SOCPlayingPiece
static int CLAY_HEX
           
static int CLAY_PORT
          Clay port type
static int CLAY_PORT_HEX
           
static int DESERT_HEX
          Desert; lowest-numbered hex type
private  int[] hexIDtoNum
          translate hex ID to an array index
private  int[] hexLayout
          Each element's value encodes port facing and hex type.
private  int[] hexNodes
          offset to add to hex coord to get all node coords
static int MAX_ROBBER_HEX
          Highest-numbered hex type which may hold a robber: WOOD_HEX.
static int MAXEDGE
          largest coordinate value for an edge
static int MAXHEX
          largest coordinate value for a hex
static int MAXNODE
          largest coordinate value for a node
static int MAXNODEPLUSONE
          largest coordinate value for a node plus one
static int MINEDGE
          smallest coordinate value for an edge
static int MINHEX
          smallest coordinate value for a hex
static int MINNODE
          smallest coordinate value for a node
static int MISC_PORT
          Misc (3-for-1) port; lowest-numbered port-type integer
static int MISC_PORT_HEX
          Misc (3-for-1) port type; lowest-numbered port-hextype integer
protected  java.util.Hashtable nodesOnBoard
          a list of nodes on the board; key is node's Integer coordinate, value is Boolean
private  int[] nodeToHex
          offset of all hexes adjacent to a node
private  int[] num2BoardNum
           
private  int[] numberLayout
          Dice number from hex numbers; @see #numToHexID
private  int[] numToHexID
          Hex coordinates from hex numbers
static int ORE_HEX
           
static int ORE_PORT
          Ore port type
static int ORE_PORT_HEX
           
private  java.util.Vector pieces
          pieces on the board; Vector of SOCPlayingPiece
private  java.util.Vector[] ports
          where the ports are; coordinates per port type.
private  java.util.Random rand
          random number generator
private  java.util.Vector roads
          roads on the board; Vector of SOCPlayingPiece
private  int robberHex
          the hex coordinate that the robber is in; placed on desert in constructor
private  java.util.Vector settlements
          settlements on the board; Vector of SOCPlayingPiece
static int SHEEP_HEX
           
static int SHEEP_PORT
          Sheep port type
static int SHEEP_PORT_HEX
           
static int WATER_HEX
          Water hex; higher-numbered than all land hex types
static int WHEAT_HEX
           
static int WHEAT_PORT
          Wheat port type
static int WHEAT_PORT_HEX
           
static int WOOD_HEX
          Wood; highest-numbered land hex type (also MAX_ROBBER_HEX)
static int WOOD_PORT
          Wood port type; highest-numbered port-type integer
static int WOOD_PORT_HEX
          Wood port type; highest-numbered port-hextype integer
 
Constructor Summary
SOCBoard()
          Create a new Settlers of Catan Board
 
Method Summary
 java.lang.String edgeCoordToString(int edge)
           
static java.util.Vector getAdjacentEdgesToEdge(int coord)
           
static java.util.Vector getAdjacentEdgesToNode(int coord)
           
static java.util.Vector getAdjacentHexesToNode(int coord)
           
static java.util.Vector getAdjacentNodesToEdge(int coord)
           
static java.util.Vector getAdjacentNodesToNode(int coord)
           
 java.util.Vector getCities()
          get the list of cities
 int[] getHexLayout()
           
 int getHexTypeFromCoord(int hex)
          Given a hex coordinate, return the type of hex
 int getHexTypeFromNumber(int hex)
          Given a hex number, return the type of hex
 int[] getNumberLayout()
           
 int getNumberOnHexFromCoord(int hex)
          Given a hex coordinate, return the number on that hex
 int getNumberOnHexFromNumber(int hex)
          Given a hex number, return the (dice-roll) number on that hex
 java.util.Vector getPieces()
          get the list of pieces on the board
 java.util.Vector getPortCoordinates(int portType)
           
 int getPortTypeFromHex(int hex)
           
 java.util.Vector getRoads()
          get the list of roads
 int getRobberHex()
           
 java.util.Vector getSettlements()
          get the list of settlements
private  void initHexIDtoNumAux(int begin, int end, int num)
          Auxiliary method for initializing part of the hexIDtoNum array
 boolean isNodeOnBoard(int node)
           
 void makeNewBoard()
          Shuffle the hex tiles and layout a board
 java.lang.String nodeCoordToString(int node)
           
private  void placePort(int port, int hex, int face)
          Auxiliary method for placing the port hexes
 void putPiece(SOCPlayingPiece pp)
          put a piece on the board
 void removePiece(SOCPlayingPiece piece)
          remove a piece from the board
 SOCPlayingPiece roadAtEdge(int edgeCoord)
          If there's a road placed at this node, find it.
 void setHexLayout(int[] hl)
          set the hexLayout
 void setNumberLayout(int[] nl)
          set the number layout
 void setRobberHex(int rh)
          set where the robber is
 SOCPlayingPiece settlementAtNode(int nodeCoord)
          If there's a settlement or city at this node, find it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DESERT_HEX

public static final int DESERT_HEX
Desert; lowest-numbered hex type

See Also:
Constant Field Values

CLAY_HEX

public static final int CLAY_HEX
See Also:
Constant Field Values

ORE_HEX

public static final int ORE_HEX
See Also:
Constant Field Values

SHEEP_HEX

public static final int SHEEP_HEX
See Also:
Constant Field Values

WHEAT_HEX

public static final int WHEAT_HEX
See Also:
Constant Field Values

WOOD_HEX

public static final int WOOD_HEX
Wood; highest-numbered land hex type (also MAX_ROBBER_HEX)

See Also:
Constant Field Values

WATER_HEX

public static final int WATER_HEX
Water hex; higher-numbered than all land hex types

See Also:
Constant Field Values

MISC_PORT_HEX

public static final int MISC_PORT_HEX
Misc (3-for-1) port type; lowest-numbered port-hextype integer

See Also:
Constant Field Values

CLAY_PORT_HEX

public static final int CLAY_PORT_HEX
See Also:
Constant Field Values

ORE_PORT_HEX

public static final int ORE_PORT_HEX
See Also:
Constant Field Values

SHEEP_PORT_HEX

public static final int SHEEP_PORT_HEX
See Also:
Constant Field Values

WHEAT_PORT_HEX

public static final int WHEAT_PORT_HEX
See Also:
Constant Field Values

WOOD_PORT_HEX

public static final int WOOD_PORT_HEX
Wood port type; highest-numbered port-hextype integer

See Also:
Constant Field Values

MISC_PORT

public static final int MISC_PORT
Misc (3-for-1) port; lowest-numbered port-type integer

See Also:
Constant Field Values

CLAY_PORT

public static final int CLAY_PORT
Clay port type

See Also:
Constant Field Values

ORE_PORT

public static final int ORE_PORT
Ore port type

See Also:
Constant Field Values

SHEEP_PORT

public static final int SHEEP_PORT
Sheep port type

See Also:
Constant Field Values

WHEAT_PORT

public static final int WHEAT_PORT
Wheat port type

See Also:
Constant Field Values

WOOD_PORT

public static final int WOOD_PORT
Wood port type; highest-numbered port-type integer

See Also:
Constant Field Values

MAX_ROBBER_HEX

public static final int MAX_ROBBER_HEX
Highest-numbered hex type which may hold a robber: WOOD_HEX.

See Also:
Constant Field Values

MAXHEX

public static final int MAXHEX
largest coordinate value for a hex

See Also:
Constant Field Values

MINHEX

public static final int MINHEX
smallest coordinate value for a hex

See Also:
Constant Field Values

MAXEDGE

public static final int MAXEDGE
largest coordinate value for an edge

See Also:
Constant Field Values

MINEDGE

public static final int MINEDGE
smallest coordinate value for an edge

See Also:
Constant Field Values

MAXNODE

public static final int MAXNODE
largest coordinate value for a node

See Also:
Constant Field Values

MINNODE

public static final int MINNODE
smallest coordinate value for a node

See Also:
Constant Field Values

MAXNODEPLUSONE

public static final int MAXNODEPLUSONE
largest coordinate value for a node plus one

See Also:
Constant Field Values

hexLayout

private int[] hexLayout
Each element's value encodes port facing and hex type. Key to the hexes[] values: 0 : desert 1 : clay 2 : ore 3 : sheep 4 : wheat 5 : wood 6 : water 7 : misc port facing 1 8 : misc port facing 2 9 : misc port facing 3 10 : misc port facing 4 11 : misc port facing 5 12 : misc port facing 6 ports are represented in binary like this:
        (port facing)           (kind of port)
              \--> [0 0 0][0 0 0 0] <--/       
kind of port: 1 : clay CLAY_PORT 2 : ore ORE_PORT 3 : sheep SHEEP_PORT 4 : wheat WHEAT_PORT 5 : wood WOOD_PORT port facing:
        6___    ___1
            \/\/
            /  \
       5___|    |___2
           |    |
            \  /
        4___/\/\___3  

See Also:
getHexTypeFromNumber(int)

boardNum2Num

private int[] boardNum2Num
Key to the numbers[] : 0 : 2 1 : 3 2 : 4 3 : 5 4 : 6 5 : 8 6 : 9 7 : 10 8 : 11 9 : 12


num2BoardNum

private int[] num2BoardNum

numberLayout

private int[] numberLayout
Dice number from hex numbers; @see #numToHexID


numToHexID

private int[] numToHexID
Hex coordinates from hex numbers


hexIDtoNum

private int[] hexIDtoNum
translate hex ID to an array index


hexNodes

private int[] hexNodes
offset to add to hex coord to get all node coords


nodeToHex

private int[] nodeToHex
offset of all hexes adjacent to a node


robberHex

private int robberHex
the hex coordinate that the robber is in; placed on desert in constructor


ports

private java.util.Vector[] ports
where the ports are; coordinates per port type. Indexes are port types, MISC_PORT to WOOD_PORT.


pieces

private java.util.Vector pieces
pieces on the board; Vector of SOCPlayingPiece


roads

private java.util.Vector roads
roads on the board; Vector of SOCPlayingPiece


settlements

private java.util.Vector settlements
settlements on the board; Vector of SOCPlayingPiece


cities

private java.util.Vector cities
cities on the board; Vector of SOCPlayingPiece


rand

private java.util.Random rand
random number generator


nodesOnBoard

protected java.util.Hashtable nodesOnBoard
a list of nodes on the board; key is node's Integer coordinate, value is Boolean

Constructor Detail

SOCBoard

public SOCBoard()
Create a new Settlers of Catan Board

Method Detail

initHexIDtoNumAux

private final void initHexIDtoNumAux(int begin,
                                     int end,
                                     int num)
Auxiliary method for initializing part of the hexIDtoNum array

Parameters:
begin - Beginning of coordinate range
end - Ending coordinate
num - Number to assign to hexIDtoNum[] within this coordinate range

makeNewBoard

public void makeNewBoard()
Shuffle the hex tiles and layout a board


placePort

private final void placePort(int port,
                             int hex,
                             int face)
Auxiliary method for placing the port hexes

Parameters:
port - Port type; in range MISC_PORT to WOOD_PORT.
hex - Hex coordinate
face - Facing of port; 1 to 6

getHexLayout

public int[] getHexLayout()
Returns:
the hex layout

getNumberLayout

public int[] getNumberLayout()
Returns:
the number layout

getRobberHex

public int getRobberHex()
Returns:
coordinate where the robber is

setHexLayout

public void setHexLayout(int[] hl)
set the hexLayout

Parameters:
hl - the hex layout

getPortTypeFromHex

public int getPortTypeFromHex(int hex)
Parameters:
hex - the hex type, as in hexLayout
Returns:
the type of port given a hex type; in range MISC_PORT to WOOD_PORT

setNumberLayout

public void setNumberLayout(int[] nl)
set the number layout

Parameters:
nl - the number layout

setRobberHex

public void setRobberHex(int rh)
set where the robber is

Parameters:
rh - the robber hex coordinate

getPortCoordinates

public java.util.Vector getPortCoordinates(int portType)
Parameters:
portType - the type of port; in range MISC_PORT to WOOD_PORT.
Returns:
the list of coordinates for a type of port; each element is an Integer

getNumberOnHexFromCoord

public int getNumberOnHexFromCoord(int hex)
Given a hex coordinate, return the number on that hex

Parameters:
hex - the coordinates for a hex
Returns:
the number on that hex

getNumberOnHexFromNumber

public int getNumberOnHexFromNumber(int hex)
Given a hex number, return the (dice-roll) number on that hex

Parameters:
hex - the number of a hex
Returns:
the dice-roll number on that hex, or 0

getHexTypeFromCoord

public int getHexTypeFromCoord(int hex)
Given a hex coordinate, return the type of hex

Parameters:
hex - the coordinates for a hex
Returns:
the type of hex: Land in range CLAY_PORT_HEX to WOOD_PORT_HEX, DESERT_HEX, or MISC_PORT_HEX for any port.

getHexTypeFromNumber

public int getHexTypeFromNumber(int hex)
Given a hex number, return the type of hex

Parameters:
hex - the number of a hex (not its coordinate)
Returns:
the type of hex: Land in range CLAY_PORT_HEX to WOOD_PORT_HEX, DESERT_HEX, or MISC_PORT_HEX for any port.

putPiece

public void putPiece(SOCPlayingPiece pp)
put a piece on the board


removePiece

public void removePiece(SOCPlayingPiece piece)
remove a piece from the board


getPieces

public java.util.Vector getPieces()
get the list of pieces on the board


getRoads

public java.util.Vector getRoads()
get the list of roads


getSettlements

public java.util.Vector getSettlements()
get the list of settlements


getCities

public java.util.Vector getCities()
get the list of cities


getAdjacentNodesToEdge

public static java.util.Vector getAdjacentNodesToEdge(int coord)
Returns:
the nodes that touch this edge

getAdjacentEdgesToEdge

public static java.util.Vector getAdjacentEdgesToEdge(int coord)
Returns:
the adjacent edges to this edge

getAdjacentHexesToNode

public static java.util.Vector getAdjacentHexesToNode(int coord)
Returns:
the coordinates (Integers) of the 1 to 3 hexes touching this node

getAdjacentEdgesToNode

public static java.util.Vector getAdjacentEdgesToNode(int coord)
Returns:
the edges touching this node

getAdjacentNodesToNode

public static java.util.Vector getAdjacentNodesToNode(int coord)
Returns:
the EDGEs adjacent to this node

settlementAtNode

public SOCPlayingPiece settlementAtNode(int nodeCoord)
If there's a settlement or city at this node, find it.

Parameters:
nodeCoord - Location coordinate (as returned by SOCBoardPanel.findNode)
Returns:
Settlement or city or null

roadAtEdge

public SOCPlayingPiece roadAtEdge(int edgeCoord)
If there's a road placed at this node, find it.

Parameters:
edgeCoord - Location coordinate (as returned by SOCBoardPanel.findEdge)
Returns:
road or null

isNodeOnBoard

public boolean isNodeOnBoard(int node)
Parameters:
node - Node coordinate
Returns:
true if the node is on the board

nodeCoordToString

public java.lang.String nodeCoordToString(int node)
Returns:
a string representation of a node coordinate

edgeCoordToString

public java.lang.String edgeCoordToString(int edge)
Returns:
a string representation of an edge coordinate