Skip to content

Instantly share code, notes, and snippets.

@danmandle
Last active August 29, 2015 13:56
Show Gist options
  • Save danmandle/e73f34f7911cf1a8dfdd to your computer and use it in GitHub Desktop.
Save danmandle/e73f34f7911cf1a8dfdd to your computer and use it in GitHub Desktop.
Rally Software Coding Exercise by Dan Mandle
<?php
/*
NOTE: This is designed to be run from the terminal.
------------
-Exercise 2-
------------
Write some code that will evaluate a poker hand and determine its rank.
*/
class pokerRound{
public $deck;
function __construct(){
$this->shuffleDeck();
}
public function shuffleDeck(){
// populate deck
$suits = array('h', 's', 'd', 'c');
$values = array_merge(range(2, 10), array('J','Q','K','A'));
foreach($suits as $suit){
foreach($values as $value){
$this->deck[] = $value.$suit;
}
}
shuffle($this->deck);
return $this->deck;
}
public function dealHands($num_players = 1){
if(sizeof($this->deck) < 5){
echo "Too many players, not enough cards left.";
exit;
}
// in order to properly deal, each player has to get one card, then two cards, etc.
$hands = array();
while(sizeof($hands[$num_players]) < 5){ // while the last hand has less than 5 cards
for($player=1; $player <= $num_players; $player++){
$hands[$player][] = array_shift($this->deck); // grab a card from the front of the array
}
}
return $hands;
}
public function evaluateHand($cards){
$hand = array();
foreach($cards as $card){
$suit = substr($card, -1); // last char is suit
$card = substr($card, 0, -1); // everything that's not the suit is the value
empty($hand['suits'][$suit]) ? $hand['suits'][$suit] = 0 : NULL; // squash PHP notices
$hand['suits'][$suit] += 1;
switch($card){
case 'J':
$card = 11;
break;
case 'Q':
$card = 12;
break;
case 'K':
$card = 13;
break;
case 'A':
$card = 14;
break;
}
empty($hand['values'][$card]) ? $hand['values'][$card] = 0 : NULL; // squash PHP notices
$hand['values'][$card] += 1;
}
ksort($hand['values']);
$values = array_keys($hand['values']);
$of_a_kind = max($hand['values']);
if($of_a_kind == 1){
$straight = true;
for($i=0; $i < sizeof($values)-1; $i++){
if($values[$i]+1 != $values[$i+1]){
$straight = false;
break;
}
}
}
else{
$straight = false;
}
// $flush = max($hand['suits']) == 5 ? true : false;
$flush = sizeof($hand['suits']) == 1 ? true : false;
if($straight && $flush){
// Straight Flush
return 'sf';
}
elseif($of_a_kind == 4){
// Four of a Kind
return '4ok';
}
elseif($of_a_kind == 3 && in_array(2, $hand['values'])){
// full house
return 'fh';
}
elseif($flush){
// yup, flush
return 'fl';
}
elseif($straight){
// straight
return 'st';
}
elseif($of_a_kind == 3){
// three of a kind
return '3ok';
}
elseif(sizeof(array_keys($values, 2)) == 2){
// two pairs
return '2p';
}
elseif($of_a_kind == 2){
// Pair
return '2ok';
}
else{
// High Card
return 'hc';
}
}
public function printHandValue($hand_code){
switch($hand_code){
case 'sf':
echo PHP_EOL."Straight Flush! Top Hand!".PHP_EOL;
break;
case '4ok':
echo PHP_EOL."Four of a Kind. Second best hand!".PHP_EOL;
break;
case 'fh':
echo PHP_EOL."Full House! Third best hand".PHP_EOL;
break;
case 'fl':
echo PHP_EOL."Flush! Fourth best.".PHP_EOL;
break;
case 'st':
echo PHP_EOL."Straight".PHP_EOL;
break;
case '3ok':
echo PHP_EOL."Three of a kind".PHP_EOL;
break;
case '2p':
echo PHP_EOL."Two Pair".PHP_EOL;
break;
case '2ok':
echo PHP_EOL."Pair".PHP_EOL;
break;
case 'hc':
echo PHP_EOL."High Card. Better luck next time...".PHP_EOL;
break;
}
}
}
$pokerRound = new pokerRound();
$hands = $pokerRound->dealHands(5);
foreach($hands as $hand){
for($i=0; $i<5; $i++){
echo $hand[$i];
$i<4 ? print(",") : print(PHP_EOL);
}
$pokerRound->printHandValue($pokerRound->evaluateHand($hand));
echo PHP_EOL.'======================='.PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment