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; } } 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; } foreach($this->hands as $face => $count) { if ($count === 2) { $this->pairs[] = $face; } if ($count === 3) { $this->toaks[] = $face; } if ($count === 4) { $this->foaks[] = $face; } } } 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.17, 8.3.0 - 8.3.4
Deprecated: Creation of dynamic property Hand::$values is deprecated in /in/UrYau on line 45 Deprecated: Creation of dynamic property Hand::$foaks is deprecated in /in/UrYau on line 46 Deprecated: Creation of dynamic property Hand::$toaks is deprecated in /in/UrYau on line 46 Deprecated: Creation of dynamic property Hand::$pairs is deprecated in /in/UrYau on line 46 Deprecated: Creation of dynamic property Hand::$possibleStraight is deprecated in /in/UrYau on line 47 Deprecated: Creation of dynamic property Hand::$hands is deprecated in /in/UrYau on line 49 string(1) "6" array(1) { [0]=> int(2) } array(1) { [0]=> int(6) } array(0) { } bool(true) bool(false) Second hand: Deprecated: Creation of dynamic property Hand::$values is deprecated in /in/UrYau on line 45 Deprecated: Creation of dynamic property Hand::$foaks is deprecated in /in/UrYau on line 46 Deprecated: Creation of dynamic property Hand::$toaks is deprecated in /in/UrYau on line 46 Deprecated: Creation of dynamic property Hand::$pairs is deprecated in /in/UrYau on line 46 Deprecated: Creation of dynamic property Hand::$possibleStraight is deprecated in /in/UrYau on line 47 Deprecated: Creation of dynamic property Hand::$hands is deprecated in /in/UrYau on line 49 string(2) "10" array(0) { } array(0) { } array(0) { } bool(false) NULL
Output for 7.1.25 - 7.1.33, 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.27
string(1) "6" array(1) { [0]=> int(2) } array(1) { [0]=> int(6) } array(0) { } bool(true) bool(false) Second hand: string(2) "10" array(0) { } array(0) { } array(0) { } bool(false) NULL

preferences:
145.13 ms | 403 KiB | 179 Q