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() *...
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
Twenty One This game is similar to the famous card game blackjack. We will play a...
Twenty One This game is similar to the famous card game blackjack. We will play a one-player version of the game. The game is played for some number N of rounds (we will use N = 10,000), at the end of which the player wins points. The player accumulates points during the whole game, and the objective is, of course, to end up with the maximum number of points. The objective in each round of the game is to score...
Twenty One This game is similar to the famous card game blackjack. We will play a...
Twenty One This game is similar to the famous card game blackjack. We will play a one-player version of the game. The game is played for some number N of rounds (we will use N = 10,000), at the end of which the player wins points. The player accumulates points during the whole game, and the objective is, of course, to end up with the maximum number of points. The objective in each round of the game is to score...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT