简体   繁体   English

扑克手分析

[英]Poker Hand Analysing

I have run into a bit of trouble on coding a hand analyser for my poker game.我在为我的扑克游戏编写手牌分析器时遇到了一些麻烦。 As of now i can analyse each players hand and get the desired result (TwoPair,OnePair,HighCard)截至目前,我可以分析每个玩家的手牌并获得所需的结果(TwoPair,OnePair,HighCard)

But what i want to do now is get the player with the highest ranking cards to win the game但我现在想做的是让排名最高的玩家赢得比赛

For Example: Player 1 = Two Pair, 
         Player 2 = HighCard,
         Player 3 = One Pair
         Player 4 = Two Pair

Take the players with the highest match (player 1 and player 4)拿下匹配度最高的玩家(玩家 1 和玩家 4)

Player Class播放器 Class

 public class Player :  IPlayer
{

    public Player(string name)
    {
        Name = name;
    }


    public string Name { get;  private set; }
    // Hold max 2 cards
    public Card[] Hand { get; set; }
    public HandResult HandResult { get ; set ; }
}

Hand Result Class手牌结果 Class

  public class HandResult
{
    public HandResult(IEnumerable<Card> cards, HandRules handRules)
    {
        result = handRules;
        Cards = cards;
    }

    public PokerGame.Domain.Rules.HandRules result { get; set; }

    public IEnumerable<Card> Cards { get; set; }// The cards the provide the result (sortof backlog)
}

Hand Rule Enum手规则枚举

    public enum HandRules 
{ 
    RoyalFlush, StraightFlush, FourOfAKind, FullHouse, Flush, Straight, ThreeOfAKind, TwoPair, OnePair, HighCard 
}

By using linq ( using System.Linq; ), and assuming you keep the players in a List<Player> collection with the variable name playerList;通过使用 linq ( using System.Linq; ),并假设您将播放器保存在List<Player>集合中,变量名为 playerList;

Player[] winners = playerList
    .Where(x => (int)x.HandResult.result == (int)playerList
        .Min(y => y.HandResult.result)
    ).ToArray();

Or, for clarity:或者,为了清楚起见:

int bestScore = playerList.Min(x => (int)x.HandResult.result);

Player[] winners = playerList
    .Where(x => (int)x.HandResult.result == bestScore).ToArray();

This will give you the player(s) whose hand score is equal to the maximum score achieved by any of them.这将为您提供手牌得分等于其中任何人获得的最高得分的玩家。

We are using.Min() here (instead of.Max()) because your enumeration (HandRules) is in reverse order.我们在这里使用.Min()(而不是.Max()),因为您的枚举(HandRules)是相反的顺序。 (enum value at index 0 represents the best hand) (索引 0 处的枚举值代表最佳手牌)

Please don't forget the kicker though.请不要忘记踢球者。 I don't see support for the kicker card in your implementation.我在您的实施中看不到对踢球卡的支持。

Based on details given in OP and comments for answer by Oguz, i believe following should help you.根据 OP 中给出的详细信息和 Oguz 回答的评论,我相信以下内容应该对您有所帮助。

var winner = playerList.OrderBy(x=>x.HandResult.result)
                       .ThenByDescending(x=>x.Hand.Max())
                       .First();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM