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; } }