Interesting read. Sadly I'd already written the base for my own so I'm just sticking with it for now.
Here's some info incase people are interested.
First, I define a score for each type of hand.
Code:
public enum HandType
{
HighCard = 0,
Pair = 100,
TwoPairs = 200,
ThreeOfAKind = 300,
Straight = 500,
Flush = 600,
FullHouse = 750,
FourOfAKind = 1000,
StraightFlush = 2000,
RoyalFlush = 5000
}
I then need a method to return the type of hand I'm holding. This is where I implement my lovely helper methods. The helpers can be written any way you like.
NOTE: My version is using 5 card draw. Only change for texas hold-em is when evaluating, you'd add your current hand to the community cards and need to change a few checks (it's rather simple to check for full house when you only have 5 cards cause well... you're only holding 2 types of cards lol) It's simple changes but obviously this is just sample code and ideas for people.
Code:
public HandType EvaluateHandType()
{
//This lets me know how many of 1 card I have (4 of a kind, 3 of a kind or at least 1 pair)
int maxOfoneCard = MaxOfOneVal();
HandType type = HandType.HighCard;
if (isFlush())
{
if (isStraight())
if (GetHighCardValue() == CardValue.Ace)//If high card is ace, it's a straight and a flush then it's royal!
type = HandType.RoyalFlush;
else//Not ace high so it's just straight flush
type = HandType.StraightFlush;
else//Normal flush, so sad
type = HandType.Flush;
}
else if (maxOfoneCard == 4)//We have 4 of the same card value being held so 4 of a kind!
type = HandType.FourOfAKind;
else if (isFullHouse())//This helper checks # of types of cards we're holding. If 2 then we know fullhouse
type = HandType.FullHouse;
else if (isStraight())//Checks range of cards between HighCard and LowCard. Also checks max # of one type. Must be 1 (no duplicate cards)
type = HandType.Straight;
else if (maxOfoneCard == 3)//3 of a kind, simple
type = HandType.ThreeOfAKind;
else if (numOfCardValues() == 3)//We only have 3 card values total... therefor must be 3 of a kind or 2 pair. We already checked 3 of a kind though
type = HandType.TwoPairs;
else if (maxOfoneCard == 2)//Pair, simple.
type = HandType.Pair;
return type;
}
Now for the actual scoring cause sometimes you'll have people with the same hand... These are determined by high card. Certain hands may require a second high card to break that tie.
Code:
public int EvaluateScore()
{
HandType typeOfHand = EvaluateHandType();
int score = Convert.ToInt32(typeOfHand) + Convert.ToInt32(GetHighCardValue())*2;
//We only need to add second card value in certain cases.
if (typeOfHand == HandType.HighCard||
typeOfHand == HandType.Flush||
typeOfHand == HandType.TwoPairs||
typeOfHand == HandType.Pair))
score += SecondHighestCardValueAsInteger();
return score;
}
If I was re-writing this I'd probably use constants versus enums. Converting back to integers is not as nice as I'd like. It works though and I've tested for every hand type. Now comes the actual gameplay side of things
[Only registered and activated users can see links. Click Here To Register...]
<edit> as you can see, I'm taking Pair score (100) and adding the second high card value (My card values start at 0 for indexing purposes so it's 1 less score then you might expect)