JSettlers v1.0.6 API
Built ${date}

soc.robot
Class SOCRobotBrain

java.lang.Object
  extended byjava.lang.Thread
      extended bysoc.robot.SOCRobotBrain
All Implemented Interfaces:
java.lang.Runnable

public class SOCRobotBrain
extends java.lang.Thread

AI for playing Settlers of Catan

Author:
Robert S Thomas

Field Summary
protected  boolean alive
          Flag for wheather or not we're alive
protected  java.util.Stack buildingPlan
          This is our current building plan
protected  SOCRobotClient client
          The client we are hooked up to
protected  int counter
          A counter used to measure passage of time
protected  int currentDRecorder
          keeps track of which dRecorder is current
protected  int curState
          Our current state
protected  SOCRobotDM decisionMaker
          the thing that determines what we want to build next
protected  boolean doneTrading
          true if we're done trading
protected  DebugRecorder[] dRecorder
          an object for recording debug information that can be accessed interactively
protected  boolean expectDICERESULT
          true if we're expecting a DICERESULT message
protected  boolean expectDISCARD
          true if we're expecting a DISCARDREQUEST message
protected  boolean expectMOVEROBBER
          true if we're expecting to have to move the robber
protected  boolean expectPLACING_CITY
          true if we're expecting the PLACING_CITY state
protected  boolean expectPLACING_FREE_ROAD1
          true if we're expecting the PLACING_FREE_ROAD1 state
protected  boolean expectPLACING_FREE_ROAD2
          true if we're expecting the PLACING_FREE_ROAD2 state
protected  boolean expectPLACING_ROAD
          true if we're expecting the PLACING_ROAD state
protected  boolean expectPLACING_ROBBER
          true if we're expecting the PLACING_ROBBER state
protected  boolean expectPLACING_SETTLEMENT
          true if we're expecting the PLACING_SETTLEMENT state
protected  boolean expectPLAY
          true if we're expecting the PLAY state
protected  boolean expectPLAY1
          true if we're expecting the PLAY1 state
protected  boolean expectPUTPIECE_FROM_START1A
          true if were expecting a PUTPIECE message after a START1A game state
protected  boolean expectPUTPIECE_FROM_START1B
          true if were expecting a PUTPIECE message after a START1B game state
protected  boolean expectPUTPIECE_FROM_START2A
          true if were expecting a PUTPIECE message after a START1A game state
protected  boolean expectPUTPIECE_FROM_START2B
          true if were expecting a PUTPIECE message after a START1A game state
protected  boolean expectSTART1A
          true if we're expecting the START1A state
protected  boolean expectSTART1B
          true if we're expecting the START1B state
protected  boolean expectSTART2A
          true if we're expecting the START2A state
protected  boolean expectSTART2B
          true if we're expecting the START2B state
protected  boolean expectWAITING_FOR_DISCOVERY
          true if we're expecting to pick two resources
protected  boolean expectWAITING_FOR_MONOPOLY
          true if we're expecting to pick a monopoly
protected  int firstSettlement
          used in planning where to put our first and second settlements
protected  SOCGame game
          The game we are playing
protected  CappedQueue gameEventQ
          The queue of game messages
protected  SOCPossiblePiece lastMove
          keeps track of the last thing we bought for debugging purposes
protected  SOCPossiblePiece lastTarget
          keeps track of the last thing we wanted for debugging purposes
protected  int monopolyChoice
          this is the resource we want to monopolize
protected  boolean moveRobberOnSeven
          true when the robber will move because a seven was rolled
protected  SOCRobotNegotiator negotiator
          the thing that determines how we negotiate
protected  boolean[] offerRejections
          true if the player with that player number has rejected our offer
protected  int oldGameState
          the game state before the current one
protected  SOCPlayer ourPlayerData
          Our player data
protected  SOCPlayerTracker ourPlayerTracker
          our player tracker
protected  boolean ourTurn
          Flag for wheather or not it is our turn
protected  SOCRobotPinger pinger
          a thread that sends ping messages to this one
protected  java.util.HashMap playerTrackers
          trackers for all players
protected  java.util.Random rand
          Random number generator
protected  SOCResourceSet resourceChoices
          these are the two resources that we want when we play a discovery dev card
protected  int[] resourceEstimates
          used to cache resource estiamtes for the board
protected  int secondSettlement
          used in planning where to put our first and second settlements
protected  int turnTime
          Timer for turn taking
protected  boolean waitingForDevCard
          true when we're waiting to receive a dev card
protected  boolean waitingForGameState
          true if we're waiting for a GAMESTATE message from the server
protected  boolean waitingForOurTurn
          true if we're waiting for a TURN message from the server when it's our turn
protected  boolean waitingForTradeMsg
          true when we're waiting for the results of a trade
protected  boolean waitingForTradeResponse
           
protected  SOCPlayingPiece whatWeWantToBuild
          This is what we want to build
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SOCRobotBrain(SOCRobotClient rc, SOCRobotParameters params, SOCGame ga, CappedQueue mq)
          Create a robot brain to play a game
 
Method Summary
protected  void bestSpot2AwayFromANodeSet(java.util.Hashtable nodesIn, java.util.Vector nodeSet, int weight)
          Takes a table of nodes and adds a weighted score to each node score in the table.
protected  void bestSpotForNumbers(java.util.Hashtable nodes, int weight)
          Takes a table of nodes and adds a weighted score to each node score in the table.
protected  void bestSpotForNumbers(java.util.Hashtable nodes, SOCPlayer player, int weight)
          Takes a table of nodes and adds a weighted score to each node score in the table.
protected  void bestSpotInANodeSet(java.util.Hashtable nodesIn, java.util.Vector nodeSet, int weight)
          Takes a table of nodes and adds a weighted score to each node score in the table.
protected  void chooseFreeResources(SOCResourceSet targetResources)
          this means that we want to play a discovery development card
protected  boolean chooseMonopoly()
          choose a resource to monopolize
protected  void chooseRobberVictim(boolean[] choices)
          choose a robber victim
protected  int considerOffer(SOCTradeOffer offer)
          consider an offer made by another player
protected  void discard(int numDiscards)
          discard some resources
protected  int[] estimateResourceRarity()
          estimate the rarity of each resource
protected  void expandTradeTreeNode(SOCTradeTree currentTreeNode, java.util.Hashtable table)
          expand a trade tree node
protected  soc.robot.BoardNodeScorePair findBestScoringNode(java.util.Hashtable nodes)
          given a table of nodes/edges with scores, return the best scoring pair
 java.util.Stack getBuildingPlan()
           
 SOCRobotClient getClient()
           
 SOCRobotDM getDecisionMaker()
           
 DebugRecorder getDRecorder()
           
 SOCGame getGame()
           
 SOCPossiblePiece getLastMove()
           
 SOCPossiblePiece getLastTarget()
           
 DebugRecorder getOldDRecorder()
           
 SOCPlayer getOurPlayerData()
           
 SOCPlayerTracker getOurPlayerTracker()
           
 java.util.HashMap getPlayerTrackers()
           
 SOCRobotParameters getRobotParameters()
           
 void kill()
          kill this brain
protected  boolean makeCounterOffer(SOCTradeOffer offer)
          make a counter offer to another player
protected  boolean makeOffer(SOCPossiblePiece target)
          make an offer to another player
protected  void moveRobber()
          /** move the robber
protected  int numberOfEnemyBuilds()
          calculate the number of builds before the next turn during init placement
 void pause(int msec)
          pause for a bit
protected  void placeFirstSettlement()
          place planned first settlement
 void placeInitRoad()
          place a road attached to the last initial settlement
protected  void placeSecondSettlement()
          place planned second settlement
protected  void planInitialSettlements()
          figure out where to place the two settlements
protected  void planSecondSettlement()
          figure out where to place the second settlement
 void run()
          Here is the run method.
protected  void scoreNodesForSettlements(java.util.Hashtable nodes, int numberWeight, int miscPortWeight, int portWeight)
          this is a function more for convience given a set of nodes, run a bunch of metrics across them to find which one is best for building a settlement
protected  int scoreTradeOutcome(SOCResourceSet tradeOutcome)
          evaluate a trade outcome by calculating how much you could build with it
 void setOurPlayerData()
          Find our player data using our nickname
protected  void tradeStuff()
          do some trading
protected  boolean tradeToTarget2(SOCResourceSet targetResources)
          make trades to get the target resources
 void turnOffDRecorder()
          turns the debug recorders off
 void turnOnDRecorder()
          turns the debug recorders on
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

alive

protected boolean alive
Flag for wheather or not we're alive


ourTurn

protected boolean ourTurn
Flag for wheather or not it is our turn


turnTime

protected int turnTime
Timer for turn taking


curState

protected int curState
Our current state


rand

protected java.util.Random rand
Random number generator


client

protected SOCRobotClient client
The client we are hooked up to


game

protected SOCGame game
The game we are playing


ourPlayerData

protected SOCPlayer ourPlayerData
Our player data


gameEventQ

protected CappedQueue gameEventQ
The queue of game messages


counter

protected int counter
A counter used to measure passage of time


whatWeWantToBuild

protected SOCPlayingPiece whatWeWantToBuild
This is what we want to build


buildingPlan

protected java.util.Stack buildingPlan
This is our current building plan


resourceChoices

protected SOCResourceSet resourceChoices
these are the two resources that we want when we play a discovery dev card


monopolyChoice

protected int monopolyChoice
this is the resource we want to monopolize


ourPlayerTracker

protected SOCPlayerTracker ourPlayerTracker
our player tracker


playerTrackers

protected java.util.HashMap playerTrackers
trackers for all players


decisionMaker

protected SOCRobotDM decisionMaker
the thing that determines what we want to build next


negotiator

protected SOCRobotNegotiator negotiator
the thing that determines how we negotiate


expectSTART1A

protected boolean expectSTART1A
true if we're expecting the START1A state


expectSTART1B

protected boolean expectSTART1B
true if we're expecting the START1B state


expectSTART2A

protected boolean expectSTART2A
true if we're expecting the START2A state


expectSTART2B

protected boolean expectSTART2B
true if we're expecting the START2B state


expectPLAY

protected boolean expectPLAY
true if we're expecting the PLAY state


expectPLAY1

protected boolean expectPLAY1
true if we're expecting the PLAY1 state


expectPLACING_ROAD

protected boolean expectPLACING_ROAD
true if we're expecting the PLACING_ROAD state


expectPLACING_SETTLEMENT

protected boolean expectPLACING_SETTLEMENT
true if we're expecting the PLACING_SETTLEMENT state


expectPLACING_CITY

protected boolean expectPLACING_CITY
true if we're expecting the PLACING_CITY state


expectPLACING_ROBBER

protected boolean expectPLACING_ROBBER
true if we're expecting the PLACING_ROBBER state


expectPLACING_FREE_ROAD1

protected boolean expectPLACING_FREE_ROAD1
true if we're expecting the PLACING_FREE_ROAD1 state


expectPLACING_FREE_ROAD2

protected boolean expectPLACING_FREE_ROAD2
true if we're expecting the PLACING_FREE_ROAD2 state


expectPUTPIECE_FROM_START1A

protected boolean expectPUTPIECE_FROM_START1A
true if were expecting a PUTPIECE message after a START1A game state


expectPUTPIECE_FROM_START1B

protected boolean expectPUTPIECE_FROM_START1B
true if were expecting a PUTPIECE message after a START1B game state


expectPUTPIECE_FROM_START2A

protected boolean expectPUTPIECE_FROM_START2A
true if were expecting a PUTPIECE message after a START1A game state


expectPUTPIECE_FROM_START2B

protected boolean expectPUTPIECE_FROM_START2B
true if were expecting a PUTPIECE message after a START1A game state


expectDICERESULT

protected boolean expectDICERESULT
true if we're expecting a DICERESULT message


expectDISCARD

protected boolean expectDISCARD
true if we're expecting a DISCARDREQUEST message


expectMOVEROBBER

protected boolean expectMOVEROBBER
true if we're expecting to have to move the robber


expectWAITING_FOR_DISCOVERY

protected boolean expectWAITING_FOR_DISCOVERY
true if we're expecting to pick two resources


expectWAITING_FOR_MONOPOLY

protected boolean expectWAITING_FOR_MONOPOLY
true if we're expecting to pick a monopoly


waitingForGameState

protected boolean waitingForGameState
true if we're waiting for a GAMESTATE message from the server


waitingForOurTurn

protected boolean waitingForOurTurn
true if we're waiting for a TURN message from the server when it's our turn


waitingForTradeMsg

protected boolean waitingForTradeMsg
true when we're waiting for the results of a trade


waitingForDevCard

protected boolean waitingForDevCard
true when we're waiting to receive a dev card


moveRobberOnSeven

protected boolean moveRobberOnSeven
true when the robber will move because a seven was rolled


waitingForTradeResponse

protected boolean waitingForTradeResponse

doneTrading

protected boolean doneTrading
true if we're done trading


offerRejections

protected boolean[] offerRejections
true if the player with that player number has rejected our offer


oldGameState

protected int oldGameState
the game state before the current one


resourceEstimates

protected int[] resourceEstimates
used to cache resource estiamtes for the board


firstSettlement

protected int firstSettlement
used in planning where to put our first and second settlements


secondSettlement

protected int secondSettlement
used in planning where to put our first and second settlements


pinger

protected SOCRobotPinger pinger
a thread that sends ping messages to this one


dRecorder

protected DebugRecorder[] dRecorder
an object for recording debug information that can be accessed interactively


currentDRecorder

protected int currentDRecorder
keeps track of which dRecorder is current


lastMove

protected SOCPossiblePiece lastMove
keeps track of the last thing we bought for debugging purposes


lastTarget

protected SOCPossiblePiece lastTarget
keeps track of the last thing we wanted for debugging purposes

Constructor Detail

SOCRobotBrain

public SOCRobotBrain(SOCRobotClient rc,
                     SOCRobotParameters params,
                     SOCGame ga,
                     CappedQueue mq)
Create a robot brain to play a game

Parameters:
rc - the robot client
params - the robot parameters
ga - the game we're playing
mq - the message queue
Method Detail

getRobotParameters

public SOCRobotParameters getRobotParameters()
Returns:
the robot parameters

getClient

public SOCRobotClient getClient()
Returns:
the player client

getPlayerTrackers

public java.util.HashMap getPlayerTrackers()
Returns:
the player trackers

getOurPlayerTracker

public SOCPlayerTracker getOurPlayerTracker()
Returns:
our player tracker

getGame

public SOCGame getGame()
Returns:
the game data

getOurPlayerData

public SOCPlayer getOurPlayerData()
Returns:
our player data

getBuildingPlan

public java.util.Stack getBuildingPlan()
Returns:
the building plan

getDecisionMaker

public SOCRobotDM getDecisionMaker()
Returns:
the decision maker

turnOnDRecorder

public void turnOnDRecorder()
turns the debug recorders on


turnOffDRecorder

public void turnOffDRecorder()
turns the debug recorders off


getDRecorder

public DebugRecorder getDRecorder()
Returns:
the debug recorder

getOldDRecorder

public DebugRecorder getOldDRecorder()
Returns:
the old debug recorder

getLastMove

public SOCPossiblePiece getLastMove()
Returns:
the last move we made

getLastTarget

public SOCPossiblePiece getLastTarget()
Returns:
our last target piece

setOurPlayerData

public void setOurPlayerData()
Find our player data using our nickname


run

public void run()
Here is the run method. Just keep receiving game events and deal with each one.


kill

public void kill()
kill this brain


pause

public void pause(int msec)
pause for a bit

Parameters:
msec - number of milliseconds to pause

planInitialSettlements

protected void planInitialSettlements()
figure out where to place the two settlements


planSecondSettlement

protected void planSecondSettlement()
figure out where to place the second settlement


placeFirstSettlement

protected void placeFirstSettlement()
place planned first settlement


placeSecondSettlement

protected void placeSecondSettlement()
place planned second settlement


placeInitRoad

public void placeInitRoad()
place a road attached to the last initial settlement


estimateResourceRarity

protected int[] estimateResourceRarity()
estimate the rarity of each resource

Returns:
an array of rarity numbers where estimates[SOCBoard.CLAY_HEX] == the clay rarity

bestSpotForNumbers

protected void bestSpotForNumbers(java.util.Hashtable nodes,
                                  int weight)
Takes a table of nodes and adds a weighted score to each node score in the table. Nodes touching hexes with better numbers get better scores.

Parameters:
nodes - the table of nodes with scores
weight - a number that is multiplied by the score

bestSpotForNumbers

protected void bestSpotForNumbers(java.util.Hashtable nodes,
                                  SOCPlayer player,
                                  int weight)
Takes a table of nodes and adds a weighted score to each node score in the table. Nodes touching hexes with better numbers get better scores. Also numbers that the player isn't touching yet are better than ones that the player is already touching.

Parameters:
nodes - the table of nodes with scores
player - the player that we are doing the rating for
weight - a number that is multiplied by the score

bestSpot2AwayFromANodeSet

protected void bestSpot2AwayFromANodeSet(java.util.Hashtable nodesIn,
                                         java.util.Vector nodeSet,
                                         int weight)
Takes a table of nodes and adds a weighted score to each node score in the table. A vector of nodes that we want to be near is also taken as an argument. Here are the rules for scoring: If a node is two away from a node in the desired set of nodes it gets 100. Otherwise it gets 0.

Parameters:
nodesIn - the table of nodes to evaluate
nodeSet - the set of desired nodes
weight - the score multiplier

bestSpotInANodeSet

protected void bestSpotInANodeSet(java.util.Hashtable nodesIn,
                                  java.util.Vector nodeSet,
                                  int weight)
Takes a table of nodes and adds a weighted score to each node score in the table. A vector of nodes that we want to be on is also taken as an argument. Here are the rules for scoring: If a node is in the desired set of nodes it gets 100. Otherwise it gets 0.

Parameters:
nodesIn - the table of nodes to evaluate
nodeSet - the set of desired nodes
weight - the score multiplier

moveRobber

protected void moveRobber()
/** move the robber


discard

protected void discard(int numDiscards)
discard some resources

Parameters:
numDiscards - the number of resources to discard

chooseRobberVictim

protected void chooseRobberVictim(boolean[] choices)
choose a robber victim

Parameters:
choices - a boolean array representing which players are possible victims

numberOfEnemyBuilds

protected int numberOfEnemyBuilds()
calculate the number of builds before the next turn during init placement


findBestScoringNode

protected soc.robot.BoardNodeScorePair findBestScoringNode(java.util.Hashtable nodes)
given a table of nodes/edges with scores, return the best scoring pair

Parameters:
nodes - the table of nodes/edges
Returns:
the best scoring pair

scoreNodesForSettlements

protected void scoreNodesForSettlements(java.util.Hashtable nodes,
                                        int numberWeight,
                                        int miscPortWeight,
                                        int portWeight)
this is a function more for convience given a set of nodes, run a bunch of metrics across them to find which one is best for building a settlement

Parameters:
nodes - a hashtable of nodes, the scores in the table will be modified
numberWeight - the weight given to nodes on good numbers
miscPortWeight - the weight given to nodes on 3:1 ports
portWeight - the weight given to nodes on good 2:1 ports

tradeStuff

protected void tradeStuff()
do some trading


expandTradeTreeNode

protected void expandTradeTreeNode(SOCTradeTree currentTreeNode,
                                   java.util.Hashtable table)
expand a trade tree node

Parameters:
currentTreeNode - the tree node that we're expanding
table - the table of all of the nodes in the tree except this one

scoreTradeOutcome

protected int scoreTradeOutcome(SOCResourceSet tradeOutcome)
evaluate a trade outcome by calculating how much you could build with it

Parameters:
tradeOutcome - a set of resources that would be the result of trading

tradeToTarget2

protected boolean tradeToTarget2(SOCResourceSet targetResources)
make trades to get the target resources

Parameters:
targetResources - the resources that we want
Returns:
true if we sent a request to trade

considerOffer

protected int considerOffer(SOCTradeOffer offer)
consider an offer made by another player

Parameters:
offer - the offer to consider
Returns:
a code that represents how we want to respond note: a negative result means we do nothing

makeOffer

protected boolean makeOffer(SOCPossiblePiece target)
make an offer to another player

Parameters:
target - the resources that we want
Returns:
true if we made an offer

makeCounterOffer

protected boolean makeCounterOffer(SOCTradeOffer offer)
make a counter offer to another player

Parameters:
offer - their offer
Returns:
true if we made an offer

chooseFreeResources

protected void chooseFreeResources(SOCResourceSet targetResources)
this means that we want to play a discovery development card


chooseMonopoly

protected boolean chooseMonopoly()
choose a resource to monopolize

Returns:
true if playing the card is worth it

JSettlers v1.0.6 API
Built ${date}