如何比较列表中元素的枚举?

时间:2014-06-10 05:58:37

标签: c# enums

我必须使用测试驱动开发实现扑克游戏。现在我必须检查给定的手是否包含同花顺。所以我必须检查每张卡的枚举是否大于前一张卡。让我展示一些代码。

public bool IsStraightFlush(IHand hand)
    {
        var sortedCards = hand.Cards.OrderBy(card => card.Face).ThenBy(card => card.Suit);

    }

每张卡都是通过两个枚举参数声明的:public Card(CardFace face, CardSuit suit) CardFace和CardSuits是枚举。 这就是我现在写的内容 - 我将给定的手分类为由五张牌组成的参数。我首先按面部订购,然后再订购。现在我必须知道如何检查手的下一张牌是否具有CardFace枚举+ 1而不是当前的牌。这是CardFace的枚举。

 public enum CardFace
{
    Two = 2,
    Three = 3,
    Four = 4,
    Five = 5,
    Six = 6,
    Seven = 7,
    Eight = 8,
    Nine = 9,
    Ten = 10,
    Jack = 11,
    Queen = 12,
    King = 13,
    Ace = 14
}

3 个答案:

答案 0 :(得分:0)

我希望这会奏效:

public bool IsStraightFlush(IHand hand)
    {
        var sortedCards = hand.Cards.OrderBy(card => card.Face).ThenBy(card => card.Suit).ToList();
        bool straightFlush = true;
        for (int i = 1; i < sortedCards.Count(); i++)
        {
            if ((int)sortedCards[i].Face != (int)sortedCards[i - 1].Face + 1)
            {
                straightFlush = false;
                break;
            }
        }
        return straightFlush;
    }

答案 1 :(得分:0)

这可能对您有所帮助,将序列转换为int,然后检查next element -1是否等于当前:

    static bool CheckForSequence(List<int> input)
    {
        input.Sort();
        var result = true;
        for (int i = 0; i < input.Count - 1; i++)
        {
            result = input[i] == input[i + 1] - 1;
            if (!result)
                break;
        }
        return result;
    }

还有一个解决方案,你仍需要施展ti int,但它要短得多:

    static bool CheckForSequence(List<int> input)
    {
        return !input.OrderBy(p => p).Select((p, i) => p - i).Distinct().Skip(1).Any();
    }

在这种情况下,如果集合顺序,则项目和索引之间的差异对于所有元素都是相同的。

答案 2 :(得分:0)

尝试使用group by,然后检查是否有一个组,其中的所有车辆都是顺序的。

public bool IsStraightFlush(IHand hand)
{
   var sortedCards = from c in hand.Cards
                     group c by c.Suit into d             
                     select new
                     {
                        Suit = d.Key,
                        Cards = d.OrderBy(x => x.Face)
                     };

    // all cards are the same suit              
    if(sortedCards.Count() == 1)
    {
        Card previousCard = null;
        foreach (var card in sortedCards.First().Cards)
        {
            if(previousCard != null && (card.Face - previousCard.Face > 1))
            {
                return false;
            }

            previousCard = card;
        }

        return true;
    }

   return false;
}

void Main()
{
    var hand = new Hand 
    {
        Cards = new List<Card>
        {
            new Card { Face = CardFace.Two, Suit = CardSuit.Clubs },
            new Card { Face = CardFace.Three, Suit = CardSuit.Clubs },
            new Card { Face = CardFace.Four, Suit = CardSuit.Clubs },
            new Card { Face = CardFace.Five, Suit = CardSuit.Clubs },
            new Card { Face = CardFace.Six, Suit = CardSuit.Clubs },
        }
    };

    Console.WriteLine(IsStraightFlush(hand));

}