1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | <?php /** * Any five card poker hand generator * * 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/Poker_probability * * @author Razvan Stanga <git@razvi.ro> */ class Anyfivecardpokerhand { 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 $hands = array ( 0 => "nothing", 1 => "one pair", 2 => "two pair", 3 => "flush", 32 => "full house", 4 => "four of a kind", ); private $deckOfCards = array (); private $debug = false; private $dbConnection = null; function __construct () { 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>."; exit (); } $this->getDeckOfCards (); } private function generateDeckOfCards () { foreach ($this->cards as $card) { foreach ($this->types as $type) { $combo = $card.' '.$type; array_push( $this->deckOfCards, $combo); } } return $this->deckOfCards; } public function getDeckOfCards () { if ( count($this->deckOfCards) == 0 ) { return $this->generateDeckOfCards (); } return $this->deckOfCards; } public function generateAnyFiveCardPokerHand ($debug=false) { $this->debug = $debug; $this->dbConnection = new dbConnection; $arr1 = $this->deckOfCards; $i = 0; foreach ( $arr1 as $k1 => $v1 ) { $key = array_search($v1, $this->deckOfCards)+1; $arr2 = array_slice($this->deckOfCards, $key); foreach ( $arr2 as $k2 => $v2 ) { $key = array_search($v2, $this->deckOfCards)+1; $arr3 = array_slice($this->deckOfCards, $key); foreach ( $arr3 as $k3 => $v3 ) { $key = array_search($v3, $this->deckOfCards)+1; $arr4 = array_slice($this->deckOfCards, $key); foreach ( $arr4 as $k4 => $v4 ) { $key = array_search($v4, $this->deckOfCards)+1; $arr5 = array_slice($this->deckOfCards, $key); foreach ( $arr5 as $k5 => $v5 ) { $combo = array (); array_push ($combo, $v1, $v2, $v3, $v4, $v5); sort ($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."')"); $i++; if ( $this->debug ) { echo "total card combinations : ".$i."\n"; } } } } } } } public function getCardCombination ($combo) { $result = array (); foreach ( $this->cards as $card ) { $r = array_filter($combo, function ($item) use ($card) { if (stripos($item, $card) !== false) { return true; } return false; }); if ($r){ $result[] = $r; } } if ( count ($result) == 5 ) { return 0; } elseif ( count ($result) == 4 ) { return 1; } elseif ( count ($result) == 3 ) { if ( count ($result[0])>=3 || count ($result[1])>=3 || count ($result[2])>=3 ) { return 3; } else { return 2; } } elseif ( count ($result) == 2 ) { if ( count ($result[0])==4 || count ($result[1])==4 ) { return 4; } elseif ( (count ($result[0])==2 || count ($result[1])==3) || (count ($result[0])==3 || count ($result[1])==2) ){ return 32; } else { return 10; } } } } ?> |