3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Card { private $suit; private $val; public function __construct($val, $suit) { $values = ['J' => 11, 'Q' => 12, 'K' => 13, 'A' => 14]; $suits = array( 'H' => 'Hearts', 'C' => 'Clubs', 'D' => 'Diamonds', 'S' => 'Spades' ); $this->suit = $suits[$suit]; $this->val = $values[$val] ?? $val; } public function getSuit() { return $this->suit; } public function getVal() { return $this->val; } } function buildCountFilter($num) { return function ($count, $face) use ($num) { if ($count == $num) return $face; }; } class Hand { private $cards; private $matches = array(); public function __construct($hand) { $cards = explode(' ', $hand); foreach ($cards as $card) { $val = substr($card, 0, -1); $suit = strtoupper(substr($card, -1)); $this->cards[] = new Card($val, $suit); } $this->values = array_map(function ($card) { return $card->getVal(); }, $this->cards); $this->pairs = $this->toaks = $this->foaks = []; // _T_hree _O_f _A_ _K_ind and _F_our _O_f _A_ _K_ind $this->possibleStraight = true; $this->hands = array_count_values($this->values); if (max($this->hands) > 1) { $this->possibleStraight = false; } $this->pairs[] = array_filter($this->hands, buildCountFilter(2), ARRAY_FILTER_USE_BOTH); $this->toaks[] = array_filter($this->hands, buildCountFilter(3), ARRAY_FILTER_USE_BOTH); $this->foaks[] = array_filter($this->hands, buildCountFilter(4), ARRAY_FILTER_USE_BOTH); } public function testMethods() { var_dump($this->getHighCard()); var_dump($this->getPairs()); var_dump($this->getToaks()); var_dump($this->getFoaks()); var_dump($this->isFullHouse()); var_dump($this->isStraight()); } public function getCards() { return $this->cards; } public function getValues() { return $this->values; } public function getHighCard() { return max($this->getValues()); } public function getPairs(){ return $this->pairs; } public function getToaks(){ return $this->toaks; } public function getFoaks() { return $this->foaks; } public function hasAce() { return isset($this->hands['14']); } public function isFullHouse() { return count($this->pairs) == 1 && count($this->toaks) == 1; } public function isStraight(){ if (! $this->possibleStraight) { return false; } // the rest is up to you if ($this->hasAce()) { } } } $h = new Hand('6h 6d 6s 2c 2h'); $h->testMethods(); echo "\n\nSecond hand:\n"; $h = new Hand('6h 7d 8s 9c 10h'); $h->testMethods();
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Creation of dynamic property Hand::$values is deprecated in /in/32Hqr on line 49 Deprecated: Creation of dynamic property Hand::$foaks is deprecated in /in/32Hqr on line 50 Deprecated: Creation of dynamic property Hand::$toaks is deprecated in /in/32Hqr on line 50 Deprecated: Creation of dynamic property Hand::$pairs is deprecated in /in/32Hqr on line 50 Deprecated: Creation of dynamic property Hand::$possibleStraight is deprecated in /in/32Hqr on line 51 Deprecated: Creation of dynamic property Hand::$hands is deprecated in /in/32Hqr on line 53 string(1) "6" array(1) { [0]=> array(1) { [2]=> int(2) } } array(1) { [0]=> array(1) { [6]=> int(3) } } array(1) { [0]=> array(0) { } } bool(true) bool(false) Second hand: Deprecated: Creation of dynamic property Hand::$values is deprecated in /in/32Hqr on line 49 Deprecated: Creation of dynamic property Hand::$foaks is deprecated in /in/32Hqr on line 50 Deprecated: Creation of dynamic property Hand::$toaks is deprecated in /in/32Hqr on line 50 Deprecated: Creation of dynamic property Hand::$pairs is deprecated in /in/32Hqr on line 50 Deprecated: Creation of dynamic property Hand::$possibleStraight is deprecated in /in/32Hqr on line 51 Deprecated: Creation of dynamic property Hand::$hands is deprecated in /in/32Hqr on line 53 string(2) "10" array(1) { [0]=> array(0) { } } array(1) { [0]=> array(0) { } } array(1) { [0]=> array(0) { } } bool(true) NULL
Output for 7.1.25 - 7.1.28, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
string(1) "6" array(1) { [0]=> array(1) { [2]=> int(2) } } array(1) { [0]=> array(1) { [6]=> int(3) } } array(1) { [0]=> array(0) { } } bool(true) bool(false) Second hand: string(2) "10" array(1) { [0]=> array(0) { } } array(1) { [0]=> array(0) { } } array(1) { [0]=> array(0) { } } bool(true) NULL

preferences:
183.65 ms | 403 KiB | 162 Q