Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a program in which a user plays a simple version of the card game 21 agai

ID: 3839366 • Letter: C

Question

Create a program in which a user plays a simple version of the card game 21 against the computer. The computer will run a network server process allowing client players to connect. Each player is initially dealt two cards from a standard deck of 52. Random numbers will represent the cards from 1 to 10. After seeing their hand the user then the house (computer) are given the opportunity to take additional cards (hit me). The hand that comes the closest to 21 without exceeding 21 wins the game. A draw results if both players have the same score.

Blackjack Rules: http://www.pagat.com/banking/blackjack.html (Links to an external site.)Links to an external site.

shuffle the deck

a way to deal cards to the players

Show the current score based on hands won / hands lost

The game is "Black Jack" or sometimes referred to as "21"

The rules to the game can be found here:   https://www.pagat.com/banking/blackjack.html

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 play another 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);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote