E N D
BLACKJACK! • The basic premise of the game is that you want to have a hand value that is closer to 21 than that of the dealer, without going over 21. An Ace can count as either 1 or 11, the cards from 2 through 9 are valued as indicated, and the 10, Jack, Queen, and King are all valued at 10. The suits of the cards do not have any meaning in the game. The value of a hand is simply the sum of the point counts of each card in the hand.
BLACKJACK! • The most common decision a player must make during the game is whether to draw another card to the hand (hit), or stop at the current total (stand).
Basic constructor: Initializes names of decks of cards to an array. Keeps track of Aces. • Cards::Cards() • { • player_ace=0, dealer_ace=0; • x=0; • for (int i = 0; i<=53; i++) { deck[i] = 0; }//card names of deck • char tmp_names[53][25] = {"Zero", • "ACE OF SPADES", "TWO OF SPADES", "THREE OF SPADES", "FOUR OF SPADES", • "FIVE OF SPADES", "SIX OF SPADES", "SEVEN OF SPADES", "EIGHT OF SPADES", • "NINE OF SPADES", "TEN OF SPADES", "JACK OF SPADES", "QUEEN OF SPADES", • "KING OF SPADES", • "ACE OF HEARTS", "TWO OF HEARTS", "THREE OF HEARTS", "FOUR OF HEARTS", • "FIVE OF HEARTS", "SIX OF HEARTS", "SEVEN OF HEARTS", "EIGHT OF HEARTS", • "NINE OF HEARTS", "TEN OF HEARTS", "JACK OF HEARTS", "QUEEN OF HEARTS", • "KING OF HEARTS", • "ACE OF CLUBS", "TWO OF CLUBS", "THREE OF CLUBS", "FOUR OF CLUBS", • "FIVE OF CLUBS", "SIX OF CLUBS", "SEVEN OF CLUBS", "EIGHT OF CLUBS", • "NINE OF CLUBS", "TEN OF CLUBS", "JACK OF CLUBS", "QUEEN OF CLUBS", • "KING OF CLUBS", • "ACE OF DIAMONDS", "TWO OF DIAMONDS", "THREE OF DIAMONDS", "FOUR OF DIAMONDS", • "FIVE OF DIAMONDS", "SIX OF DIAMONDS", "SEVEN OF DIAMONDS", "EIGHT OF DIAMONDS", • "NINE OF DIAMONDS", "TEN OF DIAMONDS", "JACK OF DIAMONDS", "QUEEN OF DIAMONDS", • "KING OF DIAMONDS"}; • for (i = 0; i<=53; i++) { strcpy(names[i],tmp_names[i]); } //copy cards into temporary name array • }//end constructor
void Cards::shuffle2() //this shuffle creates a basic array of 52 cards in order { int l; //Spades-array with values of the cards Ace through 10 for(int counter=1; counter<=10; counter++) { deck[counter]=counter; } deck[11]=10; //Values for Jack Queen King deck[12]=10; deck[13]=10; l=1; //Hearts-array with values of the cards Ace through 10 for(counter=14; counter<=23; counter++) { deck[counter]=l; l++; } deck[24]=10; //Jack Queen King deck[25]=10; deck[26]=10; l=1; The first shuffle simply puts each card in a slot in order in an array according to suite. I is used to keep the card values 1 through 10.
void Cards::shuffle() //this function shuffles all of the cards in a random order { int j,k,temp1,temp2; char tmp1[25],tmp2[25]; srand(( unsigned ) time( NULL )); //rand() seed j = 52; for (j=52; j>=1; j--) //the card names and values are randomized { k = rand() % j +1; //set k to a random # between 1 and the current value of j temp1 = deck[j]; temp2 = deck[k]; deck[j] = temp2; deck[k] = temp1; strcpy(tmp1,names[j]); strcpy(tmp2,names[k]); strcpy(names[k],tmp1); strcpy(names[j],tmp2); } //Cards::print(); } The second shuffle actually randomizes the array by placing the card values in temporary holders and swapping them. The actual names of the cards, which we initialized in the constructor, follow the number array of cards by using the same method to randomize the number array.
void Cards::init() //Deals two cards to the player and dealer. { static int test=0;//natural blackjack int player_card1, player_card2; //player cards int dealer_card1, dealer_card2; //dealer cards int player_total, dealer_total; //total of dealer and players hand int *player_test = &test; system("cls"); //check to see if card zero is the first card //put first cards from array into players hand (player_card) if (deck[x] == 0) x++; strcpy(names2[1],names[x]); player_card1=deck[x]; x++; if (deck[x] == 0) x++; strcpy(names2[2],names[x]); player_card2=deck[x]; player_total=player_card1 + player_card2;......ETC.... Dealing the cards...
You must check to see if the player or dealer's hand is an Ace. An Ace can be either a 1 or an 11 in BlackJack. if(player_card1==1) //If first card is an ace, set to 11 { player_ace=1; //Add 1 to p_ace. Lets you know later on if any of the player_card1=player_card1+10; //original two cards where an ace player_total=player_card1 + player_card2; } if(player_card2==1) //If second card is an ace, determine whether or not to make it 1 or 11 { if(player_card1 != 11) //set second card equal to 11 if the first card is not 11 { player_ace=1; player_card2=player_card2+10; player_total=player_card1 + player_card2; } else //other card was an 11, so set to 1 { player_card2=1; player_total=player_card1 + player_card2; } } Check for Aces...
The rest of the program simply consists of a bunch of if else statements that test for game winning conditions. The user also has the option of 'hitting' or 'standing'. If the user decides to 'hit', the dealing process repeats itself. Winner or Loser?
if((*player_total==21) && (*dealer_total!=21)) //If player has a natural 21 and the dealer { //doesnt, he automatically wins //display(&p_total, &d_total) ; cout<<"BLACKJACK! You have won this round!"<<endl; won=won+1; quit(test, player_won, player_lost, player_tied); } else if((*dealer_total==21) && (*player_total!=21)) //If dealer has a natural 21 and the player { //doesnt, dealer automatically wins //display(&p_total, &d_total) ; cout<<"Dealer has BlackJack! You lose this round!"<<endl; lost=lost+1; quit(test, player_won, player_lost, player_tied); } Natural blackjack: