write a program in C++ that simulates a simple blackjack card game. in BJ, the o
ID: 3800278 • Letter: W
Question
write a program in C++ that simulates a simple blackjack card game. in BJ, the object is to get card totaling 21, or to get closer to 21 than dealer without going over 21,
1) the player receives two cards from the dealer. 2) then the dealer gets two cards, one face up, and one face down. 3) you can keep asking for additional cards from the dealer ( a 'hit" ) until you decide to quit taking cards ( to "stand" ) once the player either goes over to total of 21 (termed a "bust" ) or decides to stand. 4) the dealer draws cards according to dealer rules ( see notes, below, for these rules.) whoever is closer to 21 (without busting) is the winner. NOTES : -(1)card values : face cards (10points), ACE (only worth 1pt, not 11 points...to comlicated with both values), numbered cards (each its worth its number) -(2)there are 52 cards in deck cards -(3) dealer rules: the dealer must "hit" on a total of 16(or under), and "stand" on a total of 17(or greater) - (4)if the player "busts", the dealer should not bother drawing (the game's over) -(5)put your game in loop, so that you can continue to play additional games without rerunning the program everytime. - (6) make the game USER-FRIENDLY (i.e. print out the cards drawn and the totals in a clear, readable manner, as the game progresses.)
Explanation / Answer
Solution ::
// header files
#include <iostream.h>
#include <ctime.h>
#include <string.h>
// prototype declaration
void play21(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);
// main function //
void main(){
char keep_playing = 'n'; // loop control variable
do {
play_21();
// keep playing the game ?
cout << "Do you want to play the another hand (Y/y/n)?";
cin >> keep_playing;
} while(keep_playing == 'y' || keep_playing == 'Y');
}
void play_21(void){
// play one hand of the 21
// randomize cards
srand((int) time(0));
// deal cards
int Person = deal_cards(2, "Your Cards:");
cout << " = " << Person << endl;
int House = deal_cards(2, "Computers Cards:");
cout << " = " << House << endl;
// Ask if man wants a Hit and keep Hitting
Hit(Person);
cout << endl;
// Determine if the computer takes a Hit
while ((House < Person) && (House <= 21) && (Person <= 21))
{
House += deal_cards(1, "The Computer takes a card ");
cout << ;
}
determine_winner(Person, House);
}
void determine_winner(int Human_score, int House_score)
// Compare the scores to see who is the winner
{
if (Human_score == 21)
cout << "You have 21. You win!" << endl;
else if ((Human_score < 21) && (Human_score > House_score))
cout << "You have the closer hand to 21. You win!" << endl;
else
cout << "The computer wins, sorry try again." << endl;
}
int deal_Cards(int NumberOfCards, string message)
// Function deals with the Cards
{
int return_value = 0;
int value = 0;
for (int a = 0; a <= NumberOfCards; a++)
{
int Cards = a;
while(Cards--)
{
value = Random(0,10);
cout << the value is << " ";
if(Cards)
cout << " ";
return_value += value;
}
}
return return_value;
}
void Hit(int &playerScore) // Function asks human if they want another card -'a Hit'
{
int card_count = 0;
char want_card = "y" || "n";
int card_total = 0;
card_total = playerScore;
cout << "Would you like other card?";
while (want_card == 'Y' || want_card == 'y')
{
if ((card_total > 0 ) && (card_total < 21))
card_count += 1;
card_total += Random(0,10);
cout << "huanbeing total is: " << card_total;
cout << "Do you want other card?";
cin >> want_card;
if (want_card == 'y' || want_card == 'Y')
cout << card_total + deal_Cards(1, "humanbeing take a card."); // adds the Human_score to the dealCard()
else
cout << "Humanbeing decide to stand";
if (card_total > 21)
cout << "You have greater than 21,So you Lose";
}
}
int Random(int Lower_limit, int Upper_limt)
{
//returns random number within given boundary
return 1 + rand() % (Upper_limt - Lower_limit + 1);
}
/// *** Thank You *** ///
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.