<?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 = false;
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 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 isStraight() {
if (!$this->possibleStraight) {
return false;
}
//
if ($this->hasAce()) {
}
}
public function isFlush() {
$this->suits = array_count_values($this->getSuits());
// match flush
foreach ($this->suits as $face => $count) {
if ($count == 5) {
$this->flush = true;
}
}
return $this->flush;
}
}
$h = new Hand('Ah As 10c 7d 6s');
var_dump($h->getHighCard());
var_dump($h->getPairs());
var_dump($h->getThrees());
var_dump($h->getFours());
var_dump($h->isFullHouse());
var_dump($h->isFlush());
Deprecated: Creation of dynamic property Hand::$suits is deprecated in /in/4s1Pd on line 63
Deprecated: Creation of dynamic property Hand::$fours is deprecated in /in/4s1Pd on line 68
Deprecated: Creation of dynamic property Hand::$threes is deprecated in /in/4s1Pd on line 68
Deprecated: Creation of dynamic property Hand::$pairs is deprecated in /in/4s1Pd on line 68
Deprecated: Creation of dynamic property Hand::$possibleStraight is deprecated in /in/4s1Pd on line 71
Deprecated: Creation of dynamic property Hand::$hands is deprecated in /in/4s1Pd on line 73
int(14)
array(1) {
[0]=>
int(14)
}
array(0) {
}
array(0) {
}
bool(false)
bool(false)