c#直接检查扑克

时间:2016-09-22 11:37:53

标签: c# poker

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

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

要记住的另一件事是,只有当玩家有5或10时才能进行直线。

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

public bool CheckPair(List<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> cards)
{
    // check if trips and pair is true
    return CheckPair(cards) && CheckTrips(cards);
}
public bool CheckQuads(List<Card> 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<Card> cards)
{
    // check if flush and straight are true.
    return CheckFlush(cards) && CheckStraight(cards);
}

4 个答案:

答案 0 :(得分:2)

这可能不是表现最佳的支票,但我会说它非常易读,通常是一个很好的财产。

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

public bool CheckStraight(List<Card> 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<Card> 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;
}

答案 1 :(得分:0)

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

public bool CheckStraight(List<Card> 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<Card> cards)
{
    return cards.GroupBy(card => card.Value).Count() == cards.Count() && cards.Max(card => (int)card.Value) - cards.Min(card => (int)card.Value) == 4;
    }

答案 2 :(得分:0)

我无法想到真正的一个班轮,因为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,我已经修复了代码)

答案 3 :(得分:0)

ERM,

function bool IsStraight(IEnumerable<int> 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;
}