Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[C#] Poker hand evaluation

Discussion in 'Scripting' started by Mattiebo, Jul 19, 2013.

  1. Mattiebo

    Mattiebo

    Joined:
    Feb 11, 2013
    Posts:
    12
    Hello, I'm relatively new to programming/Unity. I've been using Unity with C# for probably around 2 months now and usually when I've been a bit stuck, I've searched the internet extensively until I could find a solution, but after spending a while looking for a solution to this, I thought it would be best to ask here for some assistance.

    I'm making a poker game. Last week I got over the initial hurdle of shuffling the deck using a solution provided by EliteMossy. Fortunately, I know how I'm going to do everything else in my game except for the hand evaluation. As my scripts so far are quite long, I won't post them in full (unless requested), but instead I'll explain exactly what's happening and what I need.

    The goal is to have a script find if a hand of seven cards (This is Texas Hold 'Em) contains any special hands such as flushes, straights or pairs. Once I work out a way to do a few of these, I should be able to handle the rest.

    • My cards are stored in an integer array called deckOfCards. This array holds 52 numbers from 0 - 51 and once the shuffle function has finished, each number is different (as in, there are no duplicated numbers).
    • Each number represents an individual card. 0 - 12 are the Spades cards, 13 - 25 are Clubs, 26 - 38 are Hearts and 39 - 51 are Diamonds. I have a grid which tells me which card fits which value and I can see that, for example, the Jack of Clubs is 23, the Five of Hearts is 30 and the Ten of Diamonds is 48.
    • Card graphics are stored in a seperate Texture array. These get partnered with numbers from the int array to visualise the cards.
    • This poker game is designed for two people, and so it's relatively easy to manage. Because of this, I have dealt the cards out in the following way; the first 3 cards (numbers in the deckOfCards array) are the flop, the 4th card is the turn, the 5th card is the river, the 6th and 7th cards go to player one and the 8th and 9th cards go to player two. It is very easy to store these in seperate variables for the hand evaluation if necesarry.

    So to summarise, I need a way to evaluate the hand of a player based on seven cards which are represented in code by individual numbers from 0 - 51.

    Thanks
     
  2. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Well you need some sort of rule set.

    Create a PokerHands class, which holds information of what a pair can be and then test it up against the hands int's.
    However you need a lot of check for forexample pairs and such with your in method.

    Maybe instead of only using ints, create a struct or class containing a number and enum like:

    Code (csharp):
    1.  
    2.  
    3. public enum CardType : byte
    4. {
    5.          Hearts = 0,
    6.          Spades = 1,
    7.          Diamonds = 2,
    8.          Clubs = 3
    9. }
    10.  
    11. public class Card
    12. {
    13.         public CardType Type { get; private set; }
    14.         public int CardNumber { get; private set; }
    15.  
    16.         public Card(int number, CardType type)
    17.         {
    18.             CardNumber = number;
    19.             Type = type;
    20.         }
    21. }
    22.  
    23.  
    Then create the deck in a simple double for-loop.

    This way there will be less checks. You just need to check if a hand, for example holds two equal card numbers for a pair, instead of all the combinations before.

    You could then store rule checks in a Rule class, with a method testing if a hand got the right combination and a number defining how good the hand is.

    Edit: The problem with checking for the pairs with ints is that a pair can be for example 2 AND 15 OR 28 OR 41. This check needs to be considered for all possible pairs...
     
    Last edited: Jul 19, 2013
  3. Mattiebo

    Mattiebo

    Joined:
    Feb 11, 2013
    Posts:
    12
    Thanks BFGames, I'll play around and see what I can do.
     
  4. Mattiebo

    Mattiebo

    Joined:
    Feb 11, 2013
    Posts:
    12
    Sorry for the bump, but this has been bugging me for a while.

    The msdn page for operators shows a logical OR ' | ' and a Conditional OR ' || ' which can be used within things like if statements. Is there a way to do something like this with integers? Essentially what I'd like to have is:

    if ( intVariable1 == 1 | 2 | 3 ) { }

    The error that comes up as a result of this is "error CS0019: Operator `|' cannot be applied to operands of type `bool' and `int'". Any help for a way to do this with integers would be greatly appreciated.
     
  5. Beennn

    Beennn

    Joined:
    Sep 11, 2010
    Posts:
    373
    Code (csharp):
    1. if ( intVariable1 == 1 || intVariable1 == 2 || intVariable1 == 3 || intVariable1 == 4 ) {
    2.  
    3.     //do something
    4.  
    5. }
    EDIT: My mistake.
     
    Last edited: Jul 20, 2013
  6. Mattiebo

    Mattiebo

    Joined:
    Feb 11, 2013
    Posts:
    12
    Ah, ok. Thanks for that Beennn!