#include "dealer.h" bool Dealer::DealCard(BlackJackHand* hand) { Card card; if (!theDeck.GetCard(&card)) throw "Bad Deck"; hand->AddCard(card); return true; } bool Dealer::ProcessTurn(BlackJackHand* hand) { bool doneturn = false; bool retval = true; BlackJackHand::TURN_RESULT turn; while (!doneturn) { turn = hand->TakeTurn(); switch (turn) { case BlackJackHand::HIT: DealCard(hand); break; case BlackJackHand::BUST: retval = false; // allow fall through case BlackJackHand::STAND: doneturn = true; break; case BlackJackHand::DOUBLE_DOWN: DealCard(hand); retval = hand->HighCount() > 21; // did we bust doneturn = true; break; case BlackJackHand::SPLIT: throw "Not implemented"; // break; default: throw "Illegal result of move"; } } return retval; } Dealer::Dealer(unsigned int numDecks, unsigned int cutCardPosition): dealer(0), numDecks(numDecks), cutCardPosition(cutCardPosition) { } Dealer::~Dealer() { } void Dealer::AssignPlayerHand(BlackJackHand* hand) { players.push_back(hand); } void Dealer::SetDealerHand(BlackJackHand* hand) { dealer = hand; } Dealer::STATUS Dealer::GetPlayerStatus(const BlackJackHand* hand) const { Dealer::STATUS status = LOSE; unsigned int pCount = hand->HighCount(); unsigned int dCount = dealer->HighCount(); // First we handle blackjack as a special case if (hand->IsBlackJack()) { if (hand->TookInsurance()) status = WIN; else if (dealer->IsBlackJack()) status = PUSH; else status = BLACKJACK; } else if (pCount<= 21) { // We didn't bust if (dCount > 21 || pCount > dCount) { // dealer bust or we won status = WIN; } else if (dCount == pCount) // push status = PUSH; // otherwise the dealer beat us which is the default else { assert (dCount > pCount); } } return status; } void Dealer::NewShoe() { theDeck.BurnIt(); theDeck.AddDeck(); theDeck.Shuffle(); } void Dealer::Play() { if (theDeck.RemainingCards() <= cutCardPosition) NewShoe(); bool somebodyPlaying = false; std::list<BlackJackHand*>::iterator currentPlayer; // First we clear all the players currentPlayer = players.begin(); while (!(currentPlayer == players.end())) { (*currentPlayer)->Reset(); // Reset all the players dealer->Reset(); currentPlayer++; } // Now deal 2 cards to each player for (int i = 0; i < 2; i++) { // Deal 2 cards to each player currentPlayer = players.begin(); while (!(currentPlayer == players.end())) { DealCard(*currentPlayer); currentPlayer++; } DealCard(dealer); } // First we check for insurance if there is an Ace if (dealer->IsAceShowing()) { currentPlayer = players.begin(); while (!(currentPlayer == players.end())) { (*currentPlayer)->OfferInsurance(); // We don't care about the result currentPlayer++; } } if (dealer->IsBlackJack()) { } else { // Process moves currentPlayer = players.begin(); while (!(currentPlayer == players.end())) { somebodyPlaying = ProcessTurn(*currentPlayer) || somebodyPlaying; currentPlayer++; } if (somebodyPlaying) ProcessTurn(dealer); } }
18.219.63.95