|
You last visited: Today at 15:44
Advertisement
Poker hand evaluator
Discussion on Poker hand evaluator within the CO2 Private Server forum part of the Conquer Online 2 category.
05/18/2012, 17:33
|
#1
|
elite*gold: 0
Join Date: Aug 2010
Posts: 992
Received Thanks: 1,110
|
Poker hand evaluator
Dose anybody have a working 5 cards poker hand evaluator?
I kinda hate the fact that i can't find a fully working one because its the only missing piece from the poker system in my source.
Ps: i found a bunch of them online but most of them are coded to handle the top 10 hands IE.(flush/royal Flush/str8/str8 flush/3 of a kind/etc) which is useless :P
|
|
|
05/18/2012, 20:12
|
#2
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,383
|
The theory is all quite simple.. you have what you need, you just need to make a (fairly small) logical step forward and basically assign a score to hands.
Each hand is going to be 5 cards.
Each card is going to have a suit and a value.
To calculate a score, you're going to first check special conditions (in descending order)
EG: Are all my cards the same suit? If so I can check for flush/royal flush, etc.
Once you get the type of hand you have.... you can compare versus the other hands being played by assigning them scores and simply ordering the scores in a descending fashion.
For example... We've checked for all 'special' hands, we now check for 2/3/4 of a kind. If any of those are true, we assign a score to that type of hand to indicate that no lower form of hand can beat it... we then add to that score to indicate how well ranked our pair/3 of a kind/etc is ranked (2 Queens beats 2 Tens, etc).
It's all quite simple from an actual math side. You just need to work out some basic logic and run your own test scenarios to ensure that the logic is working correctly.
<edit>
Just use a bunch of helper methods...
int maxCardsOfValue//helper to determine 4/3/2 of a kind
int CardsofValue(value)//Helper to determine how many of a specific card value we are holding
int GetHighCardValue()//helper to return the top value card we're holding. Default score for our evaluate hands method
int GetSecondHighCardValue()//used in tiebreaking rarely.
Then you can write your bool methods determining if we're holding a specific hand type. You can assign your current hand a numerical score based on the type of hand + the high card + the second high card (depends on hand type. Doesn't apply to all)
You can then determine if one hand beats a different one by simply comparing these scores.
Examples
RoyalFlush = 2000 score
StraightFlush = 1500 Score + HighCardValue
FourOfAKind = 1000 + CardValue
FullHouse = 750 + 3CardValue
Flush = 600 + HighCardValue + SecondHighCardValue
Straight = 500 + HighCardValue
etcetcetc
Now we can use simple logic to check our conditions and return a hand score. We then compare card hands and assign a winner.
Really simple, just requires some planning and testing to validate your formula.
|
|
|
05/19/2012, 08:32
|
#3
|
elite*gold: 0
Join Date: Aug 2010
Posts: 992
Received Thanks: 1,110
|
Quote:
Originally Posted by pro4never
The theory is all quite simple.. you have what you need, you just need to make a (fairly small) logical step forward and basically assign a score to hands.
Each hand is going to be 5 cards.
Each card is going to have a suit and a value.
To calculate a score, you're going to first check special conditions (in descending order)
EG: Are all my cards the same suit? If so I can check for flush/royal flush, etc.
Once you get the type of hand you have.... you can compare versus the other hands being played by assigning them scores and simply ordering the scores in a descending fashion.
For example... We've checked for all 'special' hands, we now check for 2/3/4 of a kind. If any of those are true, we assign a score to that type of hand to indicate that no lower form of hand can beat it... we then add to that score to indicate how well ranked our pair/3 of a kind/etc is ranked (2 Queens beats 2 Tens, etc).
It's all quite simple from an actual math side. You just need to work out some basic logic and run your own test scenarios to ensure that the logic is working correctly.
<edit>
Just use a bunch of helper methods...
int maxCardsOfValue//helper to determine 4/3/2 of a kind
int CardsofValue(value)//Helper to determine how many of a specific card value we are holding
int GetHighCardValue()//helper to return the top value card we're holding. Default score for our evaluate hands method
int GetSecondHighCardValue()//used in tiebreaking rarely.
Then you can write your bool methods determining if we're holding a specific hand type. You can assign your current hand a numerical score based on the type of hand + the high card + the second high card (depends on hand type. Doesn't apply to all)
You can then determine if one hand beats a different one by simply comparing these scores.
Examples
RoyalFlush = 2000 score
StraightFlush = 1500 Score + HighCardValue
FourOfAKind = 1000 + CardValue
FullHouse = 750 + 3CardValue
Flush = 600 + HighCardValue + SecondHighCardValue
Straight = 500 + HighCardValue
etcetcetc
Now we can use simple logic to check our conditions and return a hand score. We then compare card hands and assign a winner.
Really simple, just requires some planning and testing to validate your formula.
|
Thanks for the great explanation i understand like most of it and i believe its not going to be that hard to get it done by my self.. i was just hoping someone has it/willing to share it because it wold save tons of my time/work.
silly me :P
|
|
|
05/20/2012, 12:21
|
#4
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,383
|
How far have you gotten on this? I am not doing it in any way related to conquer but I'm gonna write my own poker hand evaluator tomorrow and if you're interested I can share what I'm doing.
Writing a simple poker game in XNA for practice.
|
|
|
05/20/2012, 14:00
|
#5
|
elite*gold: 0
Join Date: Aug 2010
Posts: 992
Received Thanks: 1,110
|
Thanks for the offer pro. but i'm not any way near finishing it because i haven't started yet. but i was thinking i might do something similar to this  i like his idea.
|
|
|
05/20/2012, 23:59
|
#6
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,383
|
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
<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)
|
|
|
05/21/2012, 00:41
|
#7
|
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
|
All 5 cards are taken into consideration when determining who has a winning hand. So in your example hand if another player also had a pair of 7s and 8 and a 6, but had a 5 and not a 4 then that player would win.
|
|
|
05/21/2012, 02:29
|
#8
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,383
|
Quote:
Originally Posted by Korvacs
All 5 cards are taken into consideration when determining who has a winning hand. So in your example hand if another player also had a pair of 7s and 8 and a 6, but had a 5 and not a 4 then that player would win.
|
This is for 5 card draw. There are no communal cards.
Same thing would work for a texas hold 'em but you'd have to modify it slightly to take into account how to calculate hand type when using more than 5 cards total.
The same thing applies though... It doesn't matter where the actual hand comes from (community cards or own pocket cards). Same rules apply, you just have to be more careful when checking conditions to ensure that it fits.
And yes, bit more towards your point. VERY rarely a 3rd or 4th highest card will be taken into account (example, highcard we both have King, Queen, Ten and a Nine... the tie breaker is then the final 5th card)
For my simple application this is not a real concern but could be added fairly simply in the final comparison code... or more likely just decide the match is a tie if the scores were the same (both high card Ace with 10, 9 kickers for example). At that point decide to either call it a tie or just continue checking further cards by adding them to your score.
|
|
|
05/21/2012, 09:28
|
#9
|
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
|
What i said doesn't depend on communal cards, someone could have the hand i stated and would win in that case in 5 card draw...
Since a hand in poker is made up of 5 cards, and its the best 5 cards which win, it doesn't matter if the 5 cards include communal cards (Hold'em, Omaha) or if you are delt them (5 card draw), it is still a 5 card hand, and therefore the 5th card is just as important as the first in determining the winner.
And while it might very well be rare your lack of implementation basically means that you will hand out split pots where only 1 person should walk away with the pot. Which is incorrect..
So for anyone wanting to implement a proper poker system, for the love of *** goto the 5th card for checking whose won in all cases.
|
|
|
All times are GMT +2. The time now is 15:44.
|
|