コード例 #1
0
ファイル: XmlGame.cs プロジェクト: generateui/SettleIn
        /// <summary>
        /// Recalculates every route in case of a blokage
        /// </summary>
        /// <param name="town"></param>
        /// <param name="player"></param>
        public void CalculateLongestRoad(HexPoint town, GamePlayer player)
        {
            Route current = LongestRoute;

            // When we have a current route, check if the new town is part of it
            if (current != null)
            {
                // But only when the player building the town is other then the player owning
                // the current route
                if (!player.Equals(LongestRouteOwner))
                {
                    // Create a list of hexpoints in current route
                    List<HexPoint> routePoints = new List<HexPoint>();

                    // Put all points into the list
                    foreach (RouteNode node in current)
                    {
                        if (!routePoints.Contains(node.GetNeighbourPoints()[0]))
                            routePoints.Add(node.GetNeighbourPoints()[0]);
                        if (!routePoints.Contains(node.GetNeighbourPoints()[1]))
                            routePoints.Add(node.GetNeighbourPoints()[1]);
                    }

                    // Check if the town is placed on a hexpoint in the route
                    if (routePoints.Contains(town))
                    {
                        // We have a winner! Nice play. This means given town which is built blocks

                        // We should iterate over all players, calculating their longest routes.
                        // If current keeper of the longest route has a new route with length
                        // equalling length of another player, current keeper keeps his LR.
                        // However, when two other players both have longest route, no one gets
                        // the route.

                        // TODO: make static function call of CalculateALongestRoute
                        Route phony = new Route();

                        // Get us some nice new dictionairy to store the calculated routes
                        Dictionary<GamePlayer, Route> playersRoutes = new Dictionary<GamePlayer, Route>();

                        // Fill the dictionairy with new routes
                        foreach (GamePlayer p in _Players)
                            playersRoutes.Add(p, phony.CalculateALongestRoute(p, this));

                        // Sweet! Now let's see who is the winner of the routecontest

                        // First get the longest route of current keeper of LR
                        Route currentWinnerRoute = playersRoutes[LongestRouteOwner];

                        // Get the length of the longest route(s)
                        int newWinnerLength = playersRoutes.Max(kvp => kvp.Value.Count);

                        Route newRoute = null;

                        if (currentWinnerRoute.Count == newWinnerLength)
                        {
                            // Current keeper of longest route has a new route with the maximum length.
                            // Therefore, he stays the keeper of the longest route

                            newRoute = currentWinnerRoute;
                        }
                        else
                        {
                            // Some other player(s) have a longer route then the previous route, and still longer
                            // then the longest route of the keeper of the previous route.
                            // Determine if we have a single player having so. If a single player is found,
                            // declare him the winner. When multiple players are found, no one gets
                            // declared winner.

                            IEnumerable<KeyValuePair<GamePlayer, Route>> winners =
                                playersRoutes.Where(kvp => kvp.Value.Count == newWinnerLength);

                            // We have more then one new winners. No one gets LR.
                            if (winners.Count() > 1)
                            {
                                newRoute = null;
                            }

                            // Only one new winner
                            if (winners.Count() == 1)
                            {
                                newRoute = winners.First().Value;
                            }

                            // Old player lost route, no players with routes > 4
                            if (winners.Count() == 0)
                            {
                                newRoute = null;
                            }
                        }
                    }

                }
            }
            else
            {
                CalculateLongestRoad(player);
            }
        }