0

I'm developing a Poker Texas Hold'Em app I have all the combinations working but i cant seem to find a compact way to check which player has the highest combination and the type of it here's some of my code

void Rules(int current,int Power)
{
     //checking if the player has any pair
     {
         Power = 1;
         current = 1;
     }
}
  • current = the type of the hand (pair=1,two pair =2..straight flush=8)
  • Power = the power of the hand (pair of deuces=2....pair of kings=13)

and so on.. I'm updating current and Power with each new combination and since they are parameters of the void each player has his own "current" and "Power" so they don't get messed up.This is what i have so far and my question is how do i check which player has the highest hand without using 20-30 repetitive if statements i was thinking about :

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

but like this i have no idea which type belongs to which player so it's not use i also tried with strings but than i wont be able to compare them that easily.What should i do ? What's the right approach ?

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
  • You should make a comparison method on your `Player` class and use that to compare players. – CompuChip Nov 30 '15 at 13:55
  • 1
    Use classes and enums instead of ints. Then you could use a `Dictionary` or something similar. – Tim Schmelter Nov 30 '15 at 13:55
  • 2
    I don't think there's sufficient info / code posted to answer your question, but I also don't know how to play poker so maybe I'm just missing something. But if you just have a single `List` to track all players hand values, then you're going to have a problem obviously. Why not store hand values in your `Player` class? Then the `Rules` class can just check and compare `Player.Power` or `Player.Current`, whatever the correct value is. – sab669 Nov 30 '15 at 13:56
  • What info should i provide ? There's a lot of code in Rules which is just checking for all the different combinations so i removed it and not doing anything different to current and Power which are the 2 variables i need currently –  Nov 30 '15 at 14:00
  • 1
    As said by others, you are not providing enough information. But here you have an in principle good configuration for what you are looking for: a class storing all the hand information (e.g., cards, player, power, etc.); a collection for all the hands (a list seems fine because its number is low enough); you can then perform all the actions you wish in this collection in many ways. For the simpler actions (like ordering) you migh rely on LINQ methods and do something like: `hands = hands.OrderBy(x => x.Power).ToList();`. Separating hand properties doesn't seem a good idea here. – varocarbas Nov 30 '15 at 14:02
  • Are you asking how to determine what each player's hand is based on their 2 hold cards and the community cards or a way to rank different poker hands? – juharr Nov 30 '15 at 14:02
  • I'm asking for a way to determine different rank's –  Nov 30 '15 at 14:03
  • So your `current` is a value that indicates high card to straight flush and `power` is the strength of the type of hand when compared to another of the same type. So if you have calculated both for each player then all you need to do is compare first on `current` and use `power` for tie breakers. Alternatively you could give a numerical rank for every possible 5 card hand to do a single comparison. – juharr Nov 30 '15 at 14:08
  • That's exactly what i have but i i will have to do if(player1current==player2current)..if(player1current==player6current) and after this do the same for all the other players so it's not really to do it like this or i misunderstood your comment ? –  Nov 30 '15 at 14:11
  • OK, I think I see what you want. In that case you can use Linq. If you have a list of players just use `OrderBy` to order them from best hand to worst and take the first. `players.OrderByDescending(p=>p.current).ThenByDescending(p=>p.power).First()` – juharr Nov 30 '15 at 14:15
  • do i have to use enum for this to work ? –  Nov 30 '15 at 14:18
  • @Assim No, it will work for `int`. Using an `enum` would be more for readability than anything else. – juharr Nov 30 '15 at 14:19
  • Okay this look's like a solution to my problem but 1 last question what do i replace current and Power with ? it gives me an error http://prntscr.com/98osqj (it's the same if i use current) sorry if it's a newbie question –  Nov 30 '15 at 14:23

1 Answers1

2

You might want to make a class for reach rule, to encapsulate the logic. Something like this:

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
          );
     }
}
Master Morality
  • 5,837
  • 6
  • 31
  • 43