Click to See Complete Forum and Search --> : Poker odds calculator


HomoErectus
07-11-2007, 02:37 PM
Has anyone got any idea how to build a poker odds calculator with PHP that returns a percentage for each of the hands being compared (like on the poker on the tele).

MrCoder
07-11-2007, 06:18 PM
I seem to remember something along these lines was done for fun in this thread (http://www.webdeveloper.com/forum/showthread.php?t=105704&highlight=poker).

Have a dig in the posts, I never saw the final results to the challenge.

HomoErectus
07-12-2007, 05:40 AM
I seem to remember something along these lines was done for fun in this thread (http://www.webdeveloper.com/forum/showthread.php?t=105704&highlight=poker).

Have a dig in the posts, I never saw the final results to the challenge.Nothing in that thread seems to have anything to do with this question.

Anyone know how?

bokeh
07-14-2007, 10:07 PM
First step is to write a seven card hand evaluator that returns an integer value for each hand you feed into it. It needs to be fast, efficient code.

Step two is to either run a simulation (10000 random communities should give a reasonably accurate result) or a brute force search (which is less efficient but 100% accurate). My recommendation is simulation pre-flop and brute force search post-flop.

HomoErectus
07-15-2007, 08:20 AM
write a seven card hand evaluatorHow do I go about doing that? Sounds complicated!

NogDog
07-15-2007, 03:04 PM
You might want to search around to see if you can find a PHP implementation of the poker-eval library (http://pokersource.sourceforge.net/).

HomoErectus
07-15-2007, 05:09 PM
You might want to search around to see if you can find a PHP implementation of the poker-eval library (http://pokersource.sourceforge.net/).I've had a look at that thanks but it doesn't run under Windows XP and its not PHP. I wanted to use PHP because the script will work on all platforms.

NogDog
07-15-2007, 05:36 PM
It still might be worth looking at the source code to see what algorithms they used.

bokeh
07-16-2007, 08:11 AM
It still might be worth looking at the source code to see what algorithms they used.Have you looked at the source?

NogDog
07-16-2007, 08:57 PM
Have you looked at the source?
Nope. It's not my project...just trying to throw out a possibly helpful suggestion (and possibly not).

bokeh
07-19-2007, 03:36 AM
It still might be worth looking at the source code to see what algorithms they used.I've had a look at the source but it's like 100 different files that keep including other files, that are not documented, and that are not named in a way that would help someone follow the code.

HomoErectus
07-21-2007, 01:39 AM
Any other ideas how to go about solving this? :)

HomoErectus
07-26-2007, 03:59 AM
Hello again,

Still not solved. I started work trying to create a 7 card evaluator but there are so many different types of hands to look for the code is complicated and slow due to the sorting required (and still doesn't work properly).

Any ideas that could make this simpler would be greatly appreciated.

PS, I'm not asking anyone to write it for me, I'm just looking for ideas to simplify the code.

bokeh
07-27-2007, 03:30 AM
there are so many different types of hands to look for the code is complicated and slow due to the sorting requiredThere's no need to do any sorting or to work out the hand type; you just need a look-up table.

HomoErectus
07-28-2007, 10:29 AM
you just need a look-up table.52!
--- = 130 million
7!I'm sure you must be joking. Disregarding order there are some 130 million seven-card hands. How could you have a look-up table that big?

MrCoder
07-28-2007, 07:05 PM
Google "Rainbow Tables", same idea.

HomoErectus
08-01-2007, 11:38 AM
Google "Rainbow Tables", same idea.MrCoder,

I've googled rainbow tables and they are about cryptography, nothing to do with poker. What's the connection?

ellisgl
08-01-2007, 11:54 AM
IIRC Rainbow tables are used to look up of an encoded string to find the unencoded version.
MD5'd passwords.. Just a large database - but I don't see the connection for statistical calculations.

I would have a couple arrays, one with values of the suites
one with the cards values 2-A
one with what's been delt for each player.
one to show whats left.

do some logic ie - value of the hand.
matching card values
suite values.


There's other stuff in there that would need to be done...

HomoErectus
08-02-2007, 08:58 AM
I would have a couple arrays, one with values of the suites
one with the cards values 2-A
one with what's been delt for each player.
one to show whats left.

do some logic ie - value of the hand.
matching card values
suite values.Can you expand on that or show me some code?

ellisgl
08-02-2007, 09:34 AM
Can you expand on that or show me some code?


Here's some code I did a while back ago - it works - but it's not completed to reflect what's used and is left. But it's a base...

<?php
// Simple card game called WAR!
session_start();

$cards = array('2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5', '6'=>'6', '7'=>'7', '8'=>'8', '9'=>'9', '10'=>'10', 'J'=>'11', 'Q'=>'12', 'K'=>'13', 'A'=>'14');
$suits = array('Diamonds'=>0, 'Clubs'=>1, 'Hearts'=>2, 'Spades'=>3);
$symbs = array('Diamonds'=>'&diams;', 'Clubs'=>'&clubs;', 'Hearts'=>'&hearts;', 'Spades'=>'&spades;');
$colrs = array('Diamonds'=>'red', 'Clubs'=>'black', 'Hearts'=>'red', 'Spades'=>'black');
if($_POST)
{
// Draw cards
$ccard = array_rand($cards);
$pcard = array_rand($cards);

// Select suit
$csuit = array_rand($suits);
$psuit = array_rand($suits);

// Make sure not same card & suit
while($csuit == $psuit)
{
$psuit = array_rand($suits);
}

// Figure out winner and increment the score.
$cwin = 0;

if($cards[$ccard] > $cards[$pcard])
{
$cwin = 1;
++$_SESSION['cscore'];
}
elseif($cards[$ccard] < $cards[$pcard])
{
++$_SESSION['pscore'];
}
else
{
if($suits[$csuit] > $suits[$psuit])
{
$cwin = 1;
++$_SESSION['cscore'];

}
else
{
++$_SESSION['pscore'] ;
}
}
// Display HTML
echo 'Computer: ',$_SESSION['cscore'],'<br />'
,'Player: ',$_SESSION['pscore'],'<br /><br />'
,'Opponent: ',$ccard,' ',$csuit,'<br />'
,'You: ',$pcard,' ',$psuit,'<br /><br />';
if($cwin > 0)
{
echo 'You loose<br />';
}
else
{
echo 'You won!<br />';
}
?>
<form action="" method="post">
<input type="submit" name="Submit" value="Attack">
</form>
<?php
}
else
{
$_SESSION['cscore'] = 0;
$_SESSION['pscore'] = 0;
?>
<form action="" method="post">
<input type="submit" name="Submit" value="Lets play WAR!">
</form>
<?php
}
?>

HomoErectus
08-02-2007, 09:51 AM
Here's some code I did a while back ago - it works - but it's not completed to reflect what's used and is left.That code doesn't seem to have anything to do with scoring a poker hand.

ellisgl
08-02-2007, 09:52 AM
It's a base for a basic card game - compairing 2 card values.. It shows the arrays I was sort of talking about.

http://www.tightpoker.com/poker_odds.html

HomoErectus
08-02-2007, 10:02 AM
Well to score a poker hand the code needs to select the best 5 cards from 7 and then return a score that can be compared with other hands.

ellisgl
08-02-2007, 10:09 AM
http://www.pokerstove.com/analysis/zealots.php --- check that out. It has links to discussions and to some C code.

ellisgl
08-02-2007, 10:20 AM
http://www.phpbuilder.com/board/showthread.php?t=10283302 --- another discussion about this.

bokeh
08-03-2007, 03:18 AM
http://www.phpbuilder.com/board/showthread.php?t=10283302 --- another discussion about this.That link is an excellent example of how not to do it. The function is slow (just look at all the loops and sorting), doesn't return anything useful and can't even find an ace low straight. Use a look-up table.

MrCoder
08-03-2007, 07:09 AM
MrCoder,

I've googled rainbow tables and they are about cryptography, nothing to do with poker. What's the connection?


Rainbow tables are used to decrypt MD5 passwords.

Instead of trying every possible password to crack the MD5, the MD5 is passed to the rainbow table and then it returns the password.

This is the same way you should handle hands.

Yes rainbow tables are huge, but if the MD5 is in the table then the password will be returned in seconds instead of hours, days, months or even years.

ellisgl
08-03-2007, 08:28 AM
So all that would be needed would be only a database of all possible winning hands with a some sort of score right?

bokeh
08-03-2007, 10:00 AM
Rainbow tables are hugeBut we are not dealing with a huge look-up table; there are only 133 million combinations. Still too big for a look-up array though.So all that would be needed would be only a database of all possible winning hands with a some sort of score right?A database is too slow; you'd need to use an array for the look-up table.

Even though there are 133 million combinations of 7-card hands each of those hands falls into one of only 4824 possible rank groups (there are more rank groups for 5-card hands then 7-card hands). In common with the rainbow table idea we need a function to reduce 133 million combinations down to practical number. It is then simply a matter of returning the corresponding numeric value from the look-up array.

<?php

function ScoreHand($in)
{
/*******************************************************

AUTHOR:
Bokeh (http://bokehman.com/)

PURPOSE:
To score a 7-card Texas Hold'em Poker Hand

DESCRIPTION:
int ScoreHand( array hand )

RAM FOOTPRINT:
4.2 megabytes

INPUT:
An array of 7 members, each member representing one card
in the following format: 2 characters, the first character
represents the card rank, (AKQJT98765432) and the second
character represents the suit (CDHS). Alphabetical characters
are required to be upper case. Example input array:
array('AH', '5H', '4H', '9C', 'TC', '2D', '3H')

RETURN VALUE:
An integer between between 1 (Royal Flush) and
7414 (Nine High "98754" which is the worst hand
it is possible to form from 7 cards).
The lower the value the better the hand.

EXAMPLE USE:
$hand = array('AH', '5H', '4H', '9C', 'TC', '2H', '3H');
echo ScoreHand($hand);

LOOK-UP ARRAYS CONTAIN:
1287 5-card flushes
1716 6-card flushes
1716 7-card flushes
49205 7-card non-flushes

IMPORTANT NOTE:
The use static variables in this function allows a speed
increase of more than 100:1.

*********************************************************/

static $LookUpTable = array(
/* Email me for this array. Too big to post. */
);

static $values = array( /* allocate prime number values to the card ranks to avoid collisions */
'A'=>41,'K'=>37,'Q'=>31,'J'=>29,'T'=>23,'9'=>19,'8'=>17,'7'=>13,'6'=>11,'5'=>7,'4'=>5,'3'=>3,'2'=>2
);

$CardValue = 1;
foreach($in as $card)
{
$suits[$card[1]][] = $card;
$CardValue *= $values[$card[0]];
}
$NormalHand = $LookUpTable['NormalHands'][$CardValue];
if(count($suits)<4) // this line is optional but saves a couple of microseconds on an average hand.
foreach($suits as $suit)
{
if(count($suit)>4) /* must be a flush */
{
$SuitValue = 1;
foreach($suit as $card)
{
$SuitValue *= $values[$card[0]];
}
if(($FlushHand = $LookUpTable['FlushHands'][$SuitValue]) < $NormalHand)
{
return $FlushHand;
}
}
}
return $NormalHand;
}

?>

ellisgl
08-03-2007, 10:18 AM
Nice! But what about other things like 2 of kind, pairs and comparing the suit to determine the winner - I only see something for a flush...

bokeh
08-03-2007, 10:44 AM
Nice! But what about [...] comparing the suit to determine the winnerIn Texas Hold'em Poker there is no comparing suits to find a winner. A Royal Flush of Hearts is worth exactly the same as a Royal Flush of Diamonds, etc.But what about other things like 2 of kind, pairs [...] I only see something for a flush...All that is dealt with in the look-up table. We don't need to know the hand type at all; only one which returns the better score. The only reason a flush needs to be handled differently is because we only want to look up the cards that comprise the flush (which will be 5, 6 or 7 of the seven input cards).

ellisgl
08-03-2007, 10:50 AM
Ah - I didn't not know that suit comparison is not done. (I've never played texas holdem (even thou I've watched).. I like 5 card stud better =)

HomoErectus
08-05-2007, 06:24 AM
Ok, thanks for the array. So I can get the value of a hand now. What next? How do I convert that into probability for each player? And how do I get the hand name if I want it?

bokeh
08-05-2007, 01:21 PM
Well you either need to run some random hands or step through all possible combos to find the probability of a win.

HomoErectus
08-17-2007, 04:13 AM
Ok, but I can't work out how to do that. Can anyone help me.

bokeh
09-21-2007, 10:44 AM
How did you get on with this?

I had some spare time so I thought I would have a go at this interesting puzzle. Here's what I came up with: Texas hold'em poker probability calculator. (http://texas.holdem.poker.probability.cal.culator.org/)

What do you think?

Sheldon
09-21-2007, 10:58 AM
I think thats real good !! Not slow loading at all for query execution time.

Jackanapesink
10-03-2007, 12:36 AM
Fantastic...I would have expected it to be a little cumbersome from reading this topic, but it literally slides right through.

From a usability standpoint, the card graphics are a little too large for easily navigating a large 10 player table, but I admire your choice of card selection.

Well done!

Yann
10-27-2008, 10:35 AM
Hello bokeh, i can't email you via your server-side form... Can you tell me please how to create the lookupTable ?

bokeh
10-27-2008, 12:36 PM
PM me your email address.

Yann
11-01-2008, 01:45 PM
PM me your email address.

I read the documentation FAQ to find where is the button for sending private messages here...
It said that there must have a button "Send PM" in your posts, but i can't see anyone.

I click on the example link in the FAQ too : http://www.webdeveloper.com/forum/private.php?do=newpm

And i have this message :
Yann, you do not have permission to access this page. This could be due to one of several reasons:

1. Your user account may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
2. If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Well... it seems to be hard to contact you outside of this forum.. :confused:

Can you support me ?


Thanks for reading !

thomas10001
11-03-2008, 11:19 AM
Hi

I am also very interested in the algorithm for calculating poker odds. But I cannot contact you either via email or PM. How can I get in touch?

Regards

Thomas

bokeh
11-03-2008, 02:32 PM
Post an email.

Yann
11-03-2008, 04:15 PM
Post an email.

Sorry but i post an email in my last message and it seems to have been removed by the moderator :-(

Edit by mod: It was removed for a reason, email addresses on public websites is just not smart. You now have private messages enabled so I suggest you go that route.

Yann
11-04-2008, 10:09 AM
Edit by mod: It was removed for a reason, email addresses on public websites is just not smart. You now have private messages enabled so I suggest you go that route.

Thanks for all, i was able to send a PM ! :o

rb-cohen
01-02-2009, 12:52 PM
Hey all,

I would also like to see the look up table, it'd be handy to have for a pet project I'm working on. Unless I hear back I'll have to make my own.

I can't seem to PM either, so could I get PM's enabled or perhaps email me the code to inf0 [ a@t ] bl0grand0m.c0m, with the 0's converted back to o's.

Thanks in advance, if I get the project off the ground I'll be sure to put it up for all to see.

Regards,

Cohen