-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInsurance.php
57 lines (46 loc) · 1.81 KB
/
Insurance.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
<?php
Class BlackJackInsurance
{
const PAYOUT = 2;
private $player ;
private $bet ;
public function __construct( $player, $bet )/*{{{*/
{
$this->player = $player;
$this->bet = $bet ;
}/*}}}*/
public function payout()/*{{{*/
{
$this->player->pay( $this->bet * self::PAYOUT );
}/*}}}*/
public static function check( $game, BlackJackHand $dealerHand, array $players, array $bets )/*{{{*/
{
if ( $dealerHand->getShown() !== 'A' ) return false;
$insurance = array();
foreach ( $players as $k => $player )
{
$cost = ceil( $bets[$k]->getBet() / self::PAYOUT );
if ( $player->getMoney() < $cost )
BlackJackLog::out( BlackJackLog::INSURANCE, "Player $k can't afford insurance" );
elseif ( $player->wantInsurance( $game, $cost ) )
{
$player->pay( 0 - $cost );
BlackJackLog::out( BlackJackLog::INSURANCE, "Player $k took insurance" );
$insurance[] = new self( $player, $cost );
}
else
BlackJackLog::out( BlackJackLog::INSURANCE, "Player $k refused insurance" );
}
list( $soft, $value ) = $dealerHand->getValue() ;
if ( $value === 21 )
{
BlackJackLog::out( BlackJackLog::INSURANCE, "Paying insurance to ".count($insurance)." players" );
foreach ( $insurance as $ins )
{
$ins->payout();
}
}
elseif ( count($insurance ) )
BlackJackLog::out( BlackJackLog::INSURANCE, "No blackjack, not paying anything suckers! ".count($insurance)." suckers") ;
}/*}}}*/
}