Example #1
0
        public void TestGetNeighborEdges()
        {
            var hex = new HexEdge(0, 0, 1, 0);
            var neighbors = hex.GetNeighborEdges();
            Assert.AreEqual(4, neighbors.Count, "A hex edge must have 4 neighbors.");

            var msg = "Hex edge is missing a neighbor.";
            Assert.IsTrue(neighbors.Contains(new HexEdge(0, 0, 0, -1)), msg);
            Assert.IsTrue(neighbors.Contains(new HexEdge(1, 0, 0, -1)), msg);
            Assert.IsTrue(neighbors.Contains(new HexEdge(0, 0, 1, 1)), msg);
            Assert.IsTrue(neighbors.Contains(new HexEdge(1, 0, 1, 1)), msg);
        }
Example #2
0
 private IEnumerable<HexEdge> GetRoadPermutations(int player, HexEdge road, IEnumerable<HexEdge> allPlayerRoads)
 {
     return road.GetNeighborEdges()
                .Where(e => allPlayerRoads.Contains(e) &&
                            !IsEnemyPlayerBuildingHere(player, road.GetNeighborPoint(e))); // An enemy building can cut off a road.
 }
Example #3
0
        /// <summary>
        /// Returns a success result if this is a valid placement for a road.
        /// </summary>
        public ActionResult ValidateRoadPlacement(int player, HexEdge edge, bool startOfGame)
        {
            if (!IsEdgeInBoard(edge))
            {
                return new ActionResult(false, string.Format("The road location {0} is out of bounds.", edge));
            }

            // Do not allow removing a road.
            if (_roads.ContainsKey(edge))
            {
                return new ActionResult(false, string.Format("The road location {0} is already used.", edge));
            }

            if (startOfGame)
            {
                // Make sure this is touching a settlement owned by this player. It's the start of the game.
                if (!IsPlayerBuildingHere(player, edge))
                {
                    return new ActionResult(false, String.Format("The road location {0} is not near a settlement.", edge));
                }
            }
            else
            {
                // We must be near a settlement, city, or another road.
                if (!IsPlayerBuildingHere(player, edge))
                {
                    bool isTouchingRoad = false;
                    foreach (var surroundingRoad in edge.GetNeighborEdges())
                    {
                        if (IsPlayerRoadHere(player, surroundingRoad))
                        {
                            isTouchingRoad = true;
                            break;
                        }
                    }
                    if (!isTouchingRoad)
                    {
                        return new ActionResult(false, string.Format("The road location {0} is not near a city, settlement, or road.", edge));
                    }
                }
            }
            return ActionResult.CreateSuccess();
        }