-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHand.php
216 lines (157 loc) · 6.13 KB
/
Hand.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
Class BlackJackHand
{
private $hidden = '';
private $deck ;
private $shown = array();
private $bet ;
private $split = null ;
private $players ;
private $show = false;
public function __construct( $players, $bet, $deck, $first, $show = false )/*{{{*/
{
$this->players = $players;
if ( $bet !== null )
$this->bet = $bet ;
$this->deck = $deck ;
$this->hidden = $first ;
$this->show = $show ;
}/*}}}*/
public function dealer( $amt )/*{{{*/
{
if ( $this->bet === null ) return ;
if ( $this->split !== null ) $this->split->dealer( $amt );
if ( $this->isBlackJack() ) return $this->bet->blackjack();
list($soft, $value) = $this->getValue( false );
$this->bet->dealer( $value, $amt );
}/*}}}*/
public function dealerBust( )/*{{{*/
{
if ( $this->bet === null ) return ;
if ( $this->split !== null ) $this->split->dealerBust( );
list($soft, $value) = $this->getValue( false );
$this->bet->dealerBust( $value );
}/*}}}*/
public function dealerBlackJack( )/*{{{*/
{
if ( $this->bet === null ) return ;
if ( $this->split !== null ) $this->split->dealerBlackJack( $amt );
$this->bet->dealerBlackJack( $this->isBlackJack() );
}/*}}}*/
public function blackJack( )/*{{{*/
{
if ( $this->bet === null ) return ;
$this->bet->blackjack();
}/*}}}*/
public function hit( $hideHit = false )/*{{{*/
{
if ( $this->surrendered ) return ;
if ( $this->doubled ) return ;
if ( count($this->shown ) > 0 && !$hideHit)
BlackJackLog::out( BlackJackLog::PLAY, "Hit..." );
$this->shown[] = $d = $this->deck->draw();
foreach ( $this->players as $player )
$player->revealcard( $d );
if ( $this->show )
BlackJackLog::out( BlackJackLog::PLAY, "Hand is now ". $this->hidden . " ". implode (' ', $this->shown ) . ' => '. $this->getValue(false)[1] );
else
BlackJackLog::out( BlackJackLog::PLAY, "Hand is now X ". implode (' ', $this->shown ) );
return $d;
}/*}}}*/
private $surrendered = false;
public function surrender( )/*{{{*/
{
$this->surrendered = true ;
$this->bet->surrender ( );
$this->bet = null;
}/*}}}*/
public function isSurrendered()/*{{{*/
{
return $this->surrendered ;
}/*}}}*/
public function revealcards()/*{{{*/
{
$this->show = true ;
foreach ( $this->players as $player )
$player->revealcard( $this->hidden );
}/*}}}*/
public function getShown() /*{{{*/
{
return $this->shown[0];
}/*}}}*/
public function getValue( $exceptOnBust = true )/*{{{*/
{
$value = $this->deck->getValue( $this->hidden );
$isSoft = $this->hidden === 'A' ;
foreach ( $this->shown as $card )
{
if ( $card === 'A' ) $isSoft = true;
$value += $this->deck->getValue( $card );
}
if ( $value <= 11 && $isSoft )
return array( true, $value + 10 );
if ( $value > 21 && $exceptOnBust ) throw new BlackJackBust("Hand value is $value");
return array( false, $value ) ;
}/*}}}*/
public function isBlackJack()/*{{{*/
{
if ( count($this->shown) !== 1 ) return false;
list( $soft, $value ) = $this->getValue(false) ;
return $value === 21 && $soft ;
}/*}}}*/
public function getCards()/*{{{*/
{
return array_merge( array($this->hidden) , $this->shown );
}/*}}}*/
public function split( $dealer, $others )/*{{{*/
{
if ( $this->surrendered ) return ;
if ( !$this->isSplitAllowed() ) throw new exception("You can't split now!");
BlackJackLog::out( BlackJackLog::PLAY, "Splitting on ". implode(' ', $this->getCards() ) );
$this->split = new BlackJackHand( $this->players, New BlackJackBet( $this->bet->getGame(), $this->bet->getPlayer(), $this->bet->getBet() ),
$this->deck,
$this->shown[0], true );
$this->split->hit();
BlackJackLog::out( BlackJackLog::PLAY, "============== Split hand ============");
$this->bet->getPlayer()->deal( $dealer, $others, $this->split );
BlackJackLog::out( BlackJackLog::PLAY, "============== Done Split ============");
$this->shown = array();
$this->hit();
}/*}}}*/
private $doubled = false;
public function doubleOrStand()/*{{{*/
{
$this->double( false );
}/*}}}*/
public function double($hit = true)/*{{{*/
{
if ( $this->surrendered ) return ;
if ( !$this->isDoubleAllowed() )
return $hit ? $this->hit() : null ;
BlackJackLog::out( BlackJackLog::PLAY, "Player doubled his bet...");
$card = $this->hit(true);
if ( count( $this->shown ) === 2 )
{
$this->doubled = true;
$this->bet->double();
}
$this->getValue();
return $card;
}/*}}}*/
public function isDoubleAllowed() /*{{{*/
{
if ( count($this->shown) !== 1 ) return false;
if ( $this->bet->getBet() > $this->bet->getPlayer()->getMoney() )
BlackJackLog::out( BlackJackLog::PLAY, "User wants to double, but can't because we don't have enough money!" );
else
return true ;
} /*}}}*/
public function isSplitAllowed()/*{{{*/
{
if ( !( count( $this->shown ) === 1 && $this->shown[0] === $this->hidden ) ) return false ;
if ( $this->bet->getBet() > $this->bet->getPlayer()->getMoney() )
BlackJackLog::out( BlackJackLog::PLAY, "User wants to split, but can't because we don't have enough money!" );
else
return true ;
}/*}}}*/
}