AIsOfCatan.Board.GetLongestRoad C# (CSharp) Method

GetLongestRoad() public method

public GetLongestRoad ( ) : int>.Dictionary
return int>.Dictionary
        public Dictionary<int, int> GetLongestRoad()
        {
            // find longest road for each player
            //      player, length
            var playersLongest = new Dictionary<int, int>(4);

            // floodfill from each road segment to see if it constitutes the longest road.
            var visited = new HashSet<Edge>();
            foreach (var road in roads)
            {
                if (visited.Contains(road.Key)) continue; // already explored
                visited.Add(road.Key);

                // get ends
                var ends = this.GetAdjacentIntersections(road.Key);
                var tempVisited = new HashSet<Edge>();
                tempVisited.Add(road.Key);
                int first  = CountRoadLengthFromIntersection(road.Value, ends[0], tempVisited, visited);
                int second = CountRoadLengthFromIntersection(road.Value, ends[1], tempVisited, visited);

                int result = first + 1 + second;
                if (!playersLongest.ContainsKey(road.Value) || playersLongest[road.Value] < result)
                {
                    playersLongest[road.Value] = result;
                }
            }

            return playersLongest;
        }