AIsOfCatan.GameController.BuildRoad C# (CSharp) Method

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
        public GameState BuildRoad(Player player, Edge edge)
        {
            var r = player.Resources;
            if (!(r.Contains(Resource.Brick) && r.Contains(Resource.Lumber)))
                throw new InsufficientResourcesException("Not enough resources to buy a road");
            if (player.RoadsLeft == 0)
                throw new IllegalActionException("No more road pieces left of your color");
            if (board.GetRoad(edge) != -1)
                throw new IllegalBuildPositionException("The chosen position is occupied by another road");
            if (!RoadConnected(board, edge, player.Id))
                throw new IllegalBuildPositionException("The chosen position is not connected to any of your pieces");
            if (!board.CanBuildRoad(edge))
                throw new IllegalBuildPositionException("The chosen position is not valid");

            PayResource(player, Resource.Brick);
            PayResource(player, Resource.Lumber);

            Log(new BuildRoadLogEvent(player.Id, edge));

            player.RoadsLeft--;
            board = board.PlaceRoad(edge, player.Id);
            UpdateLongestRoad();
            return CurrentGamestate();
        }

Usage Example

Example #1
0
 public GameState BuildRoad(Edge edge)
 {
     if (!valid)
     {
         throw new IllegalActionException("Tried to perform an action on an invalid GameAction");
     }
     if (!isAfterDieRoll)
     {
         throw new IllegalActionException("Tried to build before the die roll");
     }
     return(controller.BuildRoad(player, edge));
 }