@ 2019-04-26T19:48:20Z <?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 evalHand() {
$this->getHighCard();
$this->isOnePair();
$this->isTwoPair();
$this->getThrees();
$this->getFours();
$this->isFullHouse();
$this->isFlush();
$this->isStraight();
$this->isStraightFlush();
$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();
echo "\n\nSecond Card: 2s 3h 4h 5h 6s\n\n";
$h = new Hand('2s 3h 4h 5h 6s');
$h->evalHand();
$h->getRank();
echo "\n\nThird Card: 2h 3h 4h 5h 6h\n\n";
$h = new Hand('2h 3h 4h 5h 6h');
$h->evalHand();
$h->getRank();
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.4 0.012 0.003 19.03 8.3.3 0.008 0.008 19.02 8.3.2 0.008 0.000 21.73 8.3.1 0.004 0.004 21.76 8.3.0 0.005 0.003 21.71 8.2.17 0.007 0.014 22.96 8.2.16 0.007 0.007 20.60 8.2.15 0.005 0.003 24.18 8.2.14 0.003 0.006 24.66 8.2.13 0.009 0.000 19.95 8.2.12 0.008 0.000 26.35 8.2.11 0.010 0.013 22.13 8.2.10 0.009 0.003 17.97 8.2.9 0.003 0.006 18.09 8.2.8 0.005 0.003 18.29 8.2.7 0.000 0.009 18.18 8.2.6 0.003 0.006 18.47 8.2.5 0.004 0.004 18.10 8.2.4 0.004 0.004 19.61 8.2.3 0.004 0.004 20.50 8.2.2 0.002 0.005 18.09 8.2.1 0.000 0.008 19.41 8.2.0 0.008 0.000 18.27 8.1.27 0.008 0.000 23.99 8.1.26 0.008 0.000 26.35 8.1.25 0.004 0.004 28.09 8.1.24 0.003 0.006 22.32 8.1.23 0.012 0.000 22.75 8.1.22 0.004 0.004 17.91 8.1.21 0.008 0.000 18.77 8.1.20 0.007 0.003 17.47 8.1.19 0.003 0.005 17.48 8.1.18 0.006 0.003 18.10 8.1.17 0.008 0.000 18.76 8.1.16 0.003 0.005 18.91 8.1.15 0.003 0.006 18.91 8.1.14 0.008 0.000 19.23 8.1.13 0.004 0.004 17.43 8.1.12 0.000 0.007 17.57 8.1.11 0.004 0.007 17.59 8.1.10 0.009 0.000 17.61 8.1.9 0.003 0.006 17.48 8.1.8 0.004 0.004 17.48 8.1.7 0.000 0.009 17.55 8.1.6 0.008 0.004 17.61 8.1.5 0.003 0.006 17.61 8.1.4 0.004 0.004 17.60 8.1.3 0.003 0.006 17.63 8.1.2 0.000 0.008 17.69 8.1.1 0.000 0.008 17.66 8.1.0 0.003 0.005 17.61 8.0.30 0.005 0.003 19.65 8.0.29 0.003 0.006 16.88 8.0.28 0.000 0.007 18.56 8.0.27 0.004 0.004 17.41 8.0.26 0.000 0.007 16.99 8.0.25 0.003 0.003 17.00 8.0.24 0.007 0.000 17.12 8.0.23 0.007 0.000 17.14 8.0.22 0.000 0.007 16.93 8.0.21 0.000 0.008 17.02 8.0.20 0.003 0.005 17.06 8.0.19 0.000 0.009 17.10 8.0.18 0.004 0.004 16.98 8.0.17 0.005 0.003 17.09 8.0.16 0.003 0.006 17.00 8.0.15 0.000 0.008 16.93 8.0.14 0.003 0.006 17.04 8.0.13 0.000 0.007 13.48 8.0.12 0.006 0.003 17.04 8.0.11 0.003 0.006 16.95 8.0.10 0.003 0.005 16.92 8.0.9 0.004 0.004 17.00 8.0.8 0.007 0.012 16.96 8.0.7 0.004 0.004 17.11 8.0.6 0.006 0.003 17.01 8.0.5 0.005 0.002 16.88 8.0.3 0.015 0.004 17.45 8.0.2 0.018 0.007 17.48 8.0.1 0.004 0.004 17.19 8.0.0 0.012 0.007 16.93 7.4.33 0.000 0.005 15.55 7.4.32 0.005 0.002 16.76 7.4.30 0.004 0.004 16.65 7.4.29 0.008 0.000 16.70 7.4.28 0.003 0.003 16.75 7.4.27 0.003 0.007 16.46 7.4.26 0.004 0.004 16.70 7.4.25 0.000 0.007 16.47 7.4.24 0.007 0.000 16.72 7.4.23 0.000 0.007 16.75 7.4.22 0.005 0.003 16.55 7.4.21 0.008 0.006 16.64 7.4.20 0.003 0.005 16.55 7.4.13 0.006 0.021 16.53 7.4.12 0.012 0.006 16.66 7.4.11 0.006 0.016 16.63 7.4.10 0.011 0.011 16.75 7.4.9 0.009 0.009 16.68 7.4.8 0.011 0.007 19.39 7.4.7 0.011 0.011 16.68 7.4.6 0.003 0.016 16.46 7.4.5 0.004 0.011 16.59 7.4.4 0.013 0.003 16.65 7.4.0 0.009 0.006 14.81 7.3.33 0.000 0.005 13.47 7.3.32 0.003 0.003 13.32 7.3.31 0.004 0.004 16.33 7.3.30 0.007 0.000 16.42 7.3.29 0.009 0.006 16.41 7.3.28 0.011 0.013 16.44 7.3.26 0.014 0.007 16.57 7.3.24 0.010 0.009 16.56 7.3.23 0.009 0.009 16.49 7.3.21 0.013 0.003 16.37 7.3.20 0.006 0.013 16.71 7.3.19 0.017 0.003 16.39 7.3.18 0.003 0.013 16.43 7.3.17 0.006 0.011 16.44 7.3.16 0.009 0.013 16.63 7.3.4 0.006 0.006 14.90 7.3.3 0.009 0.016 14.68 7.3.2 0.006 0.006 16.65 7.3.1 0.007 0.016 16.41 7.3.0 0.008 0.004 16.84 7.2.33 0.017 0.007 16.51 7.2.32 0.006 0.012 16.82 7.2.31 0.007 0.010 16.68 7.2.30 0.007 0.010 16.75 7.2.29 0.007 0.016 16.55 7.2.17 0.014 0.000 15.23 7.2.16 0.000 0.014 15.12 7.2.15 0.009 0.003 16.68 7.2.14 0.007 0.007 16.71 7.2.13 0.004 0.008 17.09 7.2.12 0.013 0.008 16.99 7.2.11 0.013 0.003 16.88 7.2.10 0.003 0.009 16.83 7.2.9 0.006 0.006 17.05 7.2.8 0.008 0.004 16.71 7.2.7 0.015 0.006 16.99 7.2.6 0.019 0.006 16.80 7.2.5 0.022 0.000 16.85 7.2.4 0.011 0.011 16.75 7.2.3 0.011 0.004 16.71 7.2.2 0.009 0.003 16.94 7.2.1 0.006 0.017 17.00 7.2.0 0.000 0.015 16.92 7.1.28 0.004 0.011 15.98 7.1.27 0.003 0.009 15.84 7.1.26 0.003 0.008 15.67 7.1.25 0.012 0.009 15.89
preferences:dark mode live preview
31 ms | 400 KiB | 5 Q