【问题标题】:Poker Hand Analysing扑克手分析
【发布时间】:2020-04-26 23:37:33
【问题描述】:

我在为我的扑克游戏编写手牌分析器时遇到了一些麻烦。 截至目前,我可以分析每个玩家的手牌并获得所需的结果(TwoPair,OnePair,HighCard)

但我现在想做的是让拥有最高排名牌的玩家赢得比赛

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

选择匹配度最高的玩家(玩家 1 和玩家 4)

播放器类

 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 ; }
}

手结果类

  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)
}

手规则枚举

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

【问题讨论】:

  • 您的问题是什么?你有没有尝试过任何事情来实现你的既定目标?
  • 你好培根。我有游戏的完整代码我的问题是如何从玩家对象列表中获得手牌最高的玩家

标签: c# arrays linq sorting poker


【解决方案1】:

通过使用 linq (using System.Linq;),并假设您将玩家保存在带有变量名 playerList 的 List&lt;Player&gt; 集合中;

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

或者,为了清楚起见:

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

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

这将为您提供手牌得分等于其中任何人获得的最高得分的玩家。

我们在这里使用 .Min()(而不是 .Max()),因为您的枚举 (HandRules) 顺序相反。 (索引 0 处的枚举值代表最好的牌)

请不要忘记踢球者。我在您的实现中看不到对踢球卡的支持。

【讨论】:

  • 我似乎总是让所有玩家回来(所有玩家都有枚举状态高卡)但它应该只返回具有最高卡值的玩家
  • 您需要更改 HandResult 以不仅包含手牌的类型(一对、两对等),还包含有问题的牌(比如这对牌是什么等级,或者是什么牌)顺子的最大牌等)。也请不要忘记踢球者。如果两名玩家有 AAKKJ 和 AAKK8,则 J 是踢球者。
  • 我建议计算一手牌(包括起脚牌)的总分并进行比较。您可以为手牌类型设置基值(高牌基数:0x0,对子基数:0x1000 等,并添加诸如等级 * 0x100 之类的牌,并将诸如 0x2 之类的踢球者添加到 0xD)
  • 我似乎总是让所有玩家回来(所有玩家都有枚举状态 High Card)但它应该只返回具有最高卡值的玩家(对象 Card[] 持有卡值) 现在我总是得到枚举值 9 = 高卡。公共 IEnumerable Cards { 获取;放; }// 提供结果的卡片(排序积压)
【解决方案2】:

根据 Oguz 在 OP 和 cmets 中给出的详细信息,我相信以下内容应该对您有所帮助。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2011-05-09
    相关资源
    最近更新 更多