C#扑克如何检查玩家是否有不同的手牌
本文关键字:是否 玩家 扑克 何检查 检查 | 更新日期: 2023-09-27 18:25:07
我正在开发一个Poker Texas Hold'Em应用程序。我有所有的组合,但我似乎找不到一种紧凑的方法来检查哪个玩家的组合最高,以及它的类型。以下是我的一些代码
void Rules(int current,int Power)
{
//checking if the player has any pair
{
Power = 1;
current = 1;
}
}
- 电流=手的类型(一对=1,两对=2。直齐=8)
- 力量=手的力量(一对平分=2……一对国王=13)
等等。我用每一个新的组合更新当前和力量,因为它们是虚空的参数,所以每个玩家都有自己的"当前"answers"力量",所以它们不会被搞砸。这就是我到目前为止所拥有的,我的问题是如何在不使用20-30个重复的if语句的情况下检查哪个玩家的手牌最高:
List <int> Arr = new List <int>();
void Rules(int current,int Power)
{
//checking if the player has any pair
{
Power = 1;
current = 1;
Arr.Add(Power);
Arr.Add(current);
}
}
但像这样,我不知道哪种类型属于哪种播放器,所以它没有用。我也尝试过字符串,但我无法那么容易地比较它们。我该怎么办?什么是正确的方法?
您可能需要为reach规则创建一个类,以封装逻辑。类似这样的东西:
public class Card
{
public string Name { get; private set; }
/* cards have a value of 2-14 */
public int Value { get; private set; }
}
public abstract class Rule()
{
public abstract string Name { get; }
/* hands are calculated in powers of 100, when the card value is added you will get something like 335 */
public abstract int Value { get; }
public abstract bool HasHand(IReadonlyList<Card> cards);
}
public class PairRule() : Rule
{
public override string Name
{
get { return "Pair"; }
}
public override int Value
{
get { return 100; }
}
public override bool HasHand(IReadonlyList<Card> cards)
{
/* implement rule here */
return Enumerable.Any(
from x in cards
group x by x.Value into g
where g.Count() == 2
select g
);
}
}
...
public class Player
{
public IReadonlyList<Card> Hand { get; private set; }
public int GetHandValue(IReadonlyList<Rule> rules)
{
/* get value of hand 100, 200, 300 etc. */
var handValue = Enumerable.Max(
from x in rules
where x.HasHand(Hand)
select x.Value
);
/* get value of cards */
var cardValue = Hand
.OrderByDescending(x => x.Value)
.Take(5)
.Sum();
return handValue + cardValue;
}
}
public class Pot
{
public int Value { get; private set; }
public IReadonlyList<Player> Players { get; private set; }
public IReadonlyList<Player> GetWinners(IReadonlyList<Rule> rules)
{
var playerHands = Enumerable.ToList(
from x in players
select new {
Player = x,
HandValue = x.GetHandValue(rules)
}
);
var maxHand = playerHands.Max(x => x.HandValue);
return Enumerable.ToList(
from x in playerHands
where x.HandValue == maxHand
select x.Player
);
}
}