Question

In: Computer Science

To do this in C++ preferably not in a class but if not possible do in...

To do this in C++ preferably not in a class but if not possible do in a class.

Assignment Specifications

Your assignment is to write a single-player version of the casino card game Blackjack, also known as 21. The goal of the game Blackjack is to get cards whose total value comes closest to 21 without going over. Getting a card total over 21 is called "busting". The player will play against the dealer. There are 3 possible outcomes.

  • Player comes closer to 21 than the dealer or the dealer busts but the player did not bust -> player wins the bet amount.

  • The dealer comes closer to 21 than the player or the player's total exceeds 21 -> player loses. Note if both the player and the dealer bust, then the dealer wins. This is called the "house advantage".

  • Both player and dealer have the same total and neither player busts -> tie, no money is exchanged.

At each round of play, the player will be asked to enter their bet. They will then be given 2 cards. The player will repeatedly be asked if they want to draw another card. The player can continue to draw cards while their total is less than 21. After the player's turn is over, the dealer's cards are shown. The dealer's play is always the same: the dealer will continue to draw cards if their total is less than or equal to 16.

A sample run is shown below. Assume the player starts with $100 and the game ends when the player is down to $0 or their amount exceeds $1,000. Also note the user is prompted to re-enter their bet amount if they try to bet more than they actually have.

Programming instructions

Your program should have a function called draw_card that draws a random card. The function should get a random number between 1 and 13 to determine the rank of the card and another number between 1 and 4 to determine the suite of the card. The card of rank 13 corresponds to a king, the card of rank 12 corresponds to a queen, the card of rank 11 corresponds to a jack and card of rank 1 is an ace. In blackjack face cards (Jack, Queen, King) all count as 10 points towards the card total. The function should return the value of the card to be added to the point total. Aces can be either 11 (high) or 1 (low), depending on which is more advantageous to the player to come closest to 21 without going over. The card variable will contain the description of the card such as "Two of diamonds" or "King of hearts".

You should also have a getSuit and getRank function that takes as input an int that corresponds to a Rank or Suit and returns the string version of that rank or suit. For example, rank 10 would have to return “Jack”. Suit 3 would return “hearts”.

Note: Your rand values should have spades = 0, clubs = 1, diamonds = 2, and hearts = 3.

You should seed srand(333)

Note: In your main you should have a variable that keeps track of the current total for the player and another variable that keeps track of the current total for the dealer. When the draw card function is called you should pass to it a string variable called card and the current total. Your function draw_card will return the value of the card it draws and this value should be added in main to the current total. Note that the total is only used in this function to decide if ace should count as high or low. This function does not return the total! It returns the value of the card that was drawn. You are responsible for adding this value to the total in main. When you call the function draw_card you will also pass it a string called card. After calling the function draw_card this variable card will now have the description of the card, something like "Three of diamonds". Do not output anything in the function draw card! All output should be done in main.

//-------------------------------------------------------------------

// draw_card()

// Uses rand() to draw a card then returns the numerical

// value and card name

//-------------------------------------------------------------------

int draw_card(string &card, //IN - card name

               int drawer_points); //IN - drawer's total points

   

   

/****************************************************************

*

* draw_card()

*_______________________________________________________________

* Simulates the drawing of a card. Passes by reference the

* kind of card (value and suite in string value) as well as

* returns the card's equivalent numerical value (to be added

* to totals in main())

* Also reads current drawer's points to determind value of

* aces

*_______________________________________________________________

* PRE-CONDITIONS:

* &card : passes card info

* drawer_points : reads current drawer's points to determine

* value of ace

*

* POST-CONDITIONS:

* passes card info by reference, returns card numerical value

****************************************************************/

int draw_card (string &card, //card info

                int drawer_points) //current drawer's points

Note: You do not need to account for cases where you draw a 3 and then an Ace giving you a total of 14 (Ace counts here as 11) and then you draw a Queen giving you a total of 24. In real Blackjack you would now revert the Ace back to 1 giving you a total of 14. However, this is too complicated and we will not deal with this issue. Once Ace has been counted as 11 it will stay as 11.

Things to test for:

  • Player busts

  • Dealer busts

  • Player loses all his money (Game Over)

  • Player wins (total is more than $1000)

  • Program halts when appropriate

FAQ

Q: Can the same card come up multiple times?

A: Yes. We will assume that the dealer uses multiple decks so for instance a four of clubs can come up many times in the same hand.?

Q: What happens if the player gets 21 does he automatically win?

A: In real blackjack rules the dealer gets to draw even if the player has gotten a 21. If the dealer also gets a 21 then it is considered a draw.





Example Run

You have $100. Enter bet:

50

Your cards are:

  Ten of Clubs

  Ace of Spades


The dealer's cards are:

  King of Hearts

  Seven of Clubs

The dealer's total is 17.

You win $50.

Play again? (y/n):

y

You have $150. Enter bet:

50

Your cards are:

  Seven of Clubs

  Jack of Hearts

Your total is 17. Do you want another card (y/n)?

n


The dealer's cards are:

  Queen of Spades

  Two of Diamonds

The dealer's total is 12.

The dealer draws a card.

  Five of Diamonds

The dealer's total is 17.

A draw! You get back your $50.

Play again? (y/n):

y

You have $150. Enter bet:

50

Your cards are:

  Nine of Clubs

  Queen of Spades

Your total is 19. Do you want another card (y/n)?

y

You draw a:

  Five of Clubs

Your total is 24. You busted!

Play again? (y/n):

y

You have $100. Enter bet:

100

Your cards are:

  Ace of Spades

  Three of Diamonds

Your total is 14. Do you want another card (y/n)?

y

You draw a:

  Eight of Hearts

Your total is 22. You busted!

You have $0. GAME OVER.

Example

The dealer's cards are:

  Two of Clubs

  Four of Diamonds

The dealer's total is 6.

The dealer draws a card.

  Three of Hearts

The dealer draws a card.

  Two of Clubs

The dealer draws a card.

  Nine of Spades

The dealer's total is 20.

Too bad. You lose $40.

Solutions

Expert Solution

Code

#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;
int draw_card(string &card,int drawer_points);
string getRank();
string getSuit();
int main()
{
   int playerTotal,dealerTotal,playerAmount=100,betAmount,cardValue;  
   string card;
   char again;
   bool busted=false;
   srand(333);
   while(true)
   {
       playerTotal=0;
       dealerTotal=0;
       busted=false;
       cout<<"You have $"<<playerAmount;
       cout<<" Enter bet:";
       cin>>betAmount;
       cout<<"Your Cards are: "<<endl;
       cardValue=draw_card(card,playerTotal);
       playerTotal+=cardValue;
       cout<<" "<<card<<endl;
       cardValue=draw_card(card,playerTotal);
       cout<<" "<<card<<endl;
       playerTotal+=cardValue;
       cout<<"Your total is "<<playerTotal;
       if(cardValue!=21)
       {
           while(true)
           {              
               cout<<" Do you want another card (y/n)?\n";
               cin>>again;
               if(again=='y')
               {
                   cardValue=draw_card(card,playerTotal);
                   playerTotal+=cardValue;
                   cout<<"You draw a:"<<endl;
                   cout<<" "<<card<<endl;
                   cout<<"Your total is "<<playerTotal;
                   if(playerTotal>21)
                   {
                       cout<<". You busted!"<<endl;
                       busted=true;
                       playerAmount-=betAmount;
                       break;
                   }
               }
               else
                   break;
           }
       }
       if(!busted)
       {
           cout<<"The dealer's cards are:"<<endl;
           cardValue=draw_card(card,dealerTotal);
           dealerTotal+=cardValue;
           cout<<" "<<card<<endl;
           cardValue=draw_card(card,dealerTotal);
           cout<<" "<<card<<endl;
           dealerTotal+=cardValue;
           while(dealerTotal<=16)
           {
               cout<<"The dealer draws a card."<<endl;
               cardValue=draw_card(card,dealerTotal);
               cout<<" "<<card<<endl;
               dealerTotal+=cardValue;
           }
           cout<<"The dealer's total is "<<dealerTotal;
           if(dealerTotal>21)
           {
               cout<<". Dealer buseted."<<endl;
               cout<<"You win $"<<betAmount<<endl;
               playerAmount+=betAmount;
           }
           else if(dealerTotal>playerTotal)
           {
               cout<<". You loos!"<<endl;
               playerAmount-=betAmount;
           }
           else if(dealerTotal==playerAmount)
           {
               cout<<"A draw! You get back your $"<<betAmount<<"."<<endl;
           }
           else
           {
               cout<<". You win $"<<betAmount<<endl;
               playerAmount+=betAmount;
           }
       }
       if(playerAmount==0)
       {
           cout<<"You have $"<<playerAmount<<" GAME OVER"<<endl;
           break;
       }  
       cout<<"\nPlay again? (y/n):";
       cin>>again;      
       if(again=='n')
           break;
   }
   if(playerAmount>0)
       cout<<"\nYou have $"<<playerAmount<<endl;
   return 1;
}
int draw_card(string &card,int drawer_points)
{
   string rank=getRank();
   string suit=getSuit();
   card=rank+" of "+suit;
   if(rank=="Ace")
   {
       if((drawer_points+11)<21)
           return 11;
       else
           return 1;
   }
   if(rank=="10" || rank=="Jack" || rank=="Queen" || rank=="King")
       return 10;
   return stoi(rank);
}
string getRank()
{
   string rank[]={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
   return rank[rand() % 13];
}

string getSuit()
{
   string suit[]={"spades","clubs","dimonds","hearts"};
   int r=rand() % 4;
   return suit[r];
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
Please write in C++ as simple as possible I want you to create a Book Class...
Please write in C++ as simple as possible I want you to create a Book Class for a bookstore. I am not going to tell you what variables should go into it, that is for you to figure out (but you should have at least 5+). And then you must create a UML with all the variables and methods (like: the getters and setters, a default constructor, and a constructor that takes all the variables and finally the printValues() )....
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. YES, an integer can be put into and retrieved from a character array. It's all binary under the hood. In some languages this is very easy (C/C++)...
*(Explained in greatest depth/detail if possible)* **(Preferably pertaining to Economist Karl Polanyi's Fictitious Commodities, Embeddedness, and...
*(Explained in greatest depth/detail if possible)* **(Preferably pertaining to Economist Karl Polanyi's Fictitious Commodities, Embeddedness, and Double Movement theorems)** Has the Economic and Political sectors in the state of Utah been assimilated by the sentiments of The Church of Jesus Christ of the Latter-day Saints (Mormons) religious beliefs? If so, I wonder not only how it affects Utah’s Economic related well-being, but how the LDS religion affects the entire culture of the state of Utah?
PLEASE DO IN C++ (Math: The Complex class) The description of this project is given in...
PLEASE DO IN C++ (Math: The Complex class) The description of this project is given in Programming Exercise 14.7 in the Chapter 14 Programming Exercise from the Book section. If you get a logical or runtime error, please refer https://liangcpp.pearsoncmg.com/faq.html. Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide, abs for performing complex-number operations, and the toString function for returning a string representation for a complex number. The toString function returns a+bi as...
Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of...
Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of a mod n. Your program should allow user to enter a and n. Note: For this question please make sure the code compiles and runs, it is not copied and pasted from elsewhere( I will be checking!). Thanks
How do I add an output operator to a class in C++? The specific part of...
How do I add an output operator to a class in C++? The specific part of the task said to: Define a ​class t​o hold accounts. Use the ​same​ stream variable! Write getters to access the fields in the accounts when printing. Add an output operator for your class. First, Repeat the print loop using this output operator.Now​ make the output operator a friend, by adding a friend​prototype​ to your account class definition. Add a line in the output operator...
(g) Do a web search and find another example of Simpson's Paradox, preferably in a business...
(g) Do a web search and find another example of Simpson's Paradox, preferably in a business context. Don't pick the first one you find - dig a little deeper. Give the details of the example and explain how it demonstrates this paradox. Include the url to the website.
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given...
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given below. Use and modify them) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue. NOTE: A humble request, please don't just copy and paste the answer from chegg. I need more specific answer. Also I don't have much question remaining to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT