bugfix
bugfix

<?php <?php
   
/** /**
* Any five card poker hand generator * Any five card poker hand generator
* *
* http://rip94550.wordpress.com/2011/03/14/poker-hands-%E2%80%93-5-card-draw/ * http://rip94550.wordpress.com/2011/03/14/poker-hands-%E2%80%93-5-card-draw/
* http://en.wikipedia.org/wiki/List_of_poker_hands * http://en.wikipedia.org/wiki/List_of_poker_hands
* http://en.wikipedia.org/wiki/Poker_probability * http://en.wikipedia.org/wiki/Poker_probability
* *
* @author Razvan Stanga <git@razvi.ro> * @author Razvan Stanga <git@razvi.ro>
*/ */
   
class Anyfivecardpokerhand { class Anyfivecardpokerhand {
public static $types = array ("hearts" , "diamonds" , "clubs" , "spades"); public static $types = array ("hearts" , "diamonds" , "clubs" , "spades");
public static $cards = array ("ace" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "jack" , "queen" , "king"); public static $cards = array ("ace" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "jack" , "queen" , "king");
public static $hands = array ( public static $hands = array (
0 => "nothing", 0 => "nothing",
1 => "one pair", 1 => "one pair",
2 => "two pair", 2 => "two pair",
3 => "flush", 3 => "flush",
32 => "full house", 32 => "full house",
4 => "four of a kind", 4 => "four of a kind",
); );
private $deckOfCards = array (); private $deckOfCards = array ();
private $debug = false; private $debug = false;
private $dbConnection = null; private $dbConnection = null;
   
function __construct () { function __construct () {
if ( $_SERVER['HTTP_HOST'] && !isset ($_GET['override']) ) { if ( $_SERVER['HTTP_HOST'] && !isset ($_GET['override']) ) {
echo "This script should be run in cli. To override click <a href=\"generator.php?override=true\">here</a>."; echo "This script should be run in cli. To override click <a href=\"generator.php?override=true\">here</a>.";
exit (); exit ();
} }
$this->getDeckOfCards (); $this->getDeckOfCards ();
} }
   
private function generateDeckOfCards () { private function generateDeckOfCards () {
foreach ($this->cards as $card) { foreach (self::$cards as $card) {
foreach ($this->types as $type) { foreach (self::$types as $type) {
$combo = $card.' '.$type; $combo = $card.' '.$type;
array_push( $this->deckOfCards, $combo); array_push( $this->deckOfCards, $combo);
} }
} }
return $this->deckOfCards; return $this->deckOfCards;
} }
   
public function getDeckOfCards () { public function getDeckOfCards () {
if ( count($this->deckOfCards) == 0 ) { if ( count($this->deckOfCards) == 0 ) {
return $this->generateDeckOfCards (); return $this->generateDeckOfCards ();
} }
return $this->deckOfCards; return $this->deckOfCards;
} }
   
public function generateAnyFiveCardPokerHand ($debug=false) { public function generateAnyFiveCardPokerHand ($debug=false) {
$this->debug = $debug; $this->debug = $debug;
$this->dbConnection = new dbConnection; $this->dbConnection = new dbConnection;
   
$arr1 = $this->deckOfCards; $arr1 = $this->deckOfCards;
$i = 0; $i = 0;
foreach ( $arr1 as $k1 => $v1 ) { foreach ( $arr1 as $k1 => $v1 ) {
$key = array_search($v1, $this->deckOfCards)+1; $key = array_search($v1, $this->deckOfCards)+1;
$arr2 = array_slice($this->deckOfCards, $key); $arr2 = array_slice($this->deckOfCards, $key);
   
foreach ( $arr2 as $k2 => $v2 ) { foreach ( $arr2 as $k2 => $v2 ) {
$key = array_search($v2, $this->deckOfCards)+1; $key = array_search($v2, $this->deckOfCards)+1;
$arr3 = array_slice($this->deckOfCards, $key); $arr3 = array_slice($this->deckOfCards, $key);
   
foreach ( $arr3 as $k3 => $v3 ) { foreach ( $arr3 as $k3 => $v3 ) {
$key = array_search($v3, $this->deckOfCards)+1; $key = array_search($v3, $this->deckOfCards)+1;
$arr4 = array_slice($this->deckOfCards, $key); $arr4 = array_slice($this->deckOfCards, $key);
   
foreach ( $arr4 as $k4 => $v4 ) { foreach ( $arr4 as $k4 => $v4 ) {
$key = array_search($v4, $this->deckOfCards)+1; $key = array_search($v4, $this->deckOfCards)+1;
$arr5 = array_slice($this->deckOfCards, $key); $arr5 = array_slice($this->deckOfCards, $key);
   
foreach ( $arr5 as $k5 => $v5 ) { foreach ( $arr5 as $k5 => $v5 ) {
$combo = array (); $combo = array ();
array_push ($combo, $v1, $v2, $v3, $v4, $v5); array_push ($combo, $v1, $v2, $v3, $v4, $v5);
sort ($combo); sort ($combo);
$hand = $this->getCardCombination ($combo); $hand = $this->getCardCombination ($combo);
$this->dbConnection->dbQuery ("INSERT INTO `cards` (`id`, `card1`, `card2`, `card3`, `card4`, `card5`, `combo`, `combohash`, `hand`) VALUES (NULL, '".$combo[0]."', '".$combo[1]."', '".$combo[2]."', '".$combo[3]."', '".$combo[4]."', '".implode (",", $combo)."', '".md5 ( implode ("", $combo) )."', '".$hand."')"); $this->dbConnection->dbQuery ("INSERT INTO `cards` (`id`, `card1`, `card2`, `card3`, `card4`, `card5`, `combo`, `combohash`, `hand`) VALUES (NULL, '".$combo[0]."', '".$combo[1]."', '".$combo[2]."', '".$combo[3]."', '".$combo[4]."', '".implode (",", $combo)."', '".md5 ( implode ("", $combo) )."', '".$hand."')");
$i++; $i++;
if ( $this->debug ) { if ( $this->debug ) {
echo "total card combinations : ".$i."\n"; echo "total card combinations : ".$i."\n";
} }
} }
} }
} }
} }
} }
} }
   
public function getCardCombination ($combo) { public function getCardCombination ($combo) {
$result = array (); $result = array ();
foreach ( $this->cards as $card ) { foreach ( self::$cards as $card ) {
$r = array_filter($combo, function ($item) use ($card) { $r = array_filter($combo, function ($item) use ($card) {
if (stripos($item, $card) !== false) { if (stripos($item, $card) !== false) {
return true; return true;
} }
return false; return false;
}); });
if ($r){ if ($r){
$result[] = $r; $result[] = $r;
} }
} }
if ( count ($result) == 5 ) { if ( count ($result) == 5 ) {
return 0; return 0;
} elseif ( count ($result) == 4 ) { } elseif ( count ($result) == 4 ) {
return 1; return 1;
} elseif ( count ($result) == 3 ) { } elseif ( count ($result) == 3 ) {
if ( count ($result[0])>=3 || count ($result[1])>=3 || count ($result[2])>=3 ) { if ( count ($result[0])>=3 || count ($result[1])>=3 || count ($result[2])>=3 ) {
return 3; return 3;
} else { } else {
return 2; return 2;
} }
} elseif ( count ($result) == 2 ) { } elseif ( count ($result) == 2 ) {
if ( count ($result[0])==4 || count ($result[1])==4 ) { if ( count ($result[0])==4 || count ($result[1])==4 ) {
return 4; return 4;
} elseif ( (count ($result[0])==2 || count ($result[1])==3) || (count ($result[0])==3 || count ($result[1])==2) ){ } elseif ( (count ($result[0])==2 || count ($result[1])==3) || (count ($result[0])==3 || count ($result[1])==2) ){
return 32; return 32;
} else { } else {
return 10; return 10;
} }
} }
} }
} }
   
?> ?>
comments