<?php
class Card {
private $suit;
private $val;
public function __construct($val, $suit) {
$values = array(
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'10' => 10,
'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];
}
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->isPair();
$this->isHighCard();
$this->isKind();
$this->isStraight();
}
public function getCards(){
return $this->cards;
}
public function getValues(){
foreach($this->cards as $card){
$output[] = $card->getVal();
}
return $output;
}
public function isHighCard(){
$output = max($this->getValues());
return $output;
}
public function isPair(){
foreach(array_count_values($this->getValues()) as $value => $count){
if ($count == 2 && count($this->matches) < 2) {
$this->matches[] = $value;
}
}
if (count($this->matches) == 1) {
echo "One pair";
}
if (count($this->matches) == 2) {
echo "Two pair";
}
}
public function isKind(){
$count = max(array_count_values($this->getValues()));
if($count == 3){
echo "Three of a kind";
}
if($count == 4){
echo "Four of a kind";
}
}
public function isStraight(){
$cards = $this->getValues();
$previous = null;
foreach($cards as $card){
if ($previous !== null && $card == $previous + 1){
echo "Straight";
}
$previous = $card;
}
}
}
$h = new Hand('6h 7d 8s 9c 10h');
// var_dump($x);
- Output for 7.1.25 - 7.1.28, 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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- StraightStraightStraightStraight
preferences:
177.49 ms | 403 KiB | 163 Q