-1

I have run into a bit of trouble on coding a hand analyser for my poker game. As of now i can analyse each players hand and get the desired result (TwoPair,OnePair,HighCard)

But what i want to do now is get the player with the highest ranking cards to win the game

For Example: Player 1 = Two Pair, 
         Player 2 = HighCard,
         Player 3 = One Pair
         Player 4 = Two Pair

Take the players with the highest match (player 1 and player 4)

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

Hand Result 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)
}

Hand Rule Enum

    public enum HandRules 
{ 
    RoyalFlush, StraightFlush, FourOfAKind, FullHouse, Flush, Straight, ThreeOfAKind, TwoPair, OnePair, HighCard 
}
Teckniel
  • 149
  • 1
  • 1
  • 11

2 Answers2

0

By using linq (using System.Linq;), and assuming you keep the players in a List<Player> collection with the variable name playerList;

Player[] winners = playerList
    .Where(x => (int)x.HandResult.result == (int)playerList
        .Min(y => y.HandResult.result)
    ).ToArray();

Or, for clarity:

int bestScore = playerList.Min(x => (int)x.HandResult.result);

Player[] winners = playerList
    .Where(x => (int)x.HandResult.result == bestScore).ToArray();

This will give you the player(s) whose hand score is equal to the maximum score achieved by any of them.

We are using .Min() here (instead of .Max()) because your enumeration (HandRules) is in reverse order. (enum value at index 0 represents the best hand)

Please don't forget the kicker though. I don't see support for the kicker card in your implementation.

Oguz Ozgul
  • 6,809
  • 1
  • 14
  • 26
  • I always seem to get all the players back(All players have the enum state High Card) but it should only return the player back with the highest card value – Teckniel Apr 27 '20 at 01:02
  • You need to change the HandResult to contain not only the type of the hand (one pair, two pairs etc.) but the cards in question (like what rank the pair is, or, what is the max card of a straight etc). And please also don't forget the kicker. If two players have AAKKJ and AAKK8, J is the kicker. – Oguz Ozgul Apr 27 '20 at 01:09
  • 1
    I recommend calculating an overall score for the hand (including the kicker) and just to compare this. You can have base values for hand types (high card base: 0x0, pair base: 0x1000 etc, and add the cards like rank * 0x100, and add kicker like 0x2 to 0xD) – Oguz Ozgul Apr 27 '20 at 01:12
  • I always seem to get all the players back(All players have the enum state High Card) but it should only return the player back with the highest card value ( The Object Card[] holds the card value) Now i always get enum value 9 = High Card. public IEnumerable Cards { get; set; }// The cards the provide the result (sortof backlog) – Teckniel Apr 27 '20 at 01:14
0

Based on details given in OP and comments for answer by Oguz, i believe following should help you.

var winner = playerList.OrderBy(x=>x.HandResult.result)
                       .ThenByDescending(x=>x.Hand.Max())
                       .First();
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51