扑克牌分析
Poker Hand Analysing
我 运行 在为我的扑克游戏编写手牌分析器时遇到了一些麻烦。
截至目前,我可以分析每个玩家的手牌并获得所需的结果(TwoPair、OnePair、HighCard)
但我现在要做的是让拥有最高排名牌的玩家赢得比赛
For Example: Player 1 = Two Pair,
Player 2 = HighCard,
Player 3 = One Pair
Player 4 = Two Pair
取匹配度最高的玩家(玩家 1 和玩家 4)
玩家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 ; }
}
手牌结果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)
}
手规则枚举
public enum HandRules
{
RoyalFlush, StraightFlush, FourOfAKind, FullHouse, Flush, Straight, ThreeOfAKind, TwoPair, OnePair, HighCard
}
通过使用 linq (using System.Linq;
),并假设您将玩家保存在变量名称为 playerList;
的 List<Player>
集合中
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 处的枚举值代表最好的牌)
不过请不要忘记踢球者。我在您的实施中没有看到对踢球卡的支持。
根据 OP 中给出的详细信息和 Oguz 的回答评论,我相信以下内容应该对您有所帮助。
var winner = playerList.OrderBy(x=>x.HandResult.result)
.ThenByDescending(x=>x.Hand.Max())
.First();
我 运行 在为我的扑克游戏编写手牌分析器时遇到了一些麻烦。 截至目前,我可以分析每个玩家的手牌并获得所需的结果(TwoPair、OnePair、HighCard)
但我现在要做的是让拥有最高排名牌的玩家赢得比赛
For Example: Player 1 = Two Pair,
Player 2 = HighCard,
Player 3 = One Pair
Player 4 = Two Pair
取匹配度最高的玩家(玩家 1 和玩家 4)
玩家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 ; }
}
手牌结果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)
}
手规则枚举
public enum HandRules
{
RoyalFlush, StraightFlush, FourOfAKind, FullHouse, Flush, Straight, ThreeOfAKind, TwoPair, OnePair, HighCard
}
通过使用 linq (using System.Linq;
),并假设您将玩家保存在变量名称为 playerList;
List<Player>
集合中
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 处的枚举值代表最好的牌)
不过请不要忘记踢球者。我在您的实施中没有看到对踢球卡的支持。
根据 OP 中给出的详细信息和 Oguz 的回答评论,我相信以下内容应该对您有所帮助。
var winner = playerList.OrderBy(x=>x.HandResult.result)
.ThenByDescending(x=>x.Hand.Max())
.First();