C# Class AIsOfCatan.GameController

Show file Open project: rasmusgreve/catan Class Usage Examples

Public Methods

Method Description
BuildCity ( Player player, Intersection intersection ) : GameState

Let a player upgrade a settlement to a city If the player doesn't have enough resources to build a city a InsufficientResourcesException is thrown If the player tries to build at a position where he doesn't have a settlement a IllegalBuildPosition is thrown If the player doesn't have any more city pieces left to place a IllegalActionException is thrown The required resources are taken from the player and placed back at the resource bank The settlement previously on the location is given back to the player

BuildFirstRoad ( Player player, Edge edge ) : GameState

Let a player build a road without a requirement for connectivity and free of charge The road may not be placed on top of another road Connectivity with previously placed settlement should be controlled elsewhere (in StartActions.cs)

BuildFirstSettlement ( Player player, Intersection intersection ) : GameState

Let a player build a settlement without a requirement for connectivity and free of charge The settlement still has to follow the distance rule and cannot be placed on top of another building

BuildRoad ( Player player, Edge edge ) : GameState

Let a player build a road If the player doesn't have enough resources to build a road an InsufficientResourcesException is thrown If the player tries to build at a position not connected to another road, settlement or city an IllegalBuildPositionException is thrown If the player doesn't have any more road pieces left to place an IllegalActionException is thrown

BuildSettlement ( Player player, Intersection inter ) : GameState

Let a player build a settlement If the player doesn't have enough resources to build a settlement a InsufficientResourcesException is thrown If the player tries to build too close to another building, or not connected to a road a IllegalBuildPosition is thrown If the player doesn't have any more settlement pieces left to place a IllegalActionException is thrown The required resources are taken from the player and placed back at the resource bank If the settlement is placed at a harbor, the harbor can be used immediately (rules p. 7 - footnote 12)

CompleteTrade ( Player player, int playerid ) : GameState
DrawDevelopmentCard ( Player player ) : GameState

Let a player draw a development card If the player doesn't have enough resources a InsufficientResourcesException is thrown If the development card stack is empty a NoMoreCardsException is thrown Resources to pay for the card are removed from the player hand and returned to the resource bank

GetBoard ( ) : IBoard

Returns the current game board held by this controller.

PlayKnight ( Player player ) : GameState

Let a player play a knight development card If the player doesn't have a knight on his hand a InsufficientResourcesException is thrown A knight is removed from the players hand The largest army special card is relocated if playing this knight causes it to be

PlayMonopoly ( Player player, Resource resource ) : GameState

Let a player play a Monopoly development card If the player doesn't have a Monopoly card on his hand a InsufficientResourcesException is thrown All resources of the given type is removed from players hands and all given to the playing player

PlayRoadBuilding ( Player player, Edge firstRoad, Edge secondRoad ) : GameState

Let a player play a road building development card If the player doesn't have a RoadBuilding card on his hand a InsufficientResourcesException is thrown If the player doesn't have any road pieces left a IllegalActionException is thrown If the player tries to place a road at a position where a road is already present a IllegalBuildPositionException is thrown If the player only has one road piece left, the position to place it must be passed as road1Tile1, road1Tile2 (the others are ignored)

PlayYearOfPlenty ( Player player, Resource resource1, Resource resource2 ) : GameState

Let a player play a year of plenty development card If the player doesn't have a YearOfPlenty card on his hand a InsufficientResourcesException is thrown If the resource bank doesn't have enough cards to fulfill the request a NoMoreCardsException is thrown

ProposeTrade ( Player player, List give, List take ) : ITrade>.Dictionary
StartGame ( IAgent agents, int boardSeed, int diceSeed ) : int
StartGame ( IAgent agents, int boardSeed, int diceSeed, bool visual, bool logToFile ) : int

Start the game. This method will run for the length of the game and returns the id of the winner

TradeBank ( Player player, Resource giving, Resource receiving ) : GameState

Trade resources with the bank If no harbor is owned trading is 4 : 1 If a general harbor is owned trading is 3 : 1 Special harbors trade a specific resource 2 : 1

Private Methods

Method Description
CurrentGamestate ( ) : GameState
CurrentGamestate ( int playerId ) : GameState
GameLoop ( ) : int

Executes the game, each player turn by turn until a player wins

GetPlayableDevelopmentCards ( Player player ) : List

Get a list of playable development cards for a given player Newly bought development cards cannot be played in the same round

GetPoints ( Player player ) : int

Find out how many victory points a given player has Points are counted as: Settlements give 1 point each Cities give 2 points each Victory point development cards give 1 point each Having the largest army or the longest road give 2 points each.

GetResource ( Player player, Resource resource, int quantity = 1 ) : void

Give a player an amount of some resource If there are no more cards in the pile an NoMoreCardsException is thrown

HandOutResources ( int roll ) : void

Hand out resources to players according to a roll Gives resources to players with buildings on tiles with a number corresponding to the roll and only if the tile doesn't have the robber. If there is not enough resources of a kind in the bank so that all player who shall receive, can get the amount they are allowed to, none of that kind are dealt (see rules p. 8 top)

HasWon ( Player player ) : System.Boolean

Find out if a given player has won (if his number of victory points is over or equal to 10)

Log ( LogEvent evt ) : void
MoveRobber ( Player player, GameState gameState ) : void

Let a player move the robber to a new location and draw a random card from a player with a building on the tile If the agent moves the robber to a water tile or the tile that it was already on, nothing will happen If the agent tries to draw a card from a unaffected player, no cards will be drawn

NextTurn ( ) : void

Increment the turn variable modulo the number of players

PayResource ( Player player, Resource resource, int quantity = 1 ) : void

Let a given player pay an amount of a resource to the bank

PlaceStarts ( ) : void

Let the players place their starting settlements and roads and receive resources from all neighboring tiles from the last placed settlement (rules p. 7 top)

PopulateDevelopmentCardStack ( ) : void

Populate and shuffle the development card stack according to the rules (rules p. 2 - Game Contents)

PrevTurn ( ) : void

Decrement the turn variable, if it goes below 0, wrap around

RoadConnected ( IBoard board, Edge edge, int playerId ) : bool

Figure out if a given edge on the board is connected to a given players road in some end

RollDice ( ) : int

Roll two d6 using the seeded random number generator and get the sum

ShowScores ( ) : void
StartGUI ( ) : void
TakeTurn ( Player player ) : void

Executes all parts of a players turn 1. Allow the play of a development card before the dice are rolled 2. Roll the dice, hand out resources to all players, and move the robber if roll is 7 3. Allow all actions according to the rules

UpdateLongestRoad ( ) : void

Update the player who has the longest road to be able to determine who has how many points

Method Details

BuildCity() public method

Let a player upgrade a settlement to a city If the player doesn't have enough resources to build a city a InsufficientResourcesException is thrown If the player tries to build at a position where he doesn't have a settlement a IllegalBuildPosition is thrown If the player doesn't have any more city pieces left to place a IllegalActionException is thrown The required resources are taken from the player and placed back at the resource bank The settlement previously on the location is given back to the player
public BuildCity ( Player player, Intersection intersection ) : GameState
player Player The player upgrading to a city
intersection AIsOfCatan.API.Intersection The intersection to upgrade to a city on
return GameState

BuildFirstRoad() public method

Let a player build a road without a requirement for connectivity and free of charge The road may not be placed on top of another road Connectivity with previously placed settlement should be controlled elsewhere (in StartActions.cs)
public BuildFirstRoad ( Player player, Edge edge ) : GameState
player Player The player placing the road
edge Edge The edge to build the road on
return GameState

BuildFirstSettlement() public method

Let a player build a settlement without a requirement for connectivity and free of charge The settlement still has to follow the distance rule and cannot be placed on top of another building
public BuildFirstSettlement ( Player player, Intersection intersection ) : GameState
player Player The player placing the settlement
intersection AIsOfCatan.API.Intersection The intersection to build a settlement on
return GameState

BuildRoad() public method

Let a player build a road If the player doesn't have enough resources to build a road an InsufficientResourcesException is thrown If the player tries to build at a position not connected to another road, settlement or city an IllegalBuildPositionException is thrown If the player doesn't have any more road pieces left to place an IllegalActionException is thrown
public BuildRoad ( Player player, Edge edge ) : GameState
player Player The player building a road
edge Edge The to build a road on
return GameState

BuildSettlement() public method

Let a player build a settlement If the player doesn't have enough resources to build a settlement a InsufficientResourcesException is thrown If the player tries to build too close to another building, or not connected to a road a IllegalBuildPosition is thrown If the player doesn't have any more settlement pieces left to place a IllegalActionException is thrown The required resources are taken from the player and placed back at the resource bank If the settlement is placed at a harbor, the harbor can be used immediately (rules p. 7 - footnote 12)
public BuildSettlement ( Player player, Intersection inter ) : GameState
player Player The player building a settlement
inter AIsOfCatan.API.Intersection The intersection to build a settlement on
return GameState

CompleteTrade() public method

public CompleteTrade ( Player player, int playerid ) : GameState
player Player
playerid int
return GameState

DrawDevelopmentCard() public method

Let a player draw a development card If the player doesn't have enough resources a InsufficientResourcesException is thrown If the development card stack is empty a NoMoreCardsException is thrown Resources to pay for the card are removed from the player hand and returned to the resource bank
public DrawDevelopmentCard ( Player player ) : GameState
player Player The player drawing a development card
return GameState

GetBoard() public method

Returns the current game board held by this controller.
public GetBoard ( ) : IBoard
return IBoard

PlayKnight() public method

Let a player play a knight development card If the player doesn't have a knight on his hand a InsufficientResourcesException is thrown A knight is removed from the players hand The largest army special card is relocated if playing this knight causes it to be
public PlayKnight ( Player player ) : GameState
player Player The player playing a knight
return GameState

PlayMonopoly() public method

Let a player play a Monopoly development card If the player doesn't have a Monopoly card on his hand a InsufficientResourcesException is thrown All resources of the given type is removed from players hands and all given to the playing player
public PlayMonopoly ( Player player, Resource resource ) : GameState
player Player The player playing the monopoly development card
resource Resource The resource to get monopoly on
return GameState

PlayRoadBuilding() public method

Let a player play a road building development card If the player doesn't have a RoadBuilding card on his hand a InsufficientResourcesException is thrown If the player doesn't have any road pieces left a IllegalActionException is thrown If the player tries to place a road at a position where a road is already present a IllegalBuildPositionException is thrown If the player only has one road piece left, the position to place it must be passed as road1Tile1, road1Tile2 (the others are ignored)
public PlayRoadBuilding ( Player player, Edge firstRoad, Edge secondRoad ) : GameState
player Player The player that plays the RoadBuilding development card
firstRoad Edge The first edge to build a road on
secondRoad Edge The second edge to build a road on
return GameState

PlayYearOfPlenty() public method

Let a player play a year of plenty development card If the player doesn't have a YearOfPlenty card on his hand a InsufficientResourcesException is thrown If the resource bank doesn't have enough cards to fulfill the request a NoMoreCardsException is thrown
public PlayYearOfPlenty ( Player player, Resource resource1, Resource resource2 ) : GameState
player Player The player playing the year of plenty development card
resource1 Resource The type of resource for the first card
resource2 Resource The type of resource for the second card
return GameState

ProposeTrade() public method

public ProposeTrade ( Player player, List give, List take ) : ITrade>.Dictionary
player Player
give List
take List
return ITrade>.Dictionary

StartGame() public method

public StartGame ( IAgent agents, int boardSeed, int diceSeed ) : int
agents IAgent
boardSeed int
diceSeed int
return int

StartGame() public method

Start the game. This method will run for the length of the game and returns the id of the winner
public StartGame ( IAgent agents, int boardSeed, int diceSeed, bool visual, bool logToFile ) : int
agents IAgent The competing agents (The order in which they are submitted is irrelevant)
boardSeed int The seed for the board generator, used to shuffle development cards, and for drawing a random card after moving the robber
diceSeed int The seed for the dice
visual bool True if the game should be displayed visually
logToFile bool
return int

TradeBank() public method

Trade resources with the bank If no harbor is owned trading is 4 : 1 If a general harbor is owned trading is 3 : 1 Special harbors trade a specific resource 2 : 1
public TradeBank ( Player player, Resource giving, Resource receiving ) : GameState
player Player The player wanting to trade
giving Resource The resource that the player want to give
receiving Resource The resource that the player want to receive
return GameState