GitList
Repositories
Help
Report an Issue
blackjack
Code
Commits
Branches
Tags
Search
Tree:
d7b0224
Branches
Tags
master
blackjack
Objects
Hand.php
initial commit
Dev Ghai
commited
d7b0224
at 2014-04-17 16:55:27
Hand.php
Blame
History
Raw
<?php /** * User: dev * Date: 3/13/14 * Time: 9:21 AM */ namespace Blackjack\Objects; require_once 'Card.php'; require_once 'Error.php'; define('BJ_HAND_LOST', 1023); define('BJ_HAND_IN_PROGRESS', 1024); define('BJ_HAND_STAND', 1025); define('BJ_HAND_DRAW', 1026); define('BJ_HAND_WON', 1027); class Hand { //Cards and sum is public so that json_encode can serialize it public $cards = array(); //Sum has to be kept public because value of 'A' can change. public $sum = 0; public $handState = BJ_HAND_IN_PROGRESS; private $isHard = true; private $isSplit = false; private $isLocked = false; //Aces change their value depending on hand. //Instead of searching the hand every time when a card is added, //store ace indexes to revisit them when sum is greater than 21. private $aceIndexes = array(); public function AddCard(Card $card) { if($this->isLocked) return new Error(E_BJ_HAND_LOCKED); elseif($this->handState != BJ_HAND_IN_PROGRESS) return new Error(E_BJ_HAND_UNPLAYABLE); if($card != null) array_push($this->cards, $card); $this->sum += $card->valueActual; //IF we are dealt an Ace, decide what the value of hand should be //while considering all the aces in the hand. //Following are blackjacks //11 aces //10, 1 ace //10, ace, 10 //8, ace, J if($card->valueFace == 'A') { array_push($this->aceIndexes, count($this->cards) - 1); //Check if treating this card as 11 would help us or not. if($this->sum + 10 <= 21) { $this->isHard = false; $card->valueActual += 10; $this->sum += 10; } } elseif($this->sum > 21 && count($this->aceIndexes) > 1) { //If sum is going over 21, and there are any aces in the hand //reduce their value to live happily ever after. for($aceIndex =0; $aceIndex < count($this->aceIndexes) && $this->sum > 21; $aceIndex++) { //no need to reduce value of ace if it is already 1. if($this->cards[$aceIndex]->valueActual == 11) { $this->cards[$aceIndex]->valueActual -= 10; $this->sum -= 10; } //enf of if } //end of for loop } // end of elseif //if sum has gone over 21, set the state to lost if($this->sum > 21) $this->handState = BJ_HAND_LOST; return true; } // end of function AddCard public function CanSplit() { return !$this->isLocked && (count($this->cards) == 2) && ($this->cards[0]->valueFace == $this->cards[1]->valueFace) && $this->handState == BJ_HAND_IN_PROGRESS; } public function GetSum() { return $this->sum; } public function IsBlackjack() { if (count($this->cards) == 2 && $this->sum == 21 && !$this->isSplit) return true; return false; } public function IsHard() { return $this->isHard; } public function IsSoft() { return !$this->isHard; } public function Split() { if (!$this->CanSplit()) { $error = new Error(E_BJ_CANNOT_SPLIT); return array($error, $error); } $this->isSplit = true; $cardForNewHand = array_pop($this->cards); $newHand = new Hand(true); $newHand->AddCard($cardForNewHand); return array($this, $newHand); } public function IsSplit() { return $this->isSplit; } public function IsBusted() { return $this->sum > 21; } public function IsLocked() { return $this->isLocked; } public function SetLocked() { $this->isLocked = true; } public function Lock() { $this->isLocked = true; } public function SetState($stateConstant) { //That's why lack of types suck... because you have to do with hacks like if statements :/ //Otherwise enum would have been so cool here!! if($stateConstant < BJ_HAND_LOST || $stateConstant > BJ_HAND_WON) return new Error(E_BJ_UNKNOWN_HAND_STATE); $this->handState = $stateConstant; return true; } public function GetState() { return $this->handState; } public function __construct($isSplit = false) { $this->isSplit = $isSplit; } }