C#Poker如何检查球员是否有不同的牌

时间:2015-11-30 13:53:40

标签: c# arrays poker

我正在开发一个德州扑克扑克应用程序我已经完成了所有组合工作,但我似乎无法找到一种简洁的方法来检查哪个玩家具有最高的组合及其类型在这里'我的一些代码

void Rules(int current,int Power)
{
     //checking if the player has any pair
     {
         Power = 1;
         current = 1;
     }
}
  • current =手的类型(对= 1,两对= 2 ......直接冲洗= 8)
  • 力量=手的力量(一对双手= 2 ......一对国王= 13)

等等。我用每个新组合更新当前和力量,因为它们是虚空的参数,每个玩家都拥有自己的"当前"和" Power"所以他们不会搞砸了。这就是我到目前为止的问题,我的问题是如何在没有使用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);
    }
}

但是像这样我不知道哪种类型属于哪个播放器所以它不使用我也尝试过使用字符串但是我不能轻易地比较它们。我该怎么办?什么是正确的方法?

1 个答案:

答案 0 :(得分:2)

您可能希望为覆盖率规则创建一个类,以封装逻辑。像这样:

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