private int JudgeStraight(List <SingleCard> realpokers) { var pokers = realpokers.ToList(); var count = 0; bool hasAce = false; if (pokers.Last().PokerSize == PokerSize.Max) { pokers.Insert(0, SingleCard.GetFakeAce()); } var lastIndex = 0; for (int i = 0; i < pokers.Count; i++) { if (i + 1 == pokers.Count) { break; } if (pokers[i].PokerSize == pokers[i + 1].PokerSize - 1) { count++; if (count >= 4) { if (hasAce) { lastIndex = i - 1; } else { lastIndex = i; } } continue; } count = 0; } return(lastIndex); }
public string Compute(FinalPoker finalPoker) { int sum_S = finalPoker.Pokers.Count(it => it.Suit == Suit.Spade); int sum_H = finalPoker.Pokers.Count(it => it.Suit == Suit.Heart); int sum_D = finalPoker.Pokers.Count(it => it.Suit == Suit.Diamond); int sum_C = finalPoker.Pokers.Count(it => it.Suit == Suit.Club); var singleCards = finalPoker.Pokers.OrderBy(it => it.PokerSize).GroupBy(it => it.PokerSize).Select(it => new SingleCard(it.Key, it.Count())).ToList(); try { if (singleCards.Count < 5) { //判断4条 var single = singleCards.FirstOrDefault(it => it.Count == 4); if (single != null) { var index = singleCards.IndexOf(single); SingleCard another; if (index == singleCards.Count - 1) { another = singleCards[index - 1]; } else { another = singleCards.Last(); } return("8." + single.Value + another.Value); } //三带二 single = singleCards.LastOrDefault(it => it.Count == 3); if (single != null) { SingleCard another = singleCards.LastOrDefault(it => it.Count >= 2 && it.PokerSize != single.PokerSize); if (another != null) { return("7." + single.Value + another.Value); } } } else { //同花 if (sum_S >= 5) { //同花顺 var index = JudgeStraightWithColor(finalPoker.Pokers.Where(it => it.Suit == Suit.Spade).ToList()); if (index > 0) { return("9." + index); } var pokers = finalPoker.Pokers.Where(it => it.Suit == Suit.Spade).TakeLast(5).ToList(); return("6." + pokers[4].Value + pokers[3].Value + pokers[2].Value + pokers[1].Value + pokers[0].Value); } if (sum_H >= 5) { //同花顺 var index = JudgeStraightWithColor(finalPoker.Pokers.Where(it => it.Suit == Suit.Heart).ToList()); if (index > 0) { return("9." + index); } var pokers = finalPoker.Pokers.Where(it => it.Suit == Suit.Heart).TakeLast(5).ToList(); return("6." + pokers[4].Value + pokers[3].Value + pokers[2].Value + pokers[1].Value + pokers[0].Value); } if (sum_D >= 5) { //同花顺 var index = JudgeStraightWithColor(finalPoker.Pokers.Where(it => it.Suit == Suit.Diamond).ToList()); if (index > 0) { return("9." + index); } var pokers = finalPoker.Pokers.Where(it => it.Suit == Suit.Diamond).TakeLast(5).ToList(); return("6." + pokers[4].Value + pokers[3].Value + pokers[2].Value + pokers[1].Value + pokers[0].Value); } if (sum_C >= 5) { //同花顺 var index = JudgeStraightWithColor(finalPoker.Pokers.Where(it => it.Suit == Suit.Club).ToList()); if (index > 0) { return("9." + index); } var pokers = finalPoker.Pokers.Where(it => it.Suit == Suit.Club).TakeLast(5).ToList(); return("6." + pokers[4].Value + pokers[3].Value + pokers[2].Value + pokers[1].Value + pokers[0].Value); } //顺子 if (singleCards.Count >= 5) { var index = JudgeStraight(singleCards.ToList()); if (index > 0) { return("5." + singleCards[index + 1].Value); } } //三条 var single = singleCards.LastOrDefault(it => it.Count == 3); if (single != null) { var another = singleCards.LastOrDefault(it => it.PokerSize != single.PokerSize); var another2 = singleCards.LastOrDefault(it => it.PokerSize != single.PokerSize && it.PokerSize != another.PokerSize); return("4." + single.Value + another.Value + another2.Value); } //两对 if (singleCards.Count == 5) { single = singleCards.LastOrDefault(it => it.Count == 2); var another = singleCards.LastOrDefault(it => it.Count == 2 && it.PokerSize != single.PokerSize); var another2 = singleCards.LastOrDefault(it => it.Count == 1); return("3." + single.Value + another.Value + another2.Value); } //一对 if (singleCards.Count == 6) { single = singleCards.LastOrDefault(it => it.Count == 2); var another = singleCards.LastOrDefault(it => it.Count == 1); var another2 = singleCards.LastOrDefault(it => it.Count == 1 && it.PokerSize != another.PokerSize); var another3 = singleCards.LastOrDefault(it => it.Count == 1 && it.PokerSize != another.PokerSize && it.PokerSize != another2.PokerSize); return("2." + single.Value + another.Value + another2.Value + another3.Value); } //高牌 return("1." + singleCards[6].Value + singleCards[5].Value + singleCards[4].Value + singleCards[3].Value + singleCards[2].Value + singleCards[1].Value + singleCards[0].Value); } } catch { return("0"); } return("0"); }