@ 2019-04-26T13:06:29Z <?php
class Card {
private $suit;
private $val;
public function __construct($val, $suit) {
$values = array(
'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;
}
public function setVal($val) {
$this->val = $val;
}
}
class Hand {
private $cards;
private $matches = array();
private $values = array();
private $flush;
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);
}
// get values of cards in hand
$this->values = array_map(function ($card) {
return $card->getVal();
}, $this->cards);
// get suits of cards in hand
$this->suits = array_map(function ($card) {
return $card->getSuit();
}, $this->cards);
// get pairs of threes and fours
$this->pairs = $this->threes = $this->fours = [];
// might be a straight
$this->possibleStraight = true;
$this->hands = array_count_values($this->values);
// if there are more than 2 same card values, it can't be a straight
if (max($this->hands) > 1) {
$this->possibleStraight = false;
}
// match for pairs
foreach ($this->hands as $face => $count) {
// one or two pairs
if ($count === 2) {
$this->pairs[] = $face;
}
// three of a kind
if ($count === 3) {
$this->threes[] = $face;
}
// four of a kind
if ($count === 4) {
$this->fours[] = $face;
}
}
}
public function getCards() {
return $this->cards;
}
public function getValues() {
return $this->values;
}
public function getSuits() {
return $this->suits;
}
public function getHighCard() {
return max($this->getValues());
}
public function getPairs() {
return $this->pairs;
}
public function isOnePair(){
return (count($this->pairs) == 1 ? true : false);
}
public function isTwoPair(){
return (count($this->pairs) == 2 ? true : false);
}
public function getThrees() {
return $this->threes;
}
public function getFours() {
return $this->fours;
}
public function hasAce() {
return isset($this->hands['14']);
}
public function isFullHouse() {
return count($this->pairs) == 1 && count($this->threes) == 1;
}
public function isFlush() {
$this->suits = array_count_values($this->getSuits());
// match flush
foreach ($this->suits as $face => $count) {
($count == 5 ? $this->flush = true : $this->flush = false);
}
return $this->flush;
}
public function isStraight() {
if (!$this->possibleStraight) {
return false;
}
$cards = $this->getValues();
if (array_search('14', $cards) !== FALSE &&
array_search(2, $cards) !== FALSE &&
array_search(3, $cards) !== FALSE &&
array_search(4, $cards) !== FALSE &&
array_search(5, $cards) !== FALSE ) {
$this->straight = true;
} else {
$this->straight = false;
} else if (max($cards) - min($cards) == 4) {
$this->straight = true;
} else {
$this->straight = false;
}
return $this->straight;
}
}
echo "First Card: 2s 3h 4h 5h As\n\n";
$h = new Hand('2s 3h 4h 5h As');
var_dump($h->getHighCard());
var_dump($h->isOnePair());
var_dump($h->isTwoPair());
var_dump($h->getThrees());
var_dump($h->getFours());
var_dump($h->isFullHouse());
var_dump($h->isFlush());
var_dump($h->isStraight());
echo "\n\nSecond Card: 2s 3h 4h 5h 6s\n\n";
$h = new Hand('2s 3h 4h 5h 6s');
var_dump($h->getHighCard());
var_dump($h->isOnePair());
var_dump($h->isTwoPair());
var_dump($h->getThrees());
var_dump($h->getFours());
var_dump($h->isFullHouse());
var_dump($h->isFlush());
var_dump($h->isStraight());
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.3.4 0.008 0.004 14.48 7.3.3 0.011 0.003 14.61 7.3.2 0.012 0.003 14.71 7.3.1 0.013 0.010 14.84 7.3.0 0.008 0.004 14.63 7.2.17 0.012 0.008 14.58 7.2.16 0.006 0.006 14.80 7.2.15 0.023 0.007 14.93 7.2.14 0.004 0.021 14.85 7.2.13 0.009 0.003 14.63 7.2.12 0.012 0.015 14.91 7.2.11 0.011 0.003 14.97 7.2.10 0.008 0.008 14.74 7.2.9 0.015 0.009 14.86 7.2.8 0.011 0.003 15.04 7.2.7 0.010 0.016 14.96 7.2.6 0.012 0.012 14.93 7.2.5 0.013 0.012 14.98 7.2.4 0.006 0.010 14.79 7.2.3 0.011 0.008 14.86 7.2.2 0.015 0.008 14.64 7.2.1 0.006 0.006 14.79 7.2.0 0.009 0.003 14.63 7.1.28 0.018 0.003 13.81 7.1.27 0.012 0.003 13.90 7.1.26 0.007 0.007 13.90 7.1.25 0.006 0.006 13.78
preferences:dark mode live preview
146.07 ms | 1394 KiB | 7 Q