Question

How do I start this c++ code homework? Write a code in C++ for a BlackJack...

How do I start this c++ code homework?

Write a code in C++ for a BlackJack card game using the following simplified rules:

  • Each card has a numerical value.

    • Numbered cards are counted at their face value (two counts as 2 points, three, 3 points, and so on)

    • An Ace count as either 1 point or 11 points (whichever suits the player best)

    • Jack, queen and king count 10 points each

  • The player will compete against the computer which represents the casino.

  • The goal of the player is to try to reach a total point sum of 21 without exceeding it. Whomever exceeds 21 first loses (technically known as busting).

  • At the beginning of each round, the player is dealt two open cards and the computer is dealt one open card. The cards are open, meaning that the values are known for both, the player and the computer (no hidden cards).

  • The player will see the sum of the points from his 2 open cards and decides whether or not to draw an additional card.

  • The player may draw one additional card at a time for as long as he likes or until he busts (sum of drawn cards exceeds 21). If he busts, he loses the round.

  • When the player decides that he won’t draw anymore and he is happy with whatever total amount he got, the computer will draw and open an additional card.

  • The computer will keep on drawing additional cards, one at a time as long as the sum of its cards is less or equal 16.

  • If the computer busts, the player wins.

  • If the computer doesn’t bust (total sum is less or equal 21), then the total values are compared between the computer and the player. Whomever has a higher value wins the round.

  • If the two totals are the same, no one wins the round (technically called a push).

    Here is a list of all the needed c​lasses ​to code the game:

  • Card:​ represents a card. Each card has a ​rank ​and a ​type ​which can be easily representedviae​ nums​.

    ○ Rank is one of the following {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}

    ○ Type is one of the following {CLUBS, DIAMONDS, HEARTS, SPADES} ○ Card class must have the following methods:

    • getValue ​that would return the numerical value of a card

    • displayCard ​that would print to the screen the card information. For example ​1H ​means ace of hearts​, 2D ​means two of diamonds,​ QS means queen of spades and so on.

  • Hand:​ represents the set of cards that the player or the computer holds. This class should contain a list of cards that can be implemented as an array or a vector or any data structure that you prefer. It should also have the following methods:

    • add​ that would add a card to the hand

    • clear​ that would clear all the cards from a hand (removing them)

    • getTotal​ that would get the total sum of the cards numerical values

  • Deck:​ represents the deck of cards and the actions that can be performed on the cards like shuffling and dealing. This class may inherit from the Hand class for convenience but I keep it up to you to decide whether you would inherit or not. Deck should implement the following methods:

    • Populate w​ ill create a standard deck of 52 cards

    • shuffle ​will shuffle the cards

    • deal w​ ill deal one card to a hand

● AbstractPlayer​: represents a generic abstract player that can be a human or the computer. This is an abstract class meaning that it has a pure virtual method. The methods that this class have are:

    • virtual bool isDrawing() const = 0; ​which is a pure virtual method that indicates whether a player wants to draw another card.

    • isBusted t​ hat returns true if a player has busted (sum of cards exceeds 21). Note that AbstractPlayer may inherit from Hand for convenience but this is left totally to you to decide.

  • HumanPlayer:​ represents the human player. This class inherits AbstractPlayer and should have the following methods:

    • isDrawing ​implements the inherited method that indicates whether a player wants to draw another card

    • announce ​a method that prints information about whether the player wins, loses or had a push situation.

    • ComputerPlayer:​ represents the computer (the casino). This class inherits AbstractPlayer and should have the following method:

○ isDrawing ​implements the inherited method that indicates whether the computer should be drawing another card. Remember that the rules for the computer are different. ​The computer should keep on drawing an additional card as long as the sum of its cards is less or equal 16.

● BlackJackGame​: a class representing the overall game. This class must have the following data members:

  • A Deck data member ​m_deck

  • A ComputerPlayer member ​m_casino

    It should also implement the following method: play ​that plays the game of blackjack

Use the following main function to run the game:

int main() {
cout << "\tWelcome to the game!" << endl << endl; BlackJackGame game;
// The main loop of the game bool playAgain = true;
char answer = 'y';
while (playAgain){
game.play();
// Check whether the player would like to play another round
cout << "Would you like another round? (y/n): ";
cin >> answer;
cout << endl << endl;
playAgain = (answer == 'y' ? true : false);
}
   cout <<"Game over!";
return 0;
}

The output while playing the game should be something similar to this:

Welcome to the Comp322 Blackjack table!
Casino: 8S [8]
Player: 4C 9D [13]
Do you want to draw? (y/n):y
Player: 4C 9D 7C [20]
Do you want to draw? (y/n):n Casino: 8S 2H [10]
Casino: 8S 2H 9S [19] Player wins.

Would you like another round? (y/n):y
Casino: 1S [11]
Player: 1C 2H [13]
Do you want to draw? (y/n):y
Player: 1C 2H 10D [13]
Do you want to draw? (y/n):y Player: 1C 2H 10D KH [23] Player busts.
Casino wins.
Would you like another round? (y/n):y
Casino: QS [10]
Player: 4C 4H [8]
Do you want to draw? (y/n):y
Player: 4C 4H 3S [11]
Do you want to draw? (y/n):y Player: 4C 4H 3S JC [21] Casino: QS 1C [21]
Push: No one wins.

Would you like another round? (y/n):n
Game over!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code for BlackJack card game :

BlackJack.cpp -

#include "stdafx.h"
#include "Blackjack.h"


Blackjack::Blackjack()
{
srand(time(0));
dhandSize = 0;
phandSize = 0;
dhandSum = 0;
phandSum = 0;
playerDone = false;
dealerDone = false;
}

void Blackjack::playGame()
{
cout << "Welcome to Blackjack!\n";

// Start the player and dealer with two cards
addPlayerCard();
addPlayerCard();
addDealerCard();
addDealerCard();
sumHands();
printHand();

if (dhandSum == 21)
{
cout << "Dealer has blackjack. Dealer wins.\n";
return;
}
else if (phandSum == 21)
{
cout << "Player has blackjack. Player wins.\n";
return;
}

while (dealerDone == false || playerDone == false)
{
if (playerDone == false)
{
cout << "Would you like to hit? (1 - Yes, 2 - No)\n";
cin >> phit;

if (phit == 1)
{
addPlayerCard();
printHand();
sumHands();

if (phandSum > 21)
{
cout << "Player's hand exceeded 21. Player loses.\n";
return;
}
}
}

if (playerDone == false)
{
cout << "Would you like to stand? (1 - Yes, 2 - No)\n";
cin >> pstand;
}

if (pstand == 1)
{
playerDone = true;
}

if (dhandSum < 17 && dealerDone != true)
{
addDealerCard();
printHand();
sumHands();

if (dhandSum > 21)
{
cout << "Dealer hand exceeded 21. Dealer loses.\n";
return;
}
}
else if (dhandSum >= 17)
{
dealerDone = true;
}

if (phandSum == 21 && dhandSum == 21)
{
cout << "Push, player and dealer reached 21.\n";
return;
}
else if (phandSum == 21)
{
cout << "Player reached 21. Player wins.\n";
return;
}
else if (dhandSum == 21)
{
cout << "Dealer reached 21. Dealer wins.\n";
return;
}

if ((playerDone == true && dealerDone == true) || (phandSize == 5 && phandSize == 5))
{
if (dhandSum < phandSum)
{
cout << "Sum of your hand exceeds the dealer's sum of " << dhandSum << "! You win!";
return;
}
else if (phandSum == dhandSum)
{
cout << "Dealer sum of " << dhandSum << " is equal to the sum of your hand. Tie game.";
return;
}
else if (dhandSum > phandSum)
{
cout << "Sum of your hand is lower than the dealer's sum of " << dhandSum << ". You lose!";
return;
}
}
}
}

int dhand[5];
int phand[5];
int dhandSize;
int phandSize;
int dhandSum;
int phandSum;
int phit;
int pstand;
bool playerDone;
bool dealerDone;

void Blackjack::addPlayerCard()
{
if (phandSize <= 5)
{
phand[phandSize] = 1 + (rand() % 11);
phandSize++;
}
else
{
cout << "Sorry. You have reached the maximum number of cards (5)." << endl;
playerDone = true;
}
}

void Blackjack::addDealerCard()
{
if (dhandSize <= 5)
{
dhand[dhandSize] = 1 + (rand() % 11);
dhandSize++;
}
else
{
dealerDone = true;
}
}

void Blackjack::printHand()
{
cout << "Your current hand is...\n";

for (int i = 0; i < phandSize; i++)
{
cout << " -" << phand[i] << "- \n\n";
}

cout << "Dealer's current hand is...\n";

for (int j = 0; j < dhandSize; j++)
{
cout << " -" << dhand[j] << "- \n\n";
}
}

void Blackjack::sumHands()
{
dhandSum = 0;
phandSum = 0;
for (int i = 0; i < dhandSize; i++)
{
dhandSum += dhand[i];
}

for (int j = 0; j < phandSize; j++)
{
phandSum += phand[j];
}

cout << "Current player hand sum is: " << phandSum << endl;
}

main,cpp -

#include "stdafx.h"
#include "Blackjack.h"


int main()
{

int exitGame = 1;

do
{
Blackjack casino_royale;
casino_royale.playGame();
cout << "\nWould you like to play again? (1 - Yes, 2 - No)\n";
cin >> exitGame;
}while (exitGame == 1);


cout << "\nThanks for playing!\n";
system("pause");
return 0;
}

Blackjack.h -

#pragma once
#include "stdafx.h"


class Blackjack
{
public:
Blackjack();
void playGame();

private:
int dhand[5];
int phand[5];
int dhandSize;
int phandSize;
int dhandSum;
int phandSum;
int phit;
int pstand;
bool playerDone;
bool dealerDone;
void addPlayerCard();
void addDealerCard();
void printHand();
void sumHands();
};

Add a comment
Know the answer?
Add Answer to:
How do I start this c++ code homework? Write a code in C++ for a BlackJack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a simplified Blackjack game using the Deck and Card classes (download the attached files to...

    Create a simplified Blackjack game using the Deck and Card classes (download the attached files to start).  There is also a "TestCard" class that creates a Deck and runs some simple code. You may choose to use this file to help you get started with your game program. 1) Deal 2 cards to the "player" and 2 cards to the "dealer".  Print both the player's cards, print one of the dealer's cards. Print the total value of the player's hand. 2) Ask...

  • Please write the program in python: 3. Design and implement a simulation of the game of...

    Please write the program in python: 3. Design and implement a simulation of the game of volleyball. Normal volleyball is played like racquetball, in that a team can only score points when it is serving. Games are played to 15, but must be won by at least two points. 7. Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll is 2, 3, or 12, the player loses....

  • Using C++ Create a Blackjack program with the following features: Single player game against the dealer....

    Using C++ Create a Blackjack program with the following features: Single player game against the dealer. Numbered cards count as the number they represent (example: 2 of any suit counts as 2) except the Aces which can count as either 1 or 11, at the discretion of the player holding the card. Face cards count as 10. Player starts with $500. Player places bet before being dealt a card in a new game. New game: Deal 2 cards to each...

  • Need a blackjack code for my assignment its due in 3 hours: it has to include...

    Need a blackjack code for my assignment its due in 3 hours: it has to include classes and object C++. Create a fully functioning Blackjack game in three separate phases. A text based version with no graphics, a text based object oriented version and lastly an object oriented 2D graphical version. Credits (money) is kept track of throughout the game by placing a number next to the player name. Everything shown to the user will be in plain text. No...

  • This is a must for a C++ project. Blackjack Requested files: Blackjack.cpp (Download) Type of work:...

    This is a must for a C++ project. Blackjack Requested files: Blackjack.cpp (Download) Type of work: Individual work You MUST have grade 70 or higher on the quiz before you can access this assignment. In the card game named 'Blackjack' players get two cards to start with, and then they are asked whether or not they want more cards. Players can continue to take as many cards as they like. Their goal is to get as close as possible to...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • MATLAB code help! Function Name: jackSparrow Inputs: 1. (double) A 1x2 vector describing your hand 2....

    MATLAB code help! Function Name: jackSparrow Inputs: 1. (double) A 1x2 vector describing your hand 2. (double) A 1x2 vector describing Jack Sparrow's hand 3. (double) A vector with the next cards to be drawn from the deck Outputs: 1. (char) A description of the game outcome Background: You're out at sea with the infamous Jack Sparrow, and it's a pretty calm day. Without anything better to do, Jack Sparrow challenges you to his favorite card game: BlackJack (Sparrow edition)...

  • Complete a program In C#: some code is included, you edit the areas that have not...

    Complete a program In C#: some code is included, you edit the areas that have not been filled. For your C# program you will complete code that plays the War card game. In this game, the deck of cards is evenly divided among two players. The players are not allowed to look at the cards in their hand. On each round of the game, both players lay down the top card from their hand. The player with the higher value...

  • 2 A Game of UNO You are to develop an interactive game of UNO between a...

    2 A Game of UNO You are to develop an interactive game of UNO between a number of players. The gameplay for UNO is described at https://www.unorules.com/. Your program should operate as follows. 2.1 Setup 1. UNO cards are represented as variables of the following type: typedef struct card_s { char suit[7]; int value; char action[15]; struct card_s *pt; } card; You are allowed to add attributes to this definition, but not to remove any. You can represent colors by...

  • Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import...

    Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT