AIsOfCatan.GameController.PlayRoadBuilding C# (CSharp) Method

PlayRoadBuilding() public method

Let a player play a road building development card If the player doesn't have a RoadBuilding card on his hand a InsufficientResourcesException is thrown If the player doesn't have any road pieces left a IllegalActionException is thrown If the player tries to place a road at a position where a road is already present a IllegalBuildPositionException is thrown If the player only has one road piece left, the position to place it must be passed as road1Tile1, road1Tile2 (the others are ignored)
public PlayRoadBuilding ( Player player, Edge firstRoad, Edge secondRoad ) : GameState
player Player The player that plays the RoadBuilding development card
firstRoad Edge The first edge to build a road on
secondRoad Edge The second edge to build a road on
return GameState
        public GameState PlayRoadBuilding(Player player, Edge firstRoad, Edge secondRoad)
        {
            var playable = GetPlayableDevelopmentCards(player);
            if (!playable.Contains(DevelopmentCard.RoadBuilding)) throw new InsufficientResourcesException("No Road building found in hand");
            if (player.RoadsLeft == 0) throw new IllegalActionException("No more road pieces left of your color");

            //Must always check road1
            if (!board.CanBuildRoad(firstRoad))
                throw new IllegalBuildPositionException("The chosen position is illegal or occupied");
            if (board.GetRoad(firstRoad) != -1)
                throw new IllegalBuildPositionException("There is already a road on the selected position");

            if (player.RoadsLeft == 1)
            {
                if (!RoadConnected(board, firstRoad, player.Id))
                    throw new IllegalBuildPositionException("The chosen position is not connected to any of your pieces");

                player.RoadsLeft--;
                board = board.PlaceRoad(firstRoad, player.Id);
                Log(new PlayRoadBuildingLogEvent(player.Id, firstRoad));
            }
            else
            {
                //Check road 2
                if (!board.CanBuildRoad(secondRoad))
                    throw new IllegalBuildPositionException("The chosen position is illegal or occupied");
                if (board.GetRoad(secondRoad) != -1)
                    throw new IllegalBuildPositionException("There is already a road on the selected position");

                //Can't build the same road twice
                if ((firstRoad.FirstTile == secondRoad.FirstTile && firstRoad.SecondTile == secondRoad.SecondTile) || (firstRoad.FirstTile == secondRoad.SecondTile && firstRoad.SecondTile == secondRoad.FirstTile))
                    throw new IllegalBuildPositionException("Can't build the same road twice (roadbuilding dev. card)");

                //Place the connected road first (to be able to check that both are connected in the end
                if (RoadConnected(board, firstRoad, player.Id))
                {
                    var temp = board.PlaceRoad(firstRoad, player.Id);
                    if (RoadConnected(temp, secondRoad, player.Id))
                    {
                        board = temp.PlaceRoad(secondRoad, player.Id);
                        player.RoadsLeft -= 2;
                    }
                }
                else if (RoadConnected(board, secondRoad, player.Id))
                {
                    var temp = board.PlaceRoad(secondRoad, player.Id);
                    if (RoadConnected(temp, firstRoad, player.Id))
                    {
                        board = temp.PlaceRoad(firstRoad, player.Id);
                        player.RoadsLeft -= 2;
                    }
                }
                else
                {
                    throw new IllegalBuildPositionException("The chosen positions are not connected to any of your buildings or roads");
                }
                Log(new PlayRoadBuildingLogEvent(player.Id, firstRoad, secondRoad));
            }

            player.DevelopmentCards.Remove(DevelopmentCard.RoadBuilding);
            UpdateLongestRoad();
            return CurrentGamestate();
        }

Usage Example

Example #1
0
 public GameState PlayRoadBuilding(Edge firstEdge, Edge secondEdge)
 {
     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.PlayRoadBuilding(player, firstEdge, secondEdge));
 }