Skip to main content
added 4 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Poker defining Defining what combination the user have in Poker

imI'm developing a c# pokerC# Poker app and iI currently use the modulus and the divide operator % , / and dividing by 4 because there are 4 cards of the same type but with different colors. The deck of cards for playing pokerPoker consists of 52 cards. I start counting them from 0. If we take the cards starting from aceAce up to kingKing, we will have 0-51 what. What we can do from here is assume that each Ace (0,1,2,3) has result when divided by 4  , 0 (0 /4 = 0, 1 / 4 = 0) so on. LikeWith this i, I get the current type of the card.

To determine the suit/color of the card  , I am using the %% operator because the clubs are 0-4-8-12-16. When we divide 0,4,8,12,16 by 4 (4 % 4) we get result 0. These are the Clubs and Diamonds: 1-5-9-13-17 and 1 % 4 = 1.

I dontdon't have any class for the card itself, so is there any better alternative to this  ?

Here'sHere are some of the methods how iI use to find specific combinations  :

straight[] contains the player's 2 cards and the five cards on the table.

Full HouseFullHouse method  :

Straight Method Straight method:

Also the UpdateHandUpdateHand method  :

Poker defining what combination the user have

im developing a c# poker app and i currently use the modulus and the divide operator % , / and dividing by 4 because there are 4 cards of the same type but with different colors. The deck of cards for playing poker consists of 52 cards. I start counting them from 0. If we take the cards starting from ace up to king we will have 0-51 what we can do from here is assume that each Ace (0,1,2,3) has result when divided by 4  , 0 (0 /4 = 0, 1 / 4 = 0) so on. Like this i get the current type of the card.

To determine the suit/color of the card  , I am using the % operator because the clubs are 0-4-8-12-16. When we divide 0,4,8,12,16 by 4 (4 % 4) we get result 0. These are the Clubs and Diamonds: 1-5-9-13-17 and 1 % 4 = 1.

I dont have any class for the card itself so is there any better alternative to this  ?

Here's some of the methods how i find specific combinations  :

straight[] contains the player's 2 cards and the five cards on the table

Full House method  :

Straight Method :

Also the UpdateHand method  :

Defining what combination the user have in Poker

I'm developing a C# Poker app and I currently use the modulus and the divide operator % , / and dividing by 4 because there are 4 cards of the same type but with different colors. The deck of cards for playing Poker consists of 52 cards. I start counting them from 0. If we take the cards starting from Ace up to King, we will have 0-51. What we can do from here is assume that each Ace (0,1,2,3) has result when divided by 4, 0 (0 /4 = 0, 1 / 4 = 0) so on. With this, I get the current type of the card.

To determine the suit/color of the card, I am using the % operator because the clubs are 0-4-8-12-16. When we divide 0,4,8,12,16 by 4 (4 % 4) we get result 0. These are the Clubs and Diamonds: 1-5-9-13-17 and 1 % 4 = 1.

I don't have any class for the card itself, so is there any better alternative to this?

Here are some of the methods I use to find specific combinations:

straight[] contains the player's 2 cards and the five cards on the table.

FullHouse method:

Straight method:

UpdateHand method:

rm tag from title
Link
svick
  • 24.3k
  • 4
  • 52
  • 89

C# Poker defining what combination the user have

added 4181 characters in body
Source Link
Denis
  • 8.5k
  • 5
  • 31
  • 76

Here's some of the methods how i find specific combinations :

public class Hand
{
    public enum Combinations
    {
        Default = -1,
        HighCard = -1,
        PairTable = 0,
        Pair = 1,
        TwoPair = 2,
        ThreeOfAKind = 3,
        Straight = 4,
        Flush = 5,
        FullHouse = 6,
        FourOfAKind = 7,
        StraightFlush = 8,
        RoyalFlush = 9            
    }
    public double Power { get; set; }
    public int Current { get; set; }
}

straight[] contains the player's 2 cards and the five cards on the table

    private UsersProperties RFourOfAKind(UsersProperties user, IReadOnlyList<int> straight)
    {
        const int localType = (int) Hand.Combinations.FourOfAKind;
        for (int j = 0; j <= 3; j++)
        {
            if (straight[j]/4 == straight[j + 1]/4 && straight[j]/4 == straight[j + 2]/4 &&
                straight[j]/4 == straight[j + 3]/4)
            {
                UpdateHand(user, localType, straight[j]/4*4);
            }
            if (straight[j]/4 == (int) Cards.Numbers.Ace && straight[j + 1]/4 == (int) Cards.Numbers.Ace &&
                straight[j + 2]/4 == (int) Cards.Numbers.Ace && straight[j + 3]/4 == (int) Cards.Numbers.Ace)
            {
                UpdateHand(user, localType, 13*4);
            }
        }
        return user;
    }

Full House method :

 private UsersProperties RFullHouse(UsersProperties user, int[] straight)
        {
            const int localType = (int) Hand.Combinations.FullHouse;
            for (int j = (int) Cards.Numbers.Ace; j <= (int) Cards.Numbers.King*4; j++)
            {
                int two = -1;
                int counterForThree = straight.Count(rep => rep/4 == j/4);
                if (counterForThree == 3)
                {
                    for (int k = (int) Cards.Numbers.Ace; k <= (int) Cards.Numbers.King*4; k++)
                    {
                        if (k/4 == j/4) continue;
                        int counterForTwo = straight.Count(rep => rep/4 == k/4);
                        if (counterForTwo != 2 || k/4 == two) continue;
                        two = k/4;
                        if (j == 0)
                        {
                            UpdateHand(user, localType, 13*3 + (k/4)*2);
                        }
                        else if (k == 0)
                        {
                            UpdateHand(user, localType, j/4*3 + 13*2);
                        }
                    }
                }
            }
            return user;
        }

Straight Method :

        private UsersProperties RStraight(UsersProperties user, IEnumerable<int> straight)
    {
        const int localType = (int) Hand.Combinations.Straight;
        int[] op = straight.Select(o => o/4).Distinct().ToArray();
        for (int j = 0; j < op.Length - 4; j++)
        {
            if (op[j] + 1 == op[j + 1] && op[j] + 2 == op[j + 2] && op[j] + 3 == op[j + 3] && op[j] + 4 == op[j + 4])
            {
                UpdateHand(user, localType, op.Max() - 4 == op[j] ? op.Max() : op[j + 4]);
            }
            if (op[j] != (int) Cards.Numbers.Ace || op[j + 1] != (int) Cards.Numbers.Ten ||
                op[j + 2] != (int) Cards.Numbers.Jack || op[j + 3] != (int) Cards.Numbers.Queen ||
                op[j + 4] != (int) Cards.Numbers.King) continue;

            UpdateHand(user, localType,13);
        }
        return user;
     }

Also the UpdateHand method :

private void UpdateHand(UsersProperties user, int combinationType, int powerType)
    {
        user.Type = combinationType;
        user.Power = powerType + user.Type * 100;
        _win.Add(new Hand { Power = user.Power, Current = user.Type });
    }

Here's some of the methods how i find specific combinations :

public class Hand
{
    public enum Combinations
    {
        Default = -1,
        HighCard = -1,
        PairTable = 0,
        Pair = 1,
        TwoPair = 2,
        ThreeOfAKind = 3,
        Straight = 4,
        Flush = 5,
        FullHouse = 6,
        FourOfAKind = 7,
        StraightFlush = 8,
        RoyalFlush = 9            
    }
    public double Power { get; set; }
    public int Current { get; set; }
}

straight[] contains the player's 2 cards and the five cards on the table

    private UsersProperties RFourOfAKind(UsersProperties user, IReadOnlyList<int> straight)
    {
        const int localType = (int) Hand.Combinations.FourOfAKind;
        for (int j = 0; j <= 3; j++)
        {
            if (straight[j]/4 == straight[j + 1]/4 && straight[j]/4 == straight[j + 2]/4 &&
                straight[j]/4 == straight[j + 3]/4)
            {
                UpdateHand(user, localType, straight[j]/4*4);
            }
            if (straight[j]/4 == (int) Cards.Numbers.Ace && straight[j + 1]/4 == (int) Cards.Numbers.Ace &&
                straight[j + 2]/4 == (int) Cards.Numbers.Ace && straight[j + 3]/4 == (int) Cards.Numbers.Ace)
            {
                UpdateHand(user, localType, 13*4);
            }
        }
        return user;
    }

Full House method :

 private UsersProperties RFullHouse(UsersProperties user, int[] straight)
        {
            const int localType = (int) Hand.Combinations.FullHouse;
            for (int j = (int) Cards.Numbers.Ace; j <= (int) Cards.Numbers.King*4; j++)
            {
                int two = -1;
                int counterForThree = straight.Count(rep => rep/4 == j/4);
                if (counterForThree == 3)
                {
                    for (int k = (int) Cards.Numbers.Ace; k <= (int) Cards.Numbers.King*4; k++)
                    {
                        if (k/4 == j/4) continue;
                        int counterForTwo = straight.Count(rep => rep/4 == k/4);
                        if (counterForTwo != 2 || k/4 == two) continue;
                        two = k/4;
                        if (j == 0)
                        {
                            UpdateHand(user, localType, 13*3 + (k/4)*2);
                        }
                        else if (k == 0)
                        {
                            UpdateHand(user, localType, j/4*3 + 13*2);
                        }
                    }
                }
            }
            return user;
        }

Straight Method :

        private UsersProperties RStraight(UsersProperties user, IEnumerable<int> straight)
    {
        const int localType = (int) Hand.Combinations.Straight;
        int[] op = straight.Select(o => o/4).Distinct().ToArray();
        for (int j = 0; j < op.Length - 4; j++)
        {
            if (op[j] + 1 == op[j + 1] && op[j] + 2 == op[j + 2] && op[j] + 3 == op[j + 3] && op[j] + 4 == op[j + 4])
            {
                UpdateHand(user, localType, op.Max() - 4 == op[j] ? op.Max() : op[j + 4]);
            }
            if (op[j] != (int) Cards.Numbers.Ace || op[j + 1] != (int) Cards.Numbers.Ten ||
                op[j + 2] != (int) Cards.Numbers.Jack || op[j + 3] != (int) Cards.Numbers.Queen ||
                op[j + 4] != (int) Cards.Numbers.King) continue;

            UpdateHand(user, localType,13);
        }
        return user;
     }

Also the UpdateHand method :

private void UpdateHand(UsersProperties user, int combinationType, int powerType)
    {
        user.Type = combinationType;
        user.Power = powerType + user.Type * 100;
        _win.Add(new Hand { Power = user.Power, Current = user.Type });
    }
Source Link
Denis
  • 8.5k
  • 5
  • 31
  • 76
Loading