如何在纸牌游戏中检测“手” [关闭]How to detect “Hand” in a card drawing game [closed]

• 它叫什么名字。 即：“四种”，“三种”等等。
• 考虑到这是完美的随机性，那只手的概率是多少。
• 我可以选择数据结构
• 我用C＃

3 个回复3

#3楼 票数：1 已采纳

enum Suit { Spades, Hearts, Diamonds, Clubs }
enum Rank { Two, Three, Four, Five, Six, Seven,
Eight, Nine, Ten, Jack, Queen, King, Ace }

enum HandType { Nothing, Pair, TwoPair, ThreeOfAKind, Straight,
Flush, FullHouse, FourOfAKind, StraightFlush }

public struct Card
{
public Suit Suit { get; private set; }
public Rank Rank { get; private set; }
public Card(Rank rank, Suit suit)
{
Suit = suit;
Rank = rank;
}
}

public class Hand
{
public IReadOnlyList<Card> Cards { get; private set };
public Hand(IEnumerable<Card> cards)
{
this.Cards = cards.OrderByDescending(c => c.Rank).ToList();
}
}

public static class HandEvaluator
{
public static HandType GetHandType(Hand hand)
{
bool isFlush = hand.Cards.Distinct(c => c.Suit).Count() == 1;
bool isStraight = // five consecutive ranks. Mind the wheel straight Ace->5!
if (isStraight && isFlush)
return HandType.StraightFlush;

// group cards by rank, then find the group count & sizes
var groups = hand.Cards.GroupBy(c => c.Rank);

if (/* max group size = 4 */)
return HandType.FourOfAKind;
if (/* two groups and max group size = 3 */)
return HandType.FullHouse;
if (isFlush)
return HandType.Flush;
if (isStraight)
return HandType.Straight;
if (/* three groups and max group size 3 */)
return HandType.ThreeOfAKind;

// ... And so on for two pair, pair.

return HandType.Nothing;
}
}

1回复

3回复

1回复

3回复

7回复

3回复