AIsOfCatan.GameController.BuildSettlement C# (CSharp) Method

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
        public GameState BuildSettlement(Player player, Intersection inter)
        {
            var r = player.Resources;
            if (!(r.Contains(Resource.Grain) && r.Contains(Resource.Wool) && r.Contains(Resource.Brick) && r.Contains(Resource.Lumber)))
                throw new InsufficientResourcesException("Not enough resources to buy a settlement");
            if (player.SettlementsLeft == 0)
                throw new IllegalActionException("No more settlement pieces left of your color");
            if (board.GetPiece(inter) != null)
                throw new IllegalBuildPositionException("The chosen position is occupied by another building");
            if (!board.HasNoNeighbors(inter))
                throw new IllegalBuildPositionException("The chosen position violates the distance rule");
            if (board.GetRoad(new Edge(inter.FirstTile,inter.SecondTile)) != player.Id
                && board.GetRoad(new Edge(inter.FirstTile,inter.ThirdTile)) != player.Id
                && board.GetRoad(new Edge(inter.SecondTile, inter.ThirdTile)) != player.Id)
                throw new IllegalBuildPositionException("The chosen position has no road leading to it");

            if (!board.CanBuildPiece(inter))
                throw new IllegalBuildPositionException("The chosen position is not valid");

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

            Log(new BuildPieceLogEvent(player.Id, Token.Settlement, inter));

            player.SettlementsLeft--;
            board = board.PlacePiece(inter, new Piece(Token.Settlement, player.Id));
            UpdateLongestRoad();
            return CurrentGamestate();
        }

Usage Example

Example #1
0
        //Building

        public GameState BuildSettlement(Intersection intersection)
        {
            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.BuildSettlement(player, intersection));
        }