Question

In: Computer Science

(General math) In the game of blackjack, the cards 2 through 10 are counted at their...

(General math) In the game of blackjack, the cards 2 through 10 are counted at their face values, regardless of suit; all face cards (jack, queen, and king) are counted as 10; and an ace is counted as a 1 or an 11, depending on the total count of all cards in a player’s hand. The ace is counted as 11 only if the resulting total value of all cards in a player’s hand doesn’t exceed 21; otherwise, it’s counted as 1. Using this information, write a C++ program that accepts three card values as inputs (a 1 corresponding to an ace, a 2 corresponding to a two, and so on), calculates the total value of the hand, and displays the value of the three cards.

Solutions

Expert Solution

#include <iostream>
using namespace std;
#include<stdlib.h>
int value(char card)
{
        int val;
        if (card=='A')
                val=1;
        else if (card=='J' or card=='K' or card=='Q')
                val=10;
        else 
            {
                val=card-48;
                if(val>10 or val<0)
                        {
                        return 0;       
                        }
            }
        return val;
}
int main()
{
char card; int total;
int card1,card2,card3;
cout<<"Type the card- A for Ace;J for Jack; K for King; Q for Queen";
cout<<"\nCard 1:";
cin>>card; // Get the input as character
card1=value(card);
cout<<"\nCard 2:";
cin>>card;
card2=value(card);
cout<<"\nCard 3:";
cin>>card;
card3=value(card);
total=card1+card2+card3; // total value of cards
if (card1==1 && total<=21)
        card1=11;
else if (card2==1 && total<=21)
        card2=11;
else if (card3==1 && total<=21)
        card3=11;
total=card1+card2+card3; 
cout<<"\nTotal Value in Hand"<<total;
cout<<"\nCard 1 Value"<<card1;
cout<<"\nCard 2 Value"<<card2;
cout<<"\nCard 3 Value"<<card3;
return 0;
}

SCREENSHOT OF PROGRAM

EXPLANATION

Read card from user as character and calculate the value of card. If the card is a number from 2 to 10, the character input obtained can be converted into an integer for further value processing. To accomplish this, the ASCII value is used- the ASCII value of 0 is 48. So, subtract 48 from the card's value. If the card is K,J and Q, the value is 10. Else if the input is A, then find the total value in hand. When it does not exceed 21, consider the value of card as 11.

OUTPUT - SCREENSHOT


Related Solutions

In the game of blackjack, the cards 2 through 10 are counted at their face values,...
In the game of blackjack, the cards 2 through 10 are counted at their face values, regardless of suit; all face cards (jack, queen, and king) are counted as 10; and an ace is counted as a 1 or 11, depending on the total count of all cards in a player’s hand. The ace is counted as 11 only if the resulting total value of all cards in a player’s hand doesn’t exceed 21; otherwise, it’s counted as 1. Using...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player,...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player, one hidden, so that only the player who gets it knows what it is, and one face up, so that everyone can see it. There are four players: one human player (user) and three computer players. The players take turns requesting cards, trying to get as close to 21 as possible, but not going over 21. A player may pass. Once a player has...
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...
javascript BlackJack i made a blackjack game, the code is below //this method will return a...
javascript BlackJack i made a blackjack game, the code is below //this method will return a shuffled deck function shuffleDeck() { //this will store array fo 52 objects const decks = [] const suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades'] const values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One'] //run a loop till 52 times for (let i = 0; i < 52; i++) { //push a random item decks.push({ suit: suits[Math.floor(Math.random() *...
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...
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...
Blackjack, or 21, is a popular casino game that begins with each player and the dealer...
Blackjack, or 21, is a popular casino game that begins with each player and the dealer being dealt two cards. The value of each hand is determined by the point total of the cards in the hand. Face cards and 10s count 10 points, aces can be counted as either 1 or 11 points, and all other cards count at their face value. For instance, the value of a hand consisting of a jack and an 8 is 18; the...
Ace Products sells marked playing cards to blackjack dealers. It has not paid a dividend in...
Ace Products sells marked playing cards to blackjack dealers. It has not paid a dividend in many years, but is currently contemplating some kind of dividend. The capital accounts for the firm are as follows: Common stock (2,500,000 shares at $5 par) $ 12,500,000 Capital in excess of par* 5,000,000 Retained earnings 22,500,000 Net worth $ 40,000,000 *The increase in capital in excess of par as a result of a stock dividend is equal to the new shares created times...
Ace Products sells marked playing cards to blackjack dealers. It has not paid a dividend in...
Ace Products sells marked playing cards to blackjack dealers. It has not paid a dividend in many years, but is currently contemplating some kind of dividend. The capital accounts for the firm are as follows: Common stock (2,400,000 shares at $5 par) $ 12,000,000 Capital in excess of par* 5,000,000 Retained earnings 24,000,000 Net worth $ 41,000,000 *The increase in capital in excess of par as a result of a stock dividend is equal to the new shares created times...
Vegas Products sells marked playing cards to blackjack dealers. It has not paid a dividend in...
Vegas Products sells marked playing cards to blackjack dealers. It has not paid a dividend in many years but is currently contemplating some kind of dividend. The capital accounts for the firm are: Common stock (2,500,000 shares) $12,500,000 Retained earnings 22,500,000 Net worth $35,000,000 The company’s stock is selling for $20 per share, and it earned $5,000,000 during the year with 2,500,000 shares outstanding, indicating a P/E ratio of 10. a. Restate the capital accounts at year end after the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT