Question: Using C++ and not using loops. We can use if else statements but nothing past that. Write a program that scores a blackjack hand. In

Using C++ and not using loops. We can use if else statements but nothing past that.

Write a program that scores a blackjack hand.

In blackjack, a player receives from two to five cards. In our game, players will receive two cards with the chance to "stay" or "hit" once (therefore a total of three cards).

The cards 2 through 9 are scored as 2 through 9 points each. The 10 and face cardsjack, queen, and kingare scored as 10 points. Input the values 10, jack, queen, king, and ace as the characters 't', 'j', 'q', 'k', and 'a'. (Of course, the user does not type in the single quotes.) Be sure to allow upper- as well as lowercase letters as input.

The goal is to come as close to a score of 21 as possible without going over 21. Hence, any score over 21 is considered busted. The ace can count as either 1 or 11, whichever is better for the user.

For example, an ace and a 10 can be scored as either 11 or 21. Since 21 is a better score, this hand is scored as 21. An ace and two 8s can be scored as either 17 or 27. Since 27 is a busted score, this hand is scored as 17.

The user is then asked for the card values for their two cards at hand. Card values are 2 through 10, jack, queen, king, and ace. With two cards, there is no way for the user to "bust", therefore he/she will have the option to either "stay" or "hit" once they know their total with the two cards. If the user decides to hit, they must enter the value of the third card, if it's an ace the program should determine based on the previous two cards whether the ace should be treated as a 1 or an 11.

Once the user has all three cards in hand, or decided to "stay", their hand must be compared to that of the dealer. To obtain the dealers hand you must use the following code:

First: be sure you have declared an integer variable to store the dealers hand value.

Second: in my program I called this variable dealer and therefore the code used for the dealers hand is

srand (time(NULL));

dearler=rand()%22+1;

Also, for rand() to work you must have the following at the top of your program: #include; and #include

A good way to handle input is to use the type char so that the card input 2, for example, is read as the character '2', rather than as the number 2. Input the values 2 through 9 as the characters '2' through '9' (this is only a suggestion, if this makes the project more difficult for you then do not read the numbers as characters). This is also helpful because your face cards are read in as characters. If you do not read your numbers in as characters you will be making the game more difficult to code.

After reading in the values, the program should convert them from character values to numeric card scores, taking special care for aces. The output is either a number between 2 and 21 (inclusive) or the word Busted.

If you choose to use your numbers as characters, when you get ready to add them together you must use the following:

char card1 = '5'; //this saves the character value 5 in the variable card1 (however in your program the user will enter the card value)

((int)card1-'0'); //will change the value back to an integer, however the way it is written here it does not store the value anywhere, so you may want to have a variable where it is stored.

You MUST correctly implement a switch statement for when the user decides whether to "stay" or "hit".

Also, notice that in the game of Blackjack there are specific cards that you play with, if the user enters any value that does not correspond to what is allowed then the program must end.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!