AIsOfCatan.GameController.TradeBank C# (CSharp) Method

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
        public GameState TradeBank(Player player, Resource giving, Resource receiving)
        {
            if (resourceBank[(int)receiving] == 0)
                throw new NoMoreCardsException("Resource bank has no more resources of type " + receiving);

            var harbors = board.GetPlayersHarbors(player.Id);
            var hasSpecific = harbors.Contains((HarborType) giving);
            var hasGeneral = harbors.Contains(HarborType.ThreeForOne);

            var amountToGive = (hasSpecific) ? 2 : ((hasGeneral) ? 3 : 4);

            if (player.Resources.Count(r => r == giving) < amountToGive)
                throw new InsufficientResourcesException("Player hasn't got enough resources to trade");

            PayResource(player,giving,amountToGive);
            GetResource(player,receiving);

            Log(new TradeBankLogEvent(player.Id,giving,amountToGive,receiving));

            return CurrentGamestate();
        }

Usage Example

Example #1
0
 public GameState TradeBank(Resource giving, Resource receiving)
 {
     if (!valid)
     {
         throw new IllegalActionException("Tried to trade on an invalid GameAction");
     }
     if (!isAfterDieRoll)
     {
         throw new IllegalActionException("Tried to trade before the die roll");
     }
     return(controller.TradeBank(player, giving, receiving));
 }