-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeck.php
69 lines (50 loc) · 1.79 KB
/
Deck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
Class BlackJackDeck
{
private static $sdeck = array( '2','3','4','5','6','7','8','9','10','J','Q','K','A' );
const SHUFFLES = 3 ;
private $deck = array();
private $decks;
public static function getBase()/*{{{*/
{
return self::$sdeck ;
}/*}}}*/
public function __construct( $decks = 8 )/*{{{*/
{
$this->decks = $decks;
for ( $i = 0; $i < $this->decks * 4; $i++ )
$this->deck = array_merge( $this->deck, self::$sdeck );
$this->shuffle();
// Sample stacked decks
// $this->deck = array( 'A', 'K', 'K', '6', '2', '5', '9', '10', 'J' ); // user black jack
// $this->deck = array( '5', '5', 'K', 'K', 'K', 'K', '9', '10', 'J' ); // user bust
// $this->deck = array( 'K', 'K', 'K', 'K', 'K', 'K', '9', '10', 'J' ); // push
// $this->deck = array( '9', 'K', 'K', 'K', 'K', 'K', '9', '10', 'J' ); // user loses
}/*}}}*/
private function shuffle()/*{{{*/
{
for ( $i = 0; $i < self::SHUFFLES; $i++ )
shuffle( $this->deck );
}/*}}}*/
public function getValue( $card )/*{{{*/
{
if ( $card === 'A' ) return 1;
if ( in_array( $card, array('J', 'Q', 'K' ) ) ) return 10;
return $card;
}/*}}}*/
public function isCardAvailable()/*{{{*/
{
return count($this->deck) > 0 ;
}/*}}}*/
public function getCardsRemaining()/*{{{*/
{
return count($this->deck) ;
}/*}}}*/
public function draw()/*{{{*/
{
if ( !$this->isCardAvailable() ) throw new exception("Out of cards!");
$card = array_shift( $this->deck );
//BlackJackLog::out( BlackJackLog::DECK, "Drew card $card" ) ;
return $card;
}/*}}}*/
}