Beispiel #1
0
    /**
     * Roads must be connected to a settlement or another road.
     */
    public bool CanPlaceRoad(Player player, int col, int row, BoardGrid.EdgeSpecifier edgeSpec)
    {
        // Check if connected to a settlement of the same player
        bool          validbuildingWithNoRoadsNearby = false;
        bool          validbuildingNearby            = false;
        List <Vertex> adjacentVertices = boardGrid.GetConnectedVerticesFromEdge(col, row, edgeSpec);

        foreach (Vertex vertex in adjacentVertices)
        {
            Settlement buildingObject = vertex.settlement;
            if (buildingObject != null)
            {
                if (buildingObject != null)
                {
                    if (buildingObject.ownerId == player.GetId())
                    {
                        validbuildingNearby = true;

                        int[] vertexPos = boardGrid.GetVertexPosition(vertex);
                        if (vertexPos != null)
                        {
                            List <Edge> edgesTemp = boardGrid.GetAdjacentEdgesFromVertex(vertexPos[0], vertexPos[1], (BoardGrid.VertexSpecifier)vertexPos[2]);

                            int roadCount = 0;
                            foreach (Edge edgeTemp in edgesTemp)
                            {
                                if (edgeTemp.road != null)
                                {
                                    roadCount++;
                                }
                            }

                            if (roadCount == 0)
                            {
                                validbuildingWithNoRoadsNearby = true;
                            }
                        }
                    }
                }
            }
        }

        // Check if connected to a road of the same player
        bool        validRoadNearby = false;
        List <Edge> adjacentEdges   = boardGrid.GetConnectedEdgesFromEdge(col, row, edgeSpec);

        foreach (Edge adjacentEdge in adjacentEdges)
        {
            if (adjacentEdge.road != null)
            {
                if (adjacentEdge.road.ownerId == player.GetId())
                {
                    // Check that if the road is connected, that it is not interrupted by an enemy settlement
                    int[]  adjacentEdgeCoords = boardGrid.GetEdgeCoordinates(adjacentEdge);
                    Vertex vertexBetweenRoads = boardGrid.GetVertexBetweenTwoConnectedEdges(col, row, edgeSpec, adjacentEdgeCoords[0], adjacentEdgeCoords[1], (BoardGrid.EdgeSpecifier)adjacentEdgeCoords[2]);

                    if (vertexBetweenRoads.settlement != null)
                    {
                        if (vertexBetweenRoads.settlement.ownerId == player.GetId())
                        {
                            validRoadNearby = true;
                        }
                    }
                    else
                    {
                        validRoadNearby = true;
                    }
                }
            }
        }

        // If it's the second turn, only let the player place next to the settlement with no road
        if (GameManager.Instance.GetTurnCycle() == 2 && !validbuildingWithNoRoadsNearby)
        {
            return(false);
        }

        if (!validRoadNearby && !validbuildingNearby)
        {
            return(false);
        }

        // Check if cost requirements met
        if (!player.CanAffordResourceTransaction(1, 0, 0, 1, 0) && player.freeRoads <= 0)
        {
            return(false);
        }

        // Check to see if tile is empty and have enough roads in store
        Edge edge = boardGrid.GetEdge(col, row, edgeSpec);

        if (edge.road == null && player.storeRoadNum > 0)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }