private static HandRank AddNew(List <HandRank> handRanks, KeyValuePair <Player, Hand> current) { HandRank handRank = new HandRank(handRanks.Count + 1, current.Value); handRank.AddPlayer(current.Key); handRanks.Add(handRank); return(handRank); }
public IReadOnlyList <HandRank> RankPlayerHands(IEnumerable <Player> players, IEnumerable <Card> doardCards) { ThrowIfDuplicate(players, doardCards); Dictionary <Player, Hand> hands = new Dictionary <Player, Hand>(); foreach (Player player in players) { List <Card> cards = new List <Card>(player.Cards); cards.AddRange(doardCards); Maybe <Hand> hand = handSelector.SelectBest(cards); if (!hand.HasValue) { throw new InvalidOperationException("Hand not ranked."); } hands.Add(player, hand.Value); } List <HandRank> handRanks = new List <HandRank>(); var hardsInOrder = hands.ToList().OrderByDescending(h => h.Value); HandRank handRank = null; foreach (var current in hardsInOrder) { if (!handRanks.Any()) { handRank = AddNew(handRanks, current); } else { if (handRank.IsSame(current.Value)) { handRank.AddPlayer(current.Key); } else { handRank = AddNew(handRanks, current); } } } return(handRanks); }