4. Write a program that scores a blackjack hand. In blackjack, a player receives
ID: 3725838 • Letter: 4
Question
4. Write a program that scores a blackjack hand. In blackjack, a player receives from two to five cards. The cards 2 through 10 are scored as 2 through 10 points each. The face cards -jack, queen, and king-are scored as 10 points. The goal is to come as close to a score of 21 as possible without going over 21. Hence, any score over 21 is called 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 asked how many cards she or he has, and the user responds with one of the integers 2, 3, 4, or 5. The user is then asked for the card way to handle input is to use the type char so that the card input 2, for values 2 through 9 as the characters '2" through '9'. Input the values 10, values. Card values are 2 through 10, jack, queen, king, and ace. A good example, is read as the character '2', rather than as the number 2. Input the jack, queen, king, and ace as the characters 't',T, ,'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. 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 You are likely to have one or more long multiway branches that use a switch statement or nested if-else statement. Your program should include a loop that lets the user repeat this calculation until the user says she or he is doneExplanation / Answer
Programming code :
#include <iostream>
using namespace std;
void instructions ( );
void input (int& number_of_cards, char& card_value1, char& card_value2, char& card_value3, char& card_value4, char& card_value5);
int calculate_hand (int& number_of_cards, char& card_value1, char& card_value2, char& card_value3, char& card_value4, char& card_value5);
void output_result ( );
int main ( )
{
char yes;
int number_of_cards;
char card_value1;
char card_value2;
char card_value3;
char card_value4;
char card_value5;
instructions ( );
do{
input (number_of_cards, card_value1, card_value2, card_value3, card_value4, card_value5);
cout << "Again? (y/n)" << endl;
cin >> yes;
cout << endl;
}while (yes == 'Y' || yes == 'y');
system ("Pause");
return 0;
}
void instructions ( )
{
cout << "This program scores a blackjack hand. The user is asked how many cards" << endl;
cout << "he or she has, and the card values. This program will then output the user's" << endl;
cout << "scores. The output will be either: a number between 2 and 21, or the word BUSTED" << endl << endl;
cout << "The values of the cards should be inputted as the following: input the numbers" << endl;
cout << "2-9 just as inputting regular numbers, for the number 10 input 't', for jack" << endl;
cout << "input 'j' for queen input 'q', for king input 'k', and for ace input 'a'" << endl << endl;
}
void input (int& number_of_cards, char& card_value1, char& card_value2, char& card_value3, char& card_value4, char& card_value5)
{
cout << "Please input the number of cards in your hand (either 2, 3, 4, or 5)" << endl;
cin >> number_of_cards;
cout << endl;
if (number_of_cards == 2)
{
cout << "Please input the value of the first card: " << endl;
cin >> card_value1;
cout << "Please input the value of the second card: " << endl;
cin >> card_value2;
cout << endl << endl;
}
else if (number_of_cards == 3)
{
cout << "Please input the value of the first card: " << endl;
cin >> card_value1;
cout << "Please input the value of the second card: " << endl;
cin >> card_value2;
cout << "Please input the value of the third card: " << endl;
cin >> card_value3;
cout << endl << endl;
}
else if (number_of_cards == 4)
{
cout << "Please input the value of the first card: " << endl;
cin >> card_value1;
cout << "Please input the value of the second card: " << endl;
cin >> card_value2;
cout << "Please input the value of the third card: " << endl;
cin >> card_value3;
cout << "Please input the value of the fourth card: " << endl;
cin >> card_value4;
cout << endl << endl;
}
else if (number_of_cards == 5)
{
cout << "Please input the value of the first card: " << endl;
cin >> card_value1;
cout << "Please input the value of the second card: " << endl;
cin >> card_value2;
cout << "Please input the value of the third card: " << endl;
cin >> card_value3;
cout << "Please input the value of the fourth card: " << endl;
cin >> card_value4;
cout << "Please input the value of the fifth card: " << endl;
cin >> card_value5;
cout << endl << endl;
}
}
int calculate_hand (int& number_of_cards, char& card_value1, char& card_value2, char& card_value3, char& card_value4, char& card_value5)
{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.