using System; using System.Collections.Generic; using System.Linq; namespace GameFix.Poker { public static class CardFunc { /// /// 获取牌值分数 (5 10 K) /// public static int GetCard510KScore(List cards) { int score = 0; for (int i = 0; i < cards.Count; i++) { if (cards[i].Value == DataCardConst.CardValue5) score += 5; if (cards[i].Value == DataCardConst.CardValue10) score += 10; if (cards[i].Value == DataCardConst.CardValueK) score += 10; } return score; } /// /// 获取cards 中所有510k的集合 /// public static List> Get510KList(List cards, int lessNumber) { var result = new List>(); var valueCardDict = cards.GroupBy(x => x.Value) .ToDictionary(g => g.Key, g => g.ToList()); valueCardDict.TryGetValue(DataCardConst.CardValue5, out var cards5); valueCardDict.TryGetValue(DataCardConst.CardValue10, out var cards10); valueCardDict.TryGetValue(DataCardConst.CardValueK, out var cardsK); if (cards5?.Count >= lessNumber && cards10?.Count >= lessNumber && cardsK?.Count >= lessNumber) { var min = Math.Min(Math.Min(cards5.Count, cards10.Count), cardsK.Count); for (int i = 0; i < min; i++) { var list = new List { cards5[i], cards10[i], cardsK[i] }; result.Add(list); } } return result; } public static int GetJokerCount(List cards) { return cards.Count(DataCardConst.IsJoker); } public static bool IsAllJoker(List cards) { if (cards.Count == 0) return false; if (cards.Count == 1) return true; for (int i = 1; i < cards.Count; i++) { if (!DataCardConst.IsJoker(cards[i])) { return false; } } return true; } /// /// 获取最大得同色数量 /// /// /// public static int GetMaxSameColorCount(List cards) { return cards.Select(x => GetSuitColorType(x.Suit)) .GroupBy(x => x).Select(g => g.Count()).DefaultIfEmpty(0).Max(); } /// /// 获取最大同牌值数量 /// public static int GetMaxSameValueCount(List cards) { return cards.Select(x => x.Value) .GroupBy(x => x).Select(g => g.Count()).DefaultIfEmpty(0).Max(); } /// /// 所有颜色是否相同 (红色/黑色) /// public static bool IsAllColorSame(List cards) { if (cards.Count == 0) return false; if (cards.Count == 1) return true; for (int i = 1; i < cards.Count; i++) { if (GetSuitColorType(cards[i].Suit) != GetSuitColorType(cards[i-1].Suit)) { return false; } } return true; } public static int GetSuitColorType(int suit) { if (suit == DataCardConst.SuitSpade || suit == DataCardConst.SuitClub) return 1; if (suit == DataCardConst.SuitHeart || suit == DataCardConst.SuitDiamond) return 2; return 0; } /// /// 所有花色是否相同 /// public static bool IsAllSuitSame(List cards) { if (cards.Count == 0) return false; if (cards.Count == 1) return true; for (int i = 1; i < cards.Count; i++) { if (cards[i].Suit != cards[i-1].Suit) { return false; } } return true; } /// /// 所有牌值是否相同 /// public static bool IsAllValueSame(List cards) { if (cards.Count == 0) return false; if (cards.Count == 1) return true; for (int i = 1; i < cards.Count; i++) { if (cards[i].Value != cards[i-1].Value) { return false; } } return true; } } }