/    Sign up×
Community /Pin to ProfileBookmark

Poker odds calculator

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).

to post a comment
PHP

45 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderJul 11.2007 — I seem to remember something along these lines was done for fun in this thread.

Have a dig in the posts, I never saw the final results to the challenge.
Copy linkTweet thisAlerts:
@HomoErectusauthorJul 12.2007 — I seem to remember something along these lines was done for fun in this thread.

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

Anyone know how?
Copy linkTweet thisAlerts:
@bokehJul 15.2007 — 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.
Copy linkTweet thisAlerts:
@HomoErectusauthorJul 15.2007 — write a seven card hand evaluator[/QUOTE]How do I go about doing that? Sounds complicated!
Copy linkTweet thisAlerts:
@NogDogJul 15.2007 — You might want to search around to see if you can find a PHP implementation of the [url=http://pokersource.sourceforge.net/]poker-eval library[/url].
Copy linkTweet thisAlerts:
@HomoErectusauthorJul 15.2007 — You might want to search around to see if you can find a PHP implementation of the [url=http://pokersource.sourceforge.net/]poker-eval library[/url].[/QUOTE]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.
Copy linkTweet thisAlerts:
@NogDogJul 15.2007 — It still might be worth looking at the source code to see what algorithms they used.
Copy linkTweet thisAlerts:
@bokehJul 16.2007 — It still might be worth looking at the source code to see what algorithms they used.[/QUOTE]Have you looked at the source?
Copy linkTweet thisAlerts:
@NogDogJul 17.2007 — Have you looked at the source?[/QUOTE]
Nope. It's not my project...just trying to throw out a possibly helpful suggestion (and possibly not).
Copy linkTweet thisAlerts:
@bokehJul 19.2007 — It still might be worth looking at the source code to see what algorithms they used.[/QUOTE]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.
Copy linkTweet thisAlerts:
@HomoErectusauthorJul 21.2007 — Any other ideas how to go about solving this? ?
Copy linkTweet thisAlerts:
@HomoErectusauthorJul 26.2007 — 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.
Copy linkTweet thisAlerts:
@bokehJul 27.2007 — there are so many different types of hands to look for the code is complicated and slow due to the sorting required[/QUOTE]There's no need to do any sorting or to work out the hand type; you just need a look-up table.
Copy linkTweet thisAlerts:
@HomoErectusauthorJul 28.2007 — you just need a look-up table.[/QUOTE][CODE]52!
--- = 130 million
7![/CODE]
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?
Copy linkTweet thisAlerts:
@MrCoderJul 29.2007 — Google "Rainbow Tables", same idea.
Copy linkTweet thisAlerts:
@HomoErectusauthorAug 01.2007 — Google "Rainbow Tables", same idea.[/QUOTE]MrCoder,

I've googled rainbow tables and they are about cryptography, nothing to do with poker. What's the connection?
Copy linkTweet thisAlerts:
@ellisglAug 01.2007 — 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...
Copy linkTweet thisAlerts:
@HomoErectusauthorAug 02.2007 — 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.[/QUOTE]
Can you expand on that or show me some code?
Copy linkTweet thisAlerts:
@ellisglAug 02.2007 — Can you expand on that or show me some code?[/QUOTE]


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...
[code=php]
<?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
}
?>
[/code]
Copy linkTweet thisAlerts:
@HomoErectusauthorAug 02.2007 — 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.[/QUOTE]That code doesn't seem to have anything to do with scoring a poker hand.
Copy linkTweet thisAlerts:
@ellisglAug 02.2007 — 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
Copy linkTweet thisAlerts:
@HomoErectusauthorAug 02.2007 — 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.
Copy linkTweet thisAlerts:
@ellisglAug 02.2007 — http://www.pokerstove.com/analysis/zealots.php --- check that out. It has links to discussions and to some C code.
Copy linkTweet thisAlerts:
@bokehAug 03.2007 — http://www.phpbuilder.com/board/showthread.php?t=10283302 --- another discussion about this.[/QUOTE]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.
Copy linkTweet thisAlerts:
@MrCoderAug 03.2007 — MrCoder,

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



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.
Copy linkTweet thisAlerts:
@ellisglAug 03.2007 — So all that would be needed would be only a database of all possible winning hands with a some sort of score right?
Copy linkTweet thisAlerts:
@bokehAug 03.2007 — Rainbow tables are huge[/QUOTE]But 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?[/QUOTE]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.

[code=php]<?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;
}

?>[/code]
Copy linkTweet thisAlerts:
@ellisglAug 03.2007 — 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...
Copy linkTweet thisAlerts:
@bokehAug 03.2007 — Nice! But what about [...] comparing the suit to determine the winner[/QUOTE]In 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...[/QUOTE]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).
Copy linkTweet thisAlerts:
@ellisglAug 03.2007 — 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 =)
Copy linkTweet thisAlerts:
@HomoErectusauthorAug 05.2007 — 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?
Copy linkTweet thisAlerts:
@bokehAug 05.2007 — Well you either need to run some random hands or step through all possible combos to find the probability of a win.
Copy linkTweet thisAlerts:
@HomoErectusauthorAug 17.2007 — Ok, but I can't work out how to do that. Can anyone help me.
Copy linkTweet thisAlerts:
@bokehSep 21.2007 — 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.

What do you think?
Copy linkTweet thisAlerts:
@SheldonSep 21.2007 — I think thats real good !! Not slow loading at all for query execution time.
Copy linkTweet thisAlerts:
@JackanapesinkOct 03.2007 — 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!
Copy linkTweet thisAlerts:
@YannOct 27.2008 — Hello bokeh, i can't email you via your server-side form... Can you tell me please how to create the lookupTable ?
Copy linkTweet thisAlerts:
@bokehOct 27.2008 — PM me your email address.
Copy linkTweet thisAlerts:
@YannNov 01.2008 — PM me your email address.[/QUOTE]

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 :

[FONT="Courier New"]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.[/FONT]


  • Well... it seems to be hard to contact you outside of this forum.. ?

    Can you support me ?


    Thanks for reading !
    Copy linkTweet thisAlerts:
    @thomas10001Nov 03.2008 — 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
    Copy linkTweet thisAlerts:
    @bokehNov 03.2008 — Post an email.
    Copy linkTweet thisAlerts:
    @YannNov 03.2008 — Post an email.[/QUOTE]

    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.
    Copy linkTweet thisAlerts:
    @YannNov 04.2008 — 
    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.[/QUOTE]


    Thanks for all, i was able to send a PM ! :o
    Copy linkTweet thisAlerts:
    @rb-cohenJan 02.2009 — 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
    ×

    Success!

    Help @HomoErectus spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...