AIsOfCatan.GameController.PlayMonopoly C# (CSharp) Method

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
        public GameState PlayMonopoly(Player player, Resource resource)
        {
            var playable = GetPlayableDevelopmentCards(player);
            if (!playable.Contains(DevelopmentCard.Monopoly)) throw new InsufficientResourcesException("No Monopoly in hand");
            player.DevelopmentCards.Remove(DevelopmentCard.Monopoly);
            //Take all resources of the given type out of all hands
            int count = players.Sum(t => t.Resources.RemoveAll(c => c == resource));
            //And place them in the playing players hand
            for (int i = 0; i < count; i++)
            {
                player.Resources.Add(resource);
            }

            Log(new PlayMonopolyLogEvent(player.Id, resource, count));

            return CurrentGamestate();
        }

Usage Example

Example #1
0
 public GameState PlayMonopoly(Resource resource)
 {
     if (!valid)
     {
         throw new IllegalActionException("Tried to perform an action on an invalid GameAction");
     }
     if (hasPlayedDevCard)
     {
         throw new IllegalActionException("Max one development card can be played each turn");
     }
     hasPlayedDevCard = true;
     return(controller.PlayMonopoly(player, resource));
 }