GitList
Repositories
Help
Report an Issue
blackjack
Code
Commits
Branches
Tags
Search
Tree:
d7b0224
Branches
Tags
master
blackjack
Blackjack.php
initial commit
Dev Ghai
commited
d7b0224
at 2014-04-17 16:55:27
Blackjack.php
Blame
History
Raw
<?php /** * User: dev * Date: 3/11/14 * Time: 12:25 AM */ namespace Blackjack; require_once 'Config/Config.php'; require_once 'Objects/Shoe.php'; require_once 'Objects/Hand.php'; require_once 'Objects/Player.php'; require_once 'Objects/Dealer.php'; use Blackjack\Config\Config; use Blackjack\Objects\Error; use Blackjack\Objects\Hand; use Blackjack\Objects\Player; use Blackjack\Objects\Dealer; use Blackjack\Objects\Shoe; define('BJ_TABLE_IN_PROGRESS', 2048); define('BJ_TABLE_TURN_PLAYER', 2049); define('BJ_TABLE_TURN_DEALER', 2050); define('BJ_TABLE_ENDED', 2100); class Blackjack { /*** * @var Shoe Collection of multiple decks from which cards are dealt. */ private $shoe; /*** * @var Objects\Player Dealer of the table. */ public $dealer; /*** * @var Objects\Player Player for the table. Public to allow json_encode to serialize it. */ public $player; /*** * @var Config Configuration for this game. */ private $config; private $tableState; public function __construct(Config $config, $playerName) { $this->config = $config; $this->shoe = new Shoe($config); $this->tableState = BJ_TABLE_TURN_PLAYER; $playerHand = new Hand(); $dealerHand = new Hand(); $playerHand->AddCard($this->shoe->PickCard()); $playerHand->AddCard($this->shoe->PickCard()); $dealerHand->AddCard($this->shoe->PickCard()); $this->player = new Player($config, $playerHand, $playerName); $this->dealer = new Dealer($config, $dealerHand, $this->shoe->PickCard()); } public function GetShoe() { return $this->shoe; } public function UpdateTableState($playerHandIndex) { if($this->player->GetHand($playerHandIndex)->IsBusted()) { //Player bust, dealer wins. return Dealer Object. $this->player->GetHand($playerHandIndex)->SetState(BJ_HAND_LOST); $this->dealer->SetState(BJ_HAND_WON); } elseif($this->tableState == BJ_TABLE_TURN_DEALER) { $playerHand = $this->player->GetHand($playerHandIndex); $playerSum = $playerHand->GetSum(); $dealerSum = $this->dealer->GetSum(); while($dealerSum < 17) { $isHitSuccessful = $this->dealer->Hit($this->shoe->PickCard()); if($isHitSuccessful instanceof Error) return $isHitSuccessful; $dealerSum = $this->dealer->GetSum(); } if($dealerSum > 21) { //dealer bust, player wins $playerHand->SetState(BJ_HAND_WON); $this->dealer->SetState(BJ_HAND_LOST); } elseif($playerSum < $dealerSum) { $playerHand->SetState(BJ_HAND_LOST); $this->dealer->SetState(BJ_HAND_WON); } elseif($playerSum > $dealerSum) { $playerHand->SetState(BJ_HAND_WON); $this->dealer->SetState(BJ_HAND_LOST); } elseif($playerSum == $dealerSum) { $playerHand->SetState(BJ_HAND_DRAW); $this->dealer->SetState(BJ_HAND_DRAW); } } } public function GetTableState() { //Make sure none of the hands of the player is in Stand state. If it is, game has not ended. foreach($this->player->hands as $hand) { if($hand->GetState() == BJ_HAND_IN_PROGRESS || $hand->GetState() == BJ_HAND_STAND) return BJ_TABLE_IN_PROGRESS; } return BJ_TABLE_ENDED; } public function SetDealerTurn() { $this->tableState = BJ_TABLE_TURN_DEALER; } }