Question

In: Computer Science

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 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 and display the total value of the hand, and the sum of the three cards.

Solutions

Expert Solution

>Answer

Code:

#include <assert.h>

#include <limits.h>

#include <math.h>

#include <stdbool.h>

#include <stddef.h>

#include <stdint.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <string.h>

int getCardValue(int points, int card) {

// Ace is 11 points if total sum does not exceeds 21

// else it is 1 point

if(card == 1){

if(points + 11 < 21)

return 11;

return 1;

}

//Cards value equals their number from 2 to 10

if(card >= 2 && card <= 10)

return card;

//J Q K have 10 points

if(card >= 11 && card <= 13)

return 10;

return 0;

}

void printCardSymbol(int card) {

if(card == 1)

printf("A ");

else if(card == 11)

printf("J ");

else if(card == 12)

printf("Q ");

else if(card == 13)

printf("K ");

else

printf("%d ",card);

return;

}

/* Cards:

* 1 - Ace

* [2,10] - face cards from two to ten

* 11 - J

* 12 - Q

* 13 K

*/

void blackjack() {

// Get random number from 1 to 13

int card1 = rand()%13 + 1;

// Get random number from 1 to 13

int card2 = rand() % 13 + 1;

// Get random number from 1 to 13

int card3 = rand() % 13 + 1;

// Sort card so A will be last elelent

// to add to total numbers

// If it is first number we might get error

// E.X. A 10 Q - AT first sum is zero so

// if we check A we get 11 points but total sum

// of two other cards is 20 and A can not be 11, it is 1

// So to avoid such errors we should check it at first.

if (card1 == 1) {

int tmp = card1;

card1 = card3;

card3 = tmp;

}

if(card2 == 2){

int tmp = card2;

card2 = card3;

card3 = tmp;

}

//Total Points, initialy equals to zero

int totalPoints = 0;

//We add each cards value to total points

totalPoints += getCardValue(totalPoints, card1);

totalPoints += getCardValue(totalPoints, card2);

totalPoints += getCardValue(totalPoints, card3);

//Print summery

printf("Player got cards: ");

printCardSymbol(card1);

printCardSymbol(card2);

printCardSymbol(card3);

printf("\n Player total points: %d",totalPoints);

}

int main() { blackjack ();}


Output:
Your Output (stdout) Player got cards: Q 10 A Player total points: 21


Related Solutions

(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...
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...
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...
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() *...
What is the expectation for the number of face cards (aces not included) when 10 cards...
What is the expectation for the number of face cards (aces not included) when 10 cards are drawn, with replacement, from a standard deck
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...
In Texas Hold'em, 2 cards are first dealt face down (hole cards) to each player, and...
In Texas Hold'em, 2 cards are first dealt face down (hole cards) to each player, and then five community cards are dealt face up. Each player seeks the best five card poker hand from any combination of the seven cards of the five community cards and their two hole cards. a) During a self-practice, when only two hole cards are dealt, what is the probability that you get a pair of aces? b) During a self-practice, assume only seven 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT