我在为扑克游戏编写手动分析器时遇到了一些麻烦。截至目前,我可以分析每位玩家的手牌并获得所需的结果(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
}
[通过使用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();