AIsOfCatan.GameController.BuildCity C# (CSharp) Method

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
        public GameState BuildCity(Player player, Intersection intersection)
        {
            var r = player.Resources;
            if (!(r.Count(c => c == Resource.Ore) >= 3 && r.Count(c => c == Resource.Grain) >= 2))
                throw new InsufficientResourcesException("Not enough resources to buy a city");
            if (player.CitiesLeft == 0)
                throw new IllegalActionException("No more city pieces left of your color");

            Piece piece = board.GetPiece(intersection);
            if (piece == null || piece.Player != player.Id || piece.Token != Token.Settlement)
                throw new IllegalBuildPositionException("The chosen position does not contain one of your settlements");

            PayResource(player, Resource.Ore, 3);
            PayResource(player, Resource.Grain, 2);

            Log(new BuildPieceLogEvent(player.Id, Token.City, intersection));

            player.CitiesLeft--;
            player.SettlementsLeft++;

            board = board.PlacePiece(intersection, new Piece(Token.City, player.Id));
            return CurrentGamestate();
        }

Usage Example

Example #1
0
 public GameState BuildCity(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.BuildCity(player, intersection));
 }