@ 2019-04-26T20:17:50Z <?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;
private $rank;
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 __destruct() {
}
public function evalHand() {
// $this->isRoyalFlush();
// $this->isStraightFlush();
// $this->getFours();
// $this->isFullHouse();
// $this->isFlush();
// $this->isStraight();
// $this->getThrees();
// $this->isTwoPair();
// $this->isOnePair();
// $this->getHighCard();
var_dump($this->getHighCard());
var_dump($this->isOnePair());
var_dump($this->isTwoPair());
var_dump($this->getThrees());
var_dump($this->getFours());
var_dump($this->isFullHouse());
var_dump($this->isFlush());
var_dump($this->isStraight());
var_dump($this->isStraightFlush());
var_dump($this->isRoyalFlush());
}
public function getRank() {
echo $this->rank;
}
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() {
if (count($this->pairs) == 1) {
$this->rank = 'One Pair';
}
return (count($this->pairs) == 1 ? true : false);
}
public function isTwoPair() {
if (count($this->pairs) == 2) {
$this->rank = 'Two Pair';
}
return (count($this->pairs) == 2 ? true : false);
}
public function getThrees() {
$this->rank = 'Three of a Kind';
return $this->threes;
}
public function getFours() {
$this->rank = 'Four of a Kind';
return $this->fours;
}
public function hasAce() {
return isset($this->hands['14']);
}
public function isFullHouse() {
if (count($this->pairs) == 1 && count($this->threes) == 1) {
$this->rank = 'Flush';
}
return count($this->pairs) == 1 && count($this->threes) == 1;
}
public function isFlush() {
$suits = array_count_values($this->getSuits());
// match flush
foreach ($suits as $face => $count) {
if ($count == 5) {
$this->flush = true;
$this->rank = 'Flush';
} else {
$this->flush = false;
}
}
return $this->flush;
}
public function isStraightAce() {
$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;
}
return $this->straight;
}
public function isStraightRegular() {
$cards = $this->getValues();
if (max($cards) - min($cards) == 4) {
$this->straight = true;
} else {
$this->straight = false;
}
return $this->straight;
}
public function isStraight() {
if ($this->isStraightAce()) {
$this->straight = true;
$this->rank = 'Straight';
} else if ($this->isStraightRegular()) {
$this->straight = true;
$this->rank = 'Straight';
} else {
$this->straight = false;
}
return $this->straight;
}
public function isStraightFlush() {
if ($this->isFlush() && $this->isStraight()) {
$this->rank = 'Straight Flush';
};
return ($this->isFlush() && $this->isStraight());
}
public function isRoyal() {
$cards = $this->getValues();
if (array_search(10, $cards) !== FALSE &&
array_search(11, $cards) !== FALSE &&
array_search(12, $cards) !== FALSE &&
array_search(13, $cards) !== FALSE &&
array_search(14, $cards) !== FALSE) {
return true;
} else {
return false;
}
}
public function isRoyalFlush() {
if ($this->isFlush() && $this->isRoyal()) {
$this->rank = 'Royal Flush';
};
return ($this->isFlush() && $this->isRoyal());
}
}
echo "First Card: 2s 3h 4h 5h As\n\n";
$h = new Hand('2s 3h 4h 5h As');
$h->evalHand();
$h->getRank();
unset($h);
echo "\n\nSecond Card: 2s 3h 4h 5h 6s\n\n";
$h = new Hand('2s 3h 4h 5h 6s');
$h->evalHand();
$h->getRank();
unset($h);
echo "\n\nThird Card: 2h 3h 4h 5h 6h\n\n";
$h = new Hand('2h 3h 4h 5h 6h');
$h->evalHand();
$h->getRank();
unset($h);
echo "\n\nFourth Card: As Ks Qs Js 10s\n\n";
$h = new Hand('As Ks Qs Js 10s');
$h->evalHand();
$h->getRank();
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) 8.3.6 0.007 0.013 18.43 8.3.5 0.011 0.009 17.64 8.3.4 0.012 0.003 18.96 8.3.3 0.015 0.000 18.91 8.3.2 0.008 0.000 21.86 8.3.1 0.008 0.000 21.86 8.3.0 0.003 0.006 21.90 8.2.18 0.012 0.003 16.75 8.2.17 0.012 0.003 22.96 8.2.16 0.012 0.003 20.29 8.2.15 0.000 0.008 24.18 8.2.14 0.004 0.004 24.66 8.2.13 0.004 0.004 26.16 8.2.12 0.004 0.004 19.83 8.2.11 0.003 0.007 22.16 8.2.10 0.008 0.004 18.34 8.2.9 0.000 0.008 19.49 8.2.8 0.000 0.008 17.97 8.2.7 0.004 0.004 17.88 8.2.6 0.003 0.005 18.16 8.2.5 0.003 0.006 18.07 8.2.4 0.000 0.009 18.36 8.2.3 0.000 0.008 18.22 8.2.2 0.008 0.000 18.22 8.2.1 0.003 0.005 19.50 8.2.0 0.003 0.005 18.14 8.1.28 0.015 0.004 25.92 8.1.27 0.004 0.004 20.25 8.1.26 0.004 0.004 26.35 8.1.25 0.004 0.004 28.09 8.1.24 0.003 0.006 23.79 8.1.23 0.008 0.004 17.82 8.1.22 0.004 0.004 18.02 8.1.21 0.004 0.004 18.77 8.1.20 0.000 0.010 17.60 8.1.19 0.003 0.006 17.25 8.1.18 0.006 0.003 18.10 8.1.17 0.004 0.011 18.27 8.1.16 0.003 0.005 19.06 8.1.15 0.000 0.007 18.99 8.1.14 0.000 0.007 19.05 8.1.13 0.000 0.007 20.27 8.1.12 0.000 0.008 17.59 8.1.11 0.009 0.000 17.50 8.1.10 0.000 0.007 17.42 8.1.9 0.000 0.008 17.55 8.1.8 0.003 0.005 17.49 8.1.7 0.000 0.007 17.60 8.1.6 0.006 0.003 17.74 8.1.5 0.006 0.003 17.64 8.1.4 0.006 0.003 17.63 8.1.3 0.003 0.006 17.65 8.1.2 0.003 0.006 17.61 8.1.1 0.003 0.006 17.73 8.1.0 0.003 0.005 17.48 8.0.30 0.000 0.008 18.77 8.0.29 0.003 0.006 17.00 8.0.28 0.007 0.000 18.47 8.0.27 0.005 0.003 17.27 8.0.26 0.008 0.000 18.43 8.0.25 0.000 0.007 17.16 8.0.24 0.000 0.007 17.02 8.0.23 0.004 0.004 17.13 8.0.22 0.000 0.008 17.07 8.0.21 0.004 0.004 16.92 8.0.20 0.007 0.000 17.10 8.0.19 0.006 0.003 17.04 8.0.18 0.004 0.004 16.98 8.0.17 0.005 0.003 16.97 8.0.16 0.000 0.008 17.07 8.0.15 0.004 0.004 16.95 8.0.14 0.007 0.000 16.91 8.0.13 0.003 0.003 13.44 8.0.12 0.000 0.008 16.88 8.0.11 0.000 0.007 17.13 8.0.10 0.004 0.004 16.98 8.0.9 0.004 0.004 17.01 8.0.8 0.011 0.008 16.95 8.0.7 0.000 0.008 16.88 8.0.6 0.000 0.008 16.96 8.0.5 0.000 0.008 16.91 8.0.3 0.010 0.012 17.38 8.0.2 0.015 0.006 17.45 8.0.1 0.004 0.004 17.23 8.0.0 0.011 0.010 16.80 7.4.33 0.006 0.000 16.59 7.4.32 0.000 0.006 16.71 7.4.30 0.000 0.007 16.75 7.4.29 0.004 0.004 16.75 7.4.28 0.004 0.004 16.70 7.4.27 0.003 0.007 16.75 7.4.26 0.007 0.000 16.58 7.4.25 0.000 0.007 16.64 7.4.24 0.000 0.007 16.68 7.4.23 0.000 0.007 16.36 7.4.22 0.004 0.004 16.52 7.4.21 0.007 0.008 16.71 7.4.20 0.000 0.007 16.80 7.4.13 0.027 0.000 16.38 7.4.12 0.003 0.014 16.76 7.4.11 0.012 0.006 16.70 7.4.10 0.014 0.010 16.64 7.4.9 0.013 0.005 16.82 7.4.8 0.006 0.016 19.39 7.4.7 0.011 0.008 16.61 7.4.6 0.014 0.003 16.57 7.4.5 0.009 0.006 16.77 7.4.4 0.007 0.010 16.75 7.4.0 0.005 0.011 14.95 7.3.33 0.003 0.003 13.48 7.3.32 0.006 0.000 13.33 7.3.31 0.003 0.006 16.51 7.3.30 0.004 0.004 16.52 7.3.29 0.006 0.009 16.48 7.3.28 0.005 0.013 16.47 7.3.26 0.011 0.009 16.66 7.3.24 0.011 0.010 16.54 7.3.23 0.003 0.015 16.50 7.3.21 0.010 0.007 16.78 7.3.20 0.003 0.013 16.35 7.3.19 0.009 0.009 16.55 7.3.18 0.010 0.007 16.66 7.3.17 0.016 0.003 16.75 7.3.16 0.009 0.009 16.66 7.3.12 0.010 0.010 14.71 7.3.11 0.004 0.015 15.11 7.3.10 0.009 0.003 15.13 7.3.9 0.000 0.009 14.69 7.3.8 0.007 0.007 14.66 7.3.7 0.010 0.007 15.02 7.3.6 0.004 0.007 14.71 7.3.5 0.006 0.012 14.64 7.3.4 0.009 0.004 14.81 7.3.3 0.008 0.011 14.89 7.3.2 0.009 0.005 15.90 7.3.1 0.008 0.010 15.79 7.3.0 0.017 0.005 15.82 7.2.33 0.013 0.007 16.64 7.2.32 0.007 0.017 16.78 7.2.31 0.012 0.009 16.94 7.2.30 0.006 0.011 16.89 7.2.29 0.018 0.006 16.92 7.2.25 0.007 0.008 15.28 7.2.24 0.007 0.014 14.98 7.2.23 0.007 0.010 14.95 7.2.22 0.006 0.003 15.23 7.2.21 0.004 0.015 15.30 7.2.20 0.004 0.011 15.10 7.2.19 0.010 0.003 15.37 7.2.18 0.006 0.012 15.22 7.2.17 0.007 0.014 14.97 7.2.16 0.009 0.006 15.35 7.2.15 0.007 0.011 15.28 7.2.14 0.015 0.012 15.31 7.2.13 0.012 0.006 15.29 7.2.12 0.025 0.005 15.15 7.2.11 0.020 0.006 14.96 7.2.10 0.022 0.006 15.20 7.2.9 0.015 0.000 15.20 7.2.8 0.003 0.010 15.37 7.2.7 0.029 0.000 15.27 7.2.6 0.007 0.007 15.29 7.2.5 0.010 0.003 15.30 7.2.4 0.020 0.007 15.26 7.2.3 0.012 0.006 15.24 7.2.2 0.007 0.007 14.84 7.2.1 0.021 0.006 15.27 7.2.0 0.012 0.016 15.49 7.1.33 0.013 0.003 15.81 7.1.32 0.006 0.006 15.83 7.1.31 0.008 0.008 15.95 7.1.30 0.003 0.010 15.79 7.1.29 0.006 0.006 15.86 7.1.28 0.012 0.007 14.95 7.1.27 0.009 0.005 14.93 7.1.26 0.010 0.005 14.75 7.1.25 0.086 0.000 14.08
preferences:dark mode live preview
65.02 ms | 401 KiB | 5 Q