Last active
May 24, 2018 10:39
-
-
Save sujeetkv/f9d9f022770249bd03bc23d7134302d7 to your computer and use it in GitHub Desktop.
Simple Cards Fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Card class | |
*/ | |
class Card | |
{ | |
private $deck = null; | |
private $pos = null; | |
private $suit = ''; | |
private $rank = ''; | |
public function __construct($suit, $rank, $pos, $deck) { | |
$this->suit = $suit; | |
$this->rank = $rank; | |
$this->pos = $pos; | |
$this->deck = $deck; | |
} | |
public function curr() { | |
return $this; | |
} | |
public function next() { | |
return $this->deck->get($this->pos + 1); | |
} | |
public function prev() { | |
return $this->deck->get($this->pos - 1); | |
} | |
public function getSuit() { | |
return $this->suit; | |
} | |
public function getRank() { | |
return $this->rank; | |
} | |
public function getIndex() { | |
return $this->pos; | |
} | |
public function getPosition() { | |
return ($this->pos + 1); | |
} | |
public function getArray() { | |
return array($this->suit, $this->rank); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Deck class | |
*/ | |
class Deck | |
{ | |
private $sampleSpace = array( | |
'suits' => array('Clubs', 'Diamonds', 'Hearts', 'Spades'), | |
'ranks' => array('Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King') | |
); | |
private $sample = array(); | |
private $minCount = 4; | |
private $cards = array(); | |
public function __construct($count = null) { | |
$this->createSample(); | |
$this->pickCards($count); | |
} | |
public function getRandomCard() { | |
return $this->cards[array_rand(array_keys($this->cards))]; | |
} | |
public function getTopCard() { | |
return $this->cards[0]; | |
} | |
public function getBottomCard() { | |
return $this->cards[($this->getCount() - 1)]; | |
} | |
public function getCount() { | |
return count($this->cards); | |
} | |
public function get($index) { | |
return isset($this->cards[$index]) ? $this->cards[$index] : null; | |
} | |
public function getAllCards($asArray = false) { | |
$cards = array(); | |
foreach ($this->cards as $card) { | |
$cards[] = $asArray ? $card->getArray() : $card; | |
} | |
return $cards; | |
} | |
public function getSample() { | |
return $this->sample; | |
} | |
public function shuffle() { | |
$cards = $this->cards; | |
$this->cards = array(); | |
shuffle($cards); | |
for ($i = 0; $i < count($cards); $i++) { | |
$this->cards[$i] = new Card($cards[$i]->getSuit(), $cards[$i]->getRank(), $i, $this); | |
} | |
} | |
private function createSample() { | |
foreach ($this->sampleSpace['suits'] as $suit) { | |
foreach ($this->sampleSpace['ranks'] as $rank) { | |
$this->sample[] = array($suit, $rank); | |
} | |
} | |
} | |
private function pickCards($count = null) { | |
$tmpSample = $this->sample; | |
shuffle($tmpSample); | |
if ($count) { | |
$deckCount = min(count($tmpSample), $count); | |
} else { | |
$deckCount = mt_rand($this->minCount, count($tmpSample)); | |
} | |
for ($i = 0; $i < $deckCount; $i++) { | |
$this->cards[$i] = new Card($tmpSample[$i][0], $tmpSample[$i][1], $i, $this); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//$deck = new Deck(15); | |
$deck = new Deck(); | |
//$card = $deck->getTopCard(); | |
//$card = $deck->getBottomCard(); | |
$card = $deck->getRandomCard(); | |
echo 'Curr Card: '; | |
print_r($card->getArray()); | |
echo '<br>'; | |
echo 'Curr Card Position: ' . $card->getPosition() . '<br><br>'; | |
if ($prevCard = $card->prev()) { | |
echo 'Prev Card: '; | |
print_r($prevCard->getArray()); | |
echo '<br>'; | |
} | |
if ($nextCard = $card->next()) { | |
echo 'Next Card: '; | |
print_r($nextCard->getArray()); | |
echo '<br>'; | |
} | |
echo '<br>Deck Count: ' . $deck->getCount(); | |
echo '<pre>'; | |
print_r($deck->getAllCards(true)); | |
echo '</pre>'; | |
$deck->shuffle(); | |
echo '<pre>'; | |
print_r($deck->getAllCards(true)); | |
echo '</pre>'; | |
echo '<br>Sample Space: '; | |
echo '<pre>'; | |
print_r($deck->getSample()); | |
echo '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment