buildDeck(); } private function __clone() {} var $deck = array(); var $communitycards = array(); var $hands = array(); var $burned = array(); /** * Private function buildDeck(). * Builds a deck of cards, and shuffles it. */ private function buildDeck() { $suits = "SCHD"; for ($i=0; $ideck, $suits[$i].$j); } } shuffle($this->deck); } /** * Function deal($players). * Deals cards based on number of players. * @return hand */ function deal($players) { for($i=0; $i<$players; $i++) { $hand = $this->deck[count($this->deck)-1]; array_pop($this->deck); $hand = $hand.".".$this->deck[count($this->deck)-1]; array_pop($this->deck); array_push($this->hands, $hand); } return explode(".", $this->hands[0], 2); } /** * Private function burn(). * Burns a card from the deck. * Removes the upper card of the deck, * and places it in $burned (array). */ function burn() { array_push($this->burned, $this->deck[count($this->deck)-1]); array_pop($this->deck); } /** * Function flop(). * Deals the first three communitycards (the flop). * Removes the upper card of the deck, * and places it in $communitycards (array). */ function flop() { $this->burn(); array_push($this->communitycards, $this->deck[count($this->deck)-1]); array_pop($this->deck); array_push($this->communitycards, $this->deck[count($this->deck)-1]); array_pop($this->deck); array_push($this->communitycards, $this->deck[count($this->deck)-1]); array_pop($this->deck); } /** * Function turn(). * Deals the fourth communitycard (turn). * Removes the upper card of the deck, * and places it in $communitycards (array). */ function turn() { $this->burn(); $turn = $this->deck[count($this->deck)-1]; array_push($this->communitycards, $turn); array_pop($this->deck); } /** * Function river(). * Deals the fifth communitycard (river). * Removes the upper card of the deck, * and places it in $communitycards (array). */ function river() { $this->burn(); $river = $this->deck[count($this->deck)-1]; array_push($this->communitycards, $river); array_pop($this->deck); } /** * Function score(). * Calculates the best hand. * @return score */ function score() { $cards = $this->communitycards; $hand = explode(".", $this->hands[0], 2); array_push($cards, $hand[0]); array_push($cards, $hand[1]); //$cards = array("D12", "D13", "D10", "D9", "H7", "H11", "H3"); natsort($cards); // for checking pairs and straights $faces = array(); foreach($cards as $card) { array_push($faces, ereg_replace("[^0-9]", "", $card)); } $flush = $this->isFlush($cards); $straight = $this->isStraight($faces); $pair = $this->isPair($faces); $highCard = $this->highCard($faces); if($straight && $flush) { return "straight flush"; } else if($flush) { return "flush"; } else if($straight) { return "straight"; } else if($pair) { return "pair"; } else { return $highCard; } } /** * Function highCard($cards). * Calculates the highcard/kicker. * @param cards * @return high */ function highCard($cards) { foreach($cards as $card) { if($card == 1) { return 14; } else if($card > $high) { $high = $card; } } return $high; } /** * Function isPair($cards). * Checks for pairs. * @param cards * @return boolean */ function isPair($cards) { if(count($cards) == count(array_unique($cards))) { return false; } else { return true; } } /** * Function isFlush($cards). * Checks for a flush. * @param cards * @return boolean */ function isFlush($cards) { foreach($cards as $card) { $seqence = $seqence.$card[0]; } if(preg_match("/[C]{5,}|[D]{5,}|[H]{5,}|[S]{5,}/", $seqence)) { $flush = true; } else return false; } /** * Function isStraight($cards). * Checks for a straight. * @param cards * @return boolean */ function isStraight($cards) { $cards = array_unique($cards); natsort($cards); foreach($cards as $card) { $sequence = $sequence.$card; } if( strpos($sequence, "12345") === false && strpos($sequence, "23456") === false && strpos($sequence, "34567") === false && strpos($sequence, "45678") === false && strpos($sequence, "56789") === false && strpos($sequence, "678910") === false && strpos($sequence, "7891011") === false && strpos($sequence, "89101112") === false && strpos($sequence, "910111213") === false && strpos($sequence, "110111213") === false && strpos($sequence, "12111213") === false && strpos($sequence, "1231213") === false && strpos($sequence, "123413") === false) { return false; } else return true; } /** * Function reset(). * Resets the game. * Places cards back in the deck, * and shuffles it. */ function reset() { $this->deck = array_merge_recursive($this->deck, $this->communitycards); $this->deck = array_merge_recursive($this->deck, $this->burned); foreach($this->hands as $card) { $card = explode(".", $card, 2); array_push($this->deck, $card[0]); array_push($this->deck, $card[1]); } unset($this->communitycards); unset($this->burned); unset($this->hands); shuffle($this->deck); } } ?>