Using C++ write a program that plays a version of the game 21 or \"Blackjack\".
ID: 3772969 • Letter: U
Question
Using C++ write a program that plays a version of the game 21 or "Blackjack".
The rules are a little simpler than traditional Blackjack for simplicity of the program:
--The game is 52 playing cards. Ace has a value of 11 only, CANNOT be 1.
-- Face cards (J/Q/K) have a value of 10, all other cards have the value indicated on them.
--objective for the player is to collect cards that total or nearly total a value of 21 without going over. The dealer (computer) will also be collecting cards.
-- The game starts by shuffling the deck(randomizing)
-- then the player and dealer are dealt two cards from the top of the deck. dealers cards are hidden. players cards are shown.
--after being dealt the inital two cards the player may request another random card from the deck('hit'). the value is then added to the total card value of the players hand. The dealer is then given the next card on the deck and the card value is added to his hand. This step is repeated until one of the following occurs:
------ the player no longer wants to take another card from the deck. The dealer or player with the higher hand wins
-------- the dealer OR players hand value is greater than 21. The one with the hand greater than 21 loses.
-------- The dealer or players hand EQUALS 21. the game stops and the player or dealer with the hand equal to 21 wins.
Explanation / Answer
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
void play21(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int,int);
int Random(int, int);
void main()
{
char keepPlaying = 'n';
do
{
play21();
cout << "Do you want to playanother hand (y/n)?";
cin >> keepPlaying;
}
while(keepPlaying == 'Y' || keepPlaying == 'y');
}
void play21(void)
{
srand((int) time(0));
int person = dealCards(2, "Your Cards:");
cout << " = " << person << endl;
int house = dealCards(2, "Computers Cards:");
cout << " = " << house << endl;
hit(person);
cout << endl;
while ((house < person) && (house <= 21) && (person <= 21))
{
house += dealCards(1, "The Computer takes a card ");
cout << endl;
}
determineWinner(person, house);
}
void determineWinner(int humanScore, int houseScore)
{
if (humanScore == 21)
cout << "You have 21. You win!" << endl;
else if ((humanScore < 21) && (humanScore > houseScore))
cout << "You have the closer hand to 21. You win!" << endl;
else
cout << "The computer wins, sorry try again." << endl;
}
int dealCards(int numberOfCards, string message)
{
int return_value = 0;
int value = 0;
for (int a = 0; a <= numberOfCards; a++)
{
int cards = a;
while(cards--)
{
value = Random(0,10);
cout << value << " ";
if(cards)
cout << " , ";
return_value += value;
}
}
return return_value;
}
void hit(int &playerScore)
{
int cardCount = 0;
char wantCard = "y" || "n";
int cardTotal = 0;
cardTotal = playerScore;
cout << "Would you like another card?";
while (wantCard == 'Y' || wantCard == 'y')
{
if ((cardTotal > 0 ) && (cardTotal < 21))
cardCount += 1;
cardTotal += Random(0,10);
cout << "Your total is: " << cardTotal;
cout << "Do you want another card?";
cin >> wantCard;
if (wantCard == 'Y' || wantCard == 'y')
cout << cardTotal + dealCards(1, "You take a card.");
else
cout << "You decide to stand";
if (cardTotal > 21)
cout << "You have gone over 21, You Lose";
}
}
int Random(int lowerLimit, int upperLimit)
{
return 1 + rand() % (upperLimit - lowerLimit + 1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.