Question

In: Computer Science

Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both...

Design a c++ program to simulate the BlackJack game. Rules:

1) single deck, 52 cards -both player and dealer are taking cards off the same deck (the same 52 values).

2) display BOTH cards of the players, and ONE card of the computer.

3) Check for blackjack (starting value of 21 for computer or player) -if either side has a blackjack end of the game, next hand of blackjack 4) Computer must hit if 16 or below.

5) Computer must stay if 17 or above.

6) Ace can be 1 or 11 in value -Can not bust if you go over 21 if you are using an Ace as 11 (example: 8+11+6 =/= 25, it would convert to 8+1+6 == 15) .

For example ACE ACE ACE ACE 8.

Can't Equal 11+1+1+1+8 = 22(it's a bust! no good!) ^--that 11 would drop to 1

much better: 1+1+1+1+8 = 12,

for example, A, 2, 3, A. So for the best result, the user will choose 11 for the first A, 1 for the second A, or vise versa.

Requirements: 1. Repeat the game until the player doesn't want to play. Use a new deck for each round.

2. program must be well-documented

3. must-have functions defined (not just the main method) in the program

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks

Code:

#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include<time.h>

using namespace std;

void intro ();
bool playAgain();
bool decide(char ans);
void checkWin(int, int);
string deck[52]= {"AC","2C","3C","4C","5C","6C","7C","8C","9C","0C","JC","QC",
       "KC","AD","2D","3D","4D","5D","6D","7D","8D","9D","0D","JD","QD","KD","AS","2S","3S","4S","5S",
       "6S","7S","8S","9S","0S","JS","QS","KS","AH","2H","3H","4H","5H","6H","7H","8H","9H","0H","JH","QH","KH"};
string drawCard(string deck[], vector<string> hand, vector<string> dealer);
string determineCardType(string card);
string determineSuiteType(string card);
bool aceCheck(vector<string> card);
int determineCardValue(string card, vector<string> hand);

int main(){
   bool play = true;
   int playerTotal;
   int dealerTotal;
   srand(time(0));
   intro();

   while (play == true){
       //Initial deal.
       playerTotal=0;
       dealerTotal=0;
       vector<string> playerHand;
       vector<string> dealerHand;
       string playerCard1 = drawCard(deck, playerHand, dealerHand);
       playerTotal = determineCardValue(playerCard1, playerHand);
       playerHand.push_back(playerCard1);
       cout <<"Player Card 1 :"<< determineCardType(playerCard1) << " of " << determineSuiteType(playerCard1) << endl;
       string playerCard2 = drawCard(deck, playerHand, dealerHand);
       playerTotal += determineCardValue(playerCard2, playerHand);
       playerHand.push_back(playerCard2);
       cout <<"Player Card 2 :"<< determineCardType(playerCard2) << " of " << determineSuiteType(playerCard2) << endl;
       string dealerCard1 = drawCard(deck, playerHand, dealerHand);
       dealerHand.push_back(dealerCard1);
       dealerTotal = determineCardValue(dealerCard1, dealerHand);
       cout <<"Dealer Card 1 :"<< determineCardType(dealerCard1) << " of " << determineSuiteType(dealerCard1) << endl;
       string dealerCard2 = drawCard(deck, playerHand, dealerHand);

       //Decide to hit.
       cout << "Dealer showing: " << dealerTotal << endl;
       char hit;
       do{
           cout << "You have: " << playerTotal << endl;
           cout << "Do you want to hit (y/n)?" << endl;
           cin >> hit;
           if(hit=='y'){
               string playerCard = drawCard(deck, playerHand, dealerHand);
               playerTotal += determineCardValue(playerCard, playerHand);
               playerHand.push_back(playerCard);
               cout << determineCardType(playerCard) << " of " << determineSuiteType(playerCard) << endl;
           }else{
               break;
           }
       } while (playerTotal <= 21);
       if(playerTotal>21){
           cout << "You have: " << playerTotal << endl;
           cout<<"You got busted.\n House wins.\n";
       }
       else{
       //Dealer's play
       cout << "Dealer's 2nd card: " <<
               determineCardType(dealerCard2) << " of " << determineSuiteType(dealerCard2) << endl;
       dealerHand.push_back(dealerCard2);
       dealerTotal += determineCardValue(dealerCard2, dealerHand);
       while(playerTotal < 21 && dealerTotal <= 17){
           string dealerCard = drawCard(deck, playerHand, dealerHand);
           cout <<"Dealer's next card: " <<determineCardType(dealerCard) << " of " << determineSuiteType(dealerCard) << endl;
           dealerHand.push_back(dealerCard);
           dealerTotal += determineCardValue(dealerCard, dealerHand);
       }

       //Check who won -- finish this part
       cout << "You have: " << playerTotal << endl;
       cout << "Dealer has: " << dealerTotal << endl;
       checkWin(playerTotal,dealerTotal);
       }
       play = playAgain();
   }
   cout << "You are no longer playing\n"; //debug
   system("pause");
   return 0;
}

// finish this part ***
void checkWin(int playerTotal, int dealerTotal){
   if(playerTotal == dealerTotal){
       cout << "Push." << endl;
   } else if(playerTotal == 21){
       cout << "You got BlackJack!" << endl;
       cout << "You win!" << endl;
   } else if(dealerTotal == 21){
       cout << "Dealer has BlackJack." << endl;
       cout << "House wins." << endl;
   } else if(playerTotal > dealerTotal){
       cout << "You win!";
   } else if(dealerTotal>21){
       cout << "Dealer got Busted!" << endl;
       cout << "You win!" << endl;
   }
}

bool aceCheck(vector<string> hand){
   int size = hand.size();
   for(int i = 0; i < size; i++){
       string card = hand[i];
       return card[0] == 'A';
   }
   return false;
}

int determineCardValue(string card, vector<string> hand){
   switch(card[0]){
       case '0':
           return 10;
       case 'K':
           return 10;
       case 'Q':
           return 10;
       case 'J':
           return 10;
       case 'A':
           if(aceCheck(hand)){
               return 1;
           } else {
               return 11;
           }
       default:
           return (card[0]-'0');
   }
}

string determineCardType(string card){
   string type;
   switch(card[0]){
       case 'A':
           type = "Ace";
           return type;
       case 'K':
           type = "King";
           return type;
       case 'Q':
           type = "Queen";
           return type;
       case 'J':
           type = "Jack";
           return type;
       case '0':
           type = "10";
           return type;
       default:
           type = card[0];
           return type;
   }
}

string determineSuiteType(string card){
   string suite;
   switch(card[1]){
       case 'H':
           suite = "Hearts";
           return suite;
       case 'D':
           suite = "Diamonds";
           return suite;
       case 'S':
           suite = "Spades";
           return suite;
       case 'C':
           suite = "Clubs";
           return suite;
       default:
           return "Suite undetermined.";
   }
}

void intro(){
   cout << "Welcome to BlackJack!" << endl;
}

bool decide(char ans){
   while (ans != 'y' && ans != 'n'){
       cout << "You must answer with a 'y' or 'n'" << endl;
       cout << "Would you like to play again (y/n)?" << endl;
       cin >> ans;
   }
   if(ans == 'y'){
       return true;
   } else {
       return false;
   }
}

bool playAgain(){
   cout << "Would you like to play again (y/n)?" << endl;
   char ans;
   cin >> ans;
   return decide(ans);
}

bool checkDuplicates(string card, vector<string> hand){
   int size = hand.size();
   for (int i = 0; i < size; i++){
       if(card == hand[i])
           return true;
   }
   return false;
}

string drawCard(string deck[], vector<string> hand, vector<string> dealer){
   int r = rand() % 52;
string card;
if((checkDuplicates(card, hand) || checkDuplicates(card, dealer)) && r != 52){
   card = deck[r + 1];
} else if ((checkDuplicates(card, hand) || checkDuplicates(card, dealer)) && r == 52){
   int j = 0;
   while(checkDuplicates(card, hand) || checkDuplicates(card, dealer)){
       card = deck[j];
       j++;
   }
} else {
   card = deck[r];
}
return card;
}

Output:


Related Solutions

C program simple version of blackjack following this design. 1. The basic rules of game A...
C program simple version of blackjack following this design. 1. The basic rules of game A deck of poker cards are used. For simplicity, we have unlimited number of cards, so we can generate a random card without considering which cards have already dealt. The game here is to play as a player against the computer (the dealer). The aim of the game is to accumulate a higher total of points than the dealer’s, but without going over 21. The...
Play a blackjack. Write a Java program that starts from a deck of 52 cards and...
Play a blackjack. Write a Java program that starts from a deck of 52 cards and one player plays again a dealer. Use a simple rule explained here. Ace can be counted as 1 only for simplicity. Jack, Queen, King are counted as 10. At the beginning, player receives two cards. Dealer receives two cards, but shows one and hides one. Program asks player if player wants to receive another card or not. (player can continue to receive new cards...
Write a program in c++ that picks four cards from a deck of 52 cards and...
Write a program in c++ that picks four cards from a deck of 52 cards and computes the sum of the four cards. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24. You are not allowed to use arrays. Declare four variables to hold the card values.
1. In the game of poker, five cards from a standard deck of 52 cards are...
1. In the game of poker, five cards from a standard deck of 52 cards are dealt to each player. Assume there are four players and the cards are dealt five at a time around the table until all four players have received five cards. a. What is the probability of the first player receiving a royal flush (the ace, king, queen, jack, and 10 of the same suit). b. What is the probability of the second player receiving a...
Program a simple game that shuffles a deck of 52 cards and hands them out to...
Program a simple game that shuffles a deck of 52 cards and hands them out to twoplayers randomly (26 each). Write a simulation of the card game, where, at each turn, bothplayers take the card from the top of their pile and the player with the higher card wins thatround. The winner of that round takes both cards and adds it to a stack of won cards.In essence, there are four stacks. Each player has one stack of cards they...
A standard deck of cards has 52 cards, four each of 2,3,4,5,6,7,8,9,10,J,Q,K,A. In blackjack, a player...
A standard deck of cards has 52 cards, four each of 2,3,4,5,6,7,8,9,10,J,Q,K,A. In blackjack, a player gets two cards and adds their values. Cards count as their usual numbers, except Aces are 11 (or 1), while K, Q, J are all 10. “Blackjack” means getting an Ace and a value ten card. What is probability of getting a blackjack? What is probability of getting 19? (The probability that the sum of your cards is 19, using Ace as 11) Use...
In this game, you will play in pairs with a single deck of cards (face cards...
In this game, you will play in pairs with a single deck of cards (face cards removed). Aces count as ones and all numbered cards count at face value. Players take turns flipping over two cards and finding the product. Regardless of who flipped the cards, Player 1 always gets one point if the product is even, and Player 2 always gets one point if the product is odd. Continue playing until one player reaches 20 points. Would you rather...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
A single card is chosen at random from a deck of 52 cards, the probability that...
A single card is chosen at random from a deck of 52 cards, the probability that a club card is selected is 1/4. Does this probability mean that, if you choose a card at random 8 times, a club will appear twice? If not, what does it mean? Probability?
a single card is drawn from a deck of 52 cards. find the probability that the...
a single card is drawn from a deck of 52 cards. find the probability that the card chosen is as follows. the queen of spades or a black card.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT