#include #include //just include iostream.h for VS 6 and earlier using namespace std; //necessary for visual studio .net char cardNames[13][32] = { "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace" }; char cardSuits[4][32] = { "clubs", "spades", "hearts", "diamonds" }; char cardValues[13] = { 2,3,4,5,6,7,8,9,10,10,10,10,11 }; void CardToString(int card) { cout<0) return true; return false; } bool HasAce() { int k; for(k=0;k<4;k++) if(playerCards[k*13+12]>0) return true; return false; } int HandValue() { int j; int value = 0; int numAces = 0; for(j=0;j<52;j++) { if((j+1)%13!=0) //aces have special handling value+=playerCards[j]*cardValues[j%13]; else numAces+=playerCards[j]; } if(hasDoubled > 0) //if the player has doubled, and had at least one ace { value += (hasDoubled); //all aces for double have value of 1 (11.3) numAces -= hasDoubled; //remove them from the count } if(numAces >= 1) //if we have at least one ace if(value + 11 + numAces - 1 <=21) //and can make it 11 without busting { value+=11; //use the ace as an 11 numAces--; } value+=numAces; //use the remaining aces as 1 each return value; } //virtual functions can be overwritten by derived classes //we'll set the default behavior for hitting to the dealer rules virtual bool Hit() { if(HandValue()<17) //dealer stays on 17 and over, otherwise hits return true; return false; } //dealer never doubles virtual bool Double() { return false; } }; class cDealer: public cPlayer { public: cDealer() //initialize the class with the base constructor { cPlayer(); } }; class cHuman: public cPlayer { public: cHuman() //initialize the class with the base constructor { cPlayer(); } bool Double() { char input = ' '; cout<<"Would you like to double? y or n"<>input; if(input=='n' || input == 'N') return false; int k; hasDoubled = 0; //flag that we've doubled (also stores number of aces) for(k=0;k<4;k++) hasDoubled += playerCards[k*13+12]; //add up the number of aces the player has return true; } bool Hit() { int curHand = HandValue(); if(curHand<12) { if(curHand>=9) { if(Double()) { cout<<"Player has DOUBLED!"<>input; if(input=='n' || input == 'N') return false; return true; } }; void main() { bool valid = false; //this indicates a valid selection by the user int numDecks = 0; //this keeps track of the number of decks we're playing with int deckOfCards[52]; //this is the deck we will be drawing cards from int numPlayers = 2; cPlayer ** players = new cPlayer*[numPlayers]; //our players (player 0 is always the dealer) int bet; //temp bet variable int card; //temp card variable int j=0,k=0; //our generic loop counter variables players[0] = new cDealer(); for(j=1;j>numDecks; if( numDecks<9 && ((numDecks%3)==0 || (numDecks%4)==0)) valid=true; else cout<<"That is an invalid selection."<ClearHand(); players[1]->ClearHand(); //take bets for(j=1;j>bet; } players[j]->bet = bet; cout<<"Player "<0) { deckOfCards[card]--; valid=true; } } if(j+k==0) { cout<<"Dealer is showing a "; CardToString(card); cout<TakeCard(card); } //actual game play for(j=1;jHandValue()==21) { cout<<"BLACKJACK for Player "<Has10() || players[0]->HasAce()) { if(!players[0]->HasAce()) {//dealer has a 10, pay out 1:1 players[j]->money+=players[j]->bet; cout<<"Dealer has a 10 but no ace, player wins $"<bet<money+=(players[j]->bet*3.0/2.0+0.5); cout<<"Dealer has no ace or 10, player wins $"<<(players[j]->bet*3.0/2.0+0.5)<ShowHand(); cout<<"Player "<HandValue()<hasDoubled < 0 && players[j]->Hit()) { valid=false; while(!valid) { card = rand()%52; if(deckOfCards[card]>0) { deckOfCards[card]--; players[j]->TakeCard(card); valid=true; } } cout<ShowHand(); cout<<"Player "<HandValue()<HandValue()==21) { // if the dealer has blackjack then dealer beat all // rule 11.5 states they only recieve original bet from players that doubled for(j=1;jhasDoubled > -1) //if a player has doubled players[j]->bet /= 2; //restore original bet amount cout<<"Dealer has BLACKJACK!"<Hit()) { valid=false; while(!valid) { card = rand()%52; if(deckOfCards[card]>0) { deckOfCards[card]--; players[0]->TakeCard(card); valid=true; } } } //final tally to see who wins and loses j=0; //store this to simplify int playerValue, dealerValue = players[j]->HandValue(); cout<<"Dealer has the following hand:"<ShowHand(); cout<<"Dealer has "<HandValue(); cout<<"Player "<ShowHand(); cout< 21 || playerValue > dealerValue) { players[j]->money+=players[j]->bet; cout<<"Player WINS!"<money-=players[j]->bet; cout<<"Player LOSES!"<money-=players[j]->bet; cout<<"Player LOSES!"<money<>input; if(input=='n' || input == 'N') done = true; } //end of the game for(j=0;j