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();
}