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