Menu
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).
I seem to remember something along these lines was done for fun inNothing in that thread seems to have anything to do with this question.this thread .
Have a dig in the posts, I never saw the final results to the challenge.[/QUOTE]
write a seven card hand evaluator[/QUOTE]How do I go about doing that? Sounds complicated!
You might want to search around to see if you can find a PHP implementation of theI'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.[url=http://pokersource.sourceforge.net/]poker-eval library[/url] .[/QUOTE]
It still might be worth looking at the source code to see what algorithms they used.[/QUOTE]Have you looked at the source?
Have you looked at the source?[/QUOTE]
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.
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.
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?Google "Rainbow Tables", same idea.[/QUOTE]MrCoder,
I would have a couple arrays, one with values of the suitesCan you expand on that or show me some code?
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?[/QUOTE]
[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'=>'♦', 'Clubs'=>'♣', 'Hearts'=>'♥', '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]
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.
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.http://www.phpbuilder.com/board/showthread.php?t=10283302 --- another discussion about this.[/QUOTE]
MrCoder,
I've googled rainbow tables and they are about cryptography, nothing to do with poker. What's the connection?[/QUOTE]
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.
[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]
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).
PM me your email address.[/QUOTE]
Post an email.[/QUOTE]
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]
0.1.9 — BETA 5.6