c#直接检查扑克

我正在尝试用c#编写一个扑克手评估方法。 我已经成功地使用linq为每个扑克手做了这个,除了直线。 对于那些不玩直线的人,每张卡由5张卡组成,增量为1。 Ace可以高或低。

我创建了一个名为card的对象,它具有套装,等级和值(J = 11,Q = 12等等)。 我的方法将传递一个包含7张卡(孔卡和棋盘)的对象列表。

另外要记住的是,如果玩家有5或10,则只能进行直线操作。

请参阅下面我的其他扑克手的方法,如果您对直接方法有所了解,请告诉我。 伪代码也没关系。


public bool CheckPair(List cards) { //see if exactly 2 cards card the same rank. return cards.GroupBy(card => card.Rank).Count(group => group.Count() == 2) == 1; } public bool CheckTwoPair(List cards) { //see if there are 2 lots of exactly 2 cards card the same rank. return cards.GroupBy(card => card.Rank).Count(group => group.Count() >= 2) == 2; } public bool CheckTrips(List cards) { //see if exactly 3 cards card the same rank. return cards.GroupBy(card => card.Rank).Any(group => group.Count() == 3); } public bool CheckStraight(List cards) { // order by decending to see order var cardsInOrder = cards.OrderByDescending(a => a.Value).ToList(); // check for ace as can be high and low if (cardsInOrder.First().Rank == "A") { // check if straight with ace has has 2 values bool highStraight = cards.Where(a => a.Rank == "K" || a.Rank == "Q" || a.Rank == "J" || a.Rank == "10").Count() == 4; bool lowStraight = cards.Where(a => a.Rank == "2" || a.Rank == "3" || a.Rank == "4" || a.Rank == "5").Count() == 4; // return true if straight with ace if (lowStraight == true || highStraight == true) { return true; } } else { // check for straight here return true; } // no straight if reached here. return false; } public bool CheckFlush(List cards) { //see if 5 or more cards card the same rank. return cards.GroupBy(card => card.Suit).Count(group => group.Count() >= 5) == 1; } public bool CheckFullHouse(List cards) { // check if trips and pair is true return CheckPair(cards) && CheckTrips(cards); } public bool CheckQuads(List cards) { //see if exactly 4 cards card the same rank. return cards.GroupBy(card => card.Rank).Any(group => group.Count() == 4); } // need to check same 5 cards public bool CheckStraightFlush(List cards) { // check if flush and straight are true. return CheckFlush(cards) && CheckStraight(cards); } 

这可能不是性能最好的检查,但我会说它非常易读,通常是一个很好的属性。

只需抓住5张牌,跳过你每次迭代已经看过的牌,并检查每个序列的直线。 如果有序序列不包含双精度且第一张和最后一张牌的差值为5,则该序列是直的。

 public bool CheckStraight(List cards) { //maybe check 5 and 10 here first for performance var ordered = cards.OrderByDescending(a => a.Value).ToList(); for(i = 0; i < ordered.Count - 5; i++) { var skipped = ordered.Skip(i); var possibleStraight = skipped.Take(5); if(IsStraight(possibleStraight)) { return true; } } return false; } public bool IsStraight(List fiveOrderedCards) { var doubles = cards.GroupBy(card => card.Rank).Count(group => group.Count() > 1); var inARow = cards[4] - cards[0] = 5; //Ace is 0 return !doubles && inARow; } 

我对Glubus的答案做了一些小改动。 下面的代码可以完成这项工作,但您必须手动检查车轮(A,1,2,3,4,5)。

 public bool CheckStraight(List cards) { //maybe check 5 and 10 here first for performance var ordered = cards.OrderByDescending(a => a.Value).ToList(); for (var i = 0; i < ordered.Count - 4; i++) { var skipped = ordered.Skip(i); var possibleStraight = skipped.Take(5).ToList(); if (IsStraight(possibleStraight)) { return true; } } return false; } public bool IsStraight(List cards) { return cards.GroupBy(card => card.Value).Count() == cards.Count() && cards.Max(card => (int)card.Value) - cards.Min(card => (int)card.Value) == 4; } 

呃,

 function bool IsStraight(IEnumerable cards) { var orderedCards = cards.OrderBy(n => n).ToList(); var test = orderdCards.Zip(orderdCards.Skip(1), (a, b) => b - a); var count = 0; foreach(var n in test) { if (n == 1) { count++; if (count == 4) { return true; } } else { count = 0; } } return false; } 

我不能想到真正的一个class轮,因为A可以是1或14,但这应该是好的:

 int c = 5; // How many cards straight bool Ais1 = cards.OrderBy(a => a.Value).Select((i,j) => i.Value-j).Distinct().Skip(1).Count() <= (cards.Count - c); bool Ais14 = cards.OrderBy(a => (a.Value == 1 ? 14 : a.Value)).Select((i,j) => (i.Value == 1 ? 14 : i.Value)-j).Distinct().Skip(1).Count() <= (cards.Count - c); return Ais1 || Ais14; 

(更新 - 谢谢Janne,我已经修复了代码)