public void CalculateTargetedRoutes() { var allRoutes = new List <BoardRoute>(); var highestCards = DestinationCards.OrderBy(x => x.PointValue).ToList(); foreach (var destCard in highestCards) { var matchingRoutes = CalculateTargetedRoutes(destCard); if (matchingRoutes.Any()) { allRoutes.AddRange(matchingRoutes); break; } } TargetedRoutes = allRoutes.GroupBy(x => new { x.Origin, x.Destination, x.Color, x.Length }) .Select(group => new { Metric = group.Key, Count = group.Count() }).OrderByDescending(x => x.Count) .ThenByDescending(x => x.Metric.Length) .Take(5) .Select(x => new BoardRoute(x.Metric.Origin, x.Metric.Destination, x.Metric.Color, x.Metric.Length)) .ToList(); DesiredColors = TargetedRoutes.Select(x => x.Color) .Distinct() .ToList(); if (!DesiredColors.Any() && Hand.Any()) { //The player should target what they have the most of var color = Hand.GroupBy(x => x.Color) .Select(group => new { Color = group.Key, Count = group.Count() }) .OrderByDescending(x => x.Count) .Select(x => x.Color) .First(); DesiredColors.Add(color); } }
public void TakeTurn() { CalculateTargetedRoutes(); //The calculation for Desired Routes only returned routes that can still be claimed. //If there are no desired routes, then all routes the player wants are already claimed //(whether by this player or someone else). //Therefore, if there are no desired routes, draw new destination cards if (!TargetedRoutes.Any()) { if (!Board.DestinationCards.Any()) { Console.WriteLine("No destination cards remain! " + Name + " must do something else!"); } else if (DestinationCards.Count < 5) { DrawDestinationCards(); return; } } //If the player can claim a route they desire, they will do so immediately. var hasClaimed = TryClaimRoute(); if (hasClaimed) { return; } //We now have a problem. It is possible for a player to have a lot of train cards. //So, let's have them claim the longest route they can claim //with the cards they have available, if they have more than 24 cards. if (Hand.Count >= 24) { ClaimLongestUnclaimedRoute(); } else { DrawCards(); } }