public static bool IsMetallic(this Games type) { return(type == Games.Metallic); }
/// <summary> /// 找炸弹 /// </summary> /// <param name="hand_cards"></param> /// <param name="result"></param> /// <returns></returns> private static List <CardEnum> FindBomb(Games game_type, List <CardEnum> hand_cards, CombineResult result) { var hand_cards_clone = hand_cards.Clone(); List <CardEnum> jokers = new List <CardEnum>(); if (game_type.IsMetallic()) { jokers = hand_cards_clone.FindAll(c => c == CardEnum.LittleJoker || c == CardEnum.ElderJoker); hand_cards_clone.RemoveAll(c => c == CardEnum.LittleJoker || c == CardEnum.ElderJoker); } var group_cards = hand_cards_clone.GroupBy(c => c).OrderBy(c => c.Count()); foreach (var group_card in group_cards) { if (group_card.Count() >= 4) { if (result.combine == Combine.Bomb) { //符合条件的炸弹 if (group_card.Count() > result.card_num || (group_card.Count() == result.card_num && group_card.Key > result.min_card)) { //符合条件炸弹 return(group_card.ToList()); } } else { //炸弹就行 return(group_card.ToList()); } } } if (game_type.IsMetallic()) { foreach (var group_card in group_cards) { if (group_card.Count() + jokers.Count() >= 4) { if (result.combine == Combine.Bomb) { if (group_card.Count() + jokers.Count() >= result.card_num && group_card.Key > result.min_card) { //同星级炸弹,牌比较大 var cards = group_card.ToList(); cards.AddRange(jokers.GetRange(0, result.card_num - group_card.Count())); return(cards); } else if (group_card.Count() + jokers.Count() > result.card_num) { //升一个星级 var cards = group_card.ToList(); cards.AddRange(jokers.GetRange(0, result.card_num - group_card.Count() + 1)); return(cards); } } else { //炸弹就行 var cards = group_card.ToList(); cards.AddRange(jokers.GetRange(0, 4 - group_card.Count())); return(cards); } } } } return(null); }
/// <summary> /// 连三张 /// </summary> /// <param name="cards"></param> /// <param name="result"></param> /// <returns></returns> public static bool CheckLineFullHouse(Games game_type, List <CardEnum> cards, out CombineResult result) { result = null; var cards_clone = cards.Clone(); if (cards_clone.Count >= 9 && cards_clone.Count % 3 == 0) { CardEnum min_card = cards_clone[0]; byte card_count = (byte)cards_clone.Count; int joker_count = 0; if (game_type.IsMetallic()) { joker_count = cards_clone.RemoveAll(c => Jokers.Contains(c)); } int count = cards_clone.Count; CardEnum next_card = cards_clone[0]; for (int i = 0; i < cards_clone.Count; i += 3) { if (i < count && cards_clone[i] == next_card) { if (i + 1 < count && cards_clone[i + 1] == next_card) { if (i + 2 < count && cards_clone[i + 2] == next_card) { next_card = NextCardNumber(next_card); } else if (joker_count >= 1) { joker_count -= 1; i -= 1; next_card = NextCardNumber(next_card); } else { return(false); } } else if (joker_count >= 2) { joker_count -= 2; i -= 2; next_card = NextCardNumber(next_card); } else { return(false); } } else if (joker_count >= 3) { joker_count -= 3; i -= 3; next_card = NextCardNumber(next_card); } else { return(false); } } result = new CombineResult(Combine.LineFullHouse, 0, min_card, card_count); return(true); } return(false); }
/// <summary> /// 找适合的组合 /// </summary> /// <param name="hand_cards"></param> /// <param name="result"></param> /// <returns></returns> private static List <CardEnum> FindCombine(Games game_type, List <CardEnum> hand_cards, CombineResult result) { var hand_cards_clone = hand_cards.Clone(); List <CardEnum> useful_cards = new List <CardEnum>(); List <CardEnum> jokers = new List <CardEnum>(); if (game_type.IsMetallic()) { jokers = hand_cards.FindAll(c => c == CardEnum.LittleJoker || c == CardEnum.ElderJoker); hand_cards_clone.RemoveAll(c => c == CardEnum.LittleJoker || c == CardEnum.ElderJoker); } foreach (var item in hand_cards_clone.FindAll(c => c > result.min_card).GroupBy(c => c)) { switch (result.combine) { case Combine.Bridge: //一张 if (item.Count() == 1) { useful_cards.AddRange(item); } break; case Combine.OnePairs: //一对 if (item.Count() == 2) { useful_cards.AddRange(item); } break; case Combine.LinePairs: //连对 if (item.Count() >= 2) { useful_cards.AddRange(item); } break; case Combine.FullHouse: //三张 if (item.Count() >= 3) { useful_cards.AddRange(item); } break; case Combine.LineFullHouse: //连三张 if (item.Count() >= 3) { useful_cards.AddRange(item); } break; case Combine.Straight: //顺子 if (item.Count() == 1) { useful_cards.AddRange(item); } break; } } //第一遍,去除大小王,选最佳匹配方式 if (useful_cards.Count < result.card_num) { //牌数量不够的情况下,直接加上大小王做匹配 useful_cards.AddRange(jokers); useful_cards = useful_cards.FindAll(c => c > result.min_card); if (useful_cards.Count < result.card_num) { goto ReCheck; } } var cards = CheckCombine(game_type, useful_cards, result); if (cards != null) { return(cards); } //第二遍,所有符合条件牌都加上 ReCheck: useful_cards = hand_cards.FindAll(c => c > result.min_card); if (useful_cards.Count < result.card_num) { return(null); } return(CheckCombine(game_type, useful_cards, result)); }