QuestionQuestion

Transcribed TextTranscribed Text

Program 6 – Polymorphism, Queues, and Stacks Super Blackjack Super Blackjack is a card/dice game that works a lot like Blackjack. The objective is to get as close to 21 without going over. The player and the dealer are each dealt two cards. The player’s cards are dealt face up, and only one of the dealer’s cards are shown. Before any cards are dealt, the player makes a wager from their available bank balance. On the deal, if the player has 21 showing, then the player scores a “Blackjack” and wins. In Super Blackjack, numbered cards are the value on the card, “face” cards are worth 10, and Aces are worth either 1 or 11. If the player does not have 21 on the deal, then the player must “hit” (take an extra card) or “stay” (waits for the dealer to complete their hand). If the player goes over 21, then they lose and the dealer wins. If the player chooses to stay, then they must wait for the dealer to close out their hand (a dealer will automatically “hit” on a card if the dealer’s hand is less than 17 (and they will keep hitting provided their hand remains less than 17); if the dealer’s hand is 17 or greater, then they will stay). If the dealer ends up with more than 21, then the dealer busts and the player wins. If the player and dealer end up tying (including 21), then nobody wins and no money changes hands. Super Blackjack is “super” because the player can choose to roll a die instead of taking a card. The value (1-6) shown on the die is the value added to the player’s hand. Implementation Details You are to implement Super Blackjack using polymorphic principles. Each of the player’s hand (cards and dice) as well as the dealer’s hand (cards only) must be stored in an array or vector. Each class should have its own CPP/H file. When your program starts, you should read in the bank balance from a file. When your program ends, you should update the file with the new bank balance. You must implement a queue that stores 52 shuffled cards. You must draw from the queue in order to deal cards (no duplicate cards). At the end of the session, you should report a “history” of the hands and their outcomes. You should deallocate all heap memory after every hand. You must implement at least one stack. You may use the <stack> and <queue> libraries. Upload your files to GitHub. Sample Output Welcome to Super Blackjack Loading bank balance... ---- HAND 1 ---- You have 118 dollars. How many dollars do you want to bet? 4 Dealing Cards... Dealer Shows: +------------+ | 2| | ** ** | | ****** | | ****** | |****| |**| |2 | +------------+ Player Hand: +------------+ | A | | ** ** ** | | ****** | | **** | | ** | | **** | | A | +------------+ +------------+ | 10 | | ** | | ****** | | ****** | | ** | | ****** | | 10 | +------------+ Your Hand Value: 21 You hit blackjack on the deal... You won 4 dollars. Enter 'c' to continue playing or 'q' to quit: c ---- HAND 2 ---- You have 122 dollars. How many dollars do you want to bet? 3 Dealing Cards... Dealer Shows: +------------+ | J | | ** ** | | ****** | | ****** | | **** | | ** | | J | +------------+ Player Hand: +------------+ | 4 | | ** ** ** | | ****** | | **** | | ** | | **** | | 4 | +------------+ +------------+ | 5 | | ** ** | | ****** | | ****** | | **** | | ** | | 5 | +------------+ Your Hand Value: 9 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: s You chose to stay. Dealer Hand: +------------+ | J | | ** ** | | ****** | | ****** | | **** | | ** | | J | +------------+ +------------+ | Q | | ** ** | | ****** | | ****** | | **** | | ** | | Q | +------------+ Dealer Hand Value: 20 You had 9 and the dealer had 20. You lost 3 dollars. Enter 'c' to continue playing or 'q' to quit: c ---- HAND 3 ---- You have 119 dollars. How many dollars do you want to bet? 6 Dealing Cards... Dealer Shows: +------------+ | 10 | | ** | | **** | | ****** | | **** | | ** | | 10 | +------------+ Player Hand: +------------+ | 2 | | ** ** ** | | ****** | | **** | | ** | | **** | | 2 | +------------+ +------------+ | 4 | | ** ** ** | | ****** | | **** | | ** | | **** | | 4 | +------------+ Your Hand Value: 6 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: h You chose to hit with a card. New card: +------------+ | A | | ** | | **** | | ****** | | **** | | ** | | A | +------------+ Your Hand Value: 17 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: h You chose to hit with a card. New card: +------------+ | 3 | | ** | | **** | | ****** | | **** | | ** | | 3 | +------------+ Your Hand Value: 20 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: s You chose to stay. Dealer Hand: +------------+ | 10 | | ** | | **** | | ****** | | **** | | ** | | 10 | +------------+ +------------+ | 9 | | ** | | **** | | ****** | | **** | | ** | | 9 | +------------+ Dealer Hand Value: 19 You had 20 and the dealer had 19. You won 6 dollars. Enter 'c' to continue playing or 'q' to quit: c ---- HAND 4 ---- You have 125 dollars. How many dollars do you want to bet? 3 Dealing Cards... Dealer Shows: +------------+ | 7 | | ** ** | | ****** | | ****** | | **** | | ** | | 7 | +------------+ Player Hand: +------------+ | J | | ** | | **** | | ****** | | **** | | ** | | J | +------------+ +------------+ | 3 | | ** ** | | ****** | | ****** | | **** | | ** | | 3 | +------------+ Your Hand Value: 13 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: h You chose to hit with a card. New card: +------------+ | 4 | | ** ** | | ****** | | ****** | | **** | | ** | | 4 | +------------+ Your Hand Value: 17 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: h You chose to hit with a card. New card: +------------+ | 10 | | ** ** | | ****** | | ****** | | **** | | ** | | 10 | +------------+ Your Hand Value: 27 You busted out and lost 3 dollars. Enter 'c' to continue playing or 'q' to quit: c ---- HAND 5 ---- You have 122 dollars. How many dollars do you want to bet? 6 Dealing Cards... Dealer Shows: +------------+ | Q | | ** | | **** | | ****** | | **** | | ** | | Q | +------------+ Player Hand: +------------+ | A | | ** ** | | ****** | | ****** | | **** | | ** | | A | +------------+ +------------+ | 7 | | ** ** ** | | ****** | | **** | | ** | | **** | | 7 | +------------+ Your Hand Value: 18 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: s You chose to stay. Dealer Hand: +------------+ | Q | | ** | | **** | | ****** | | **** | | ** | | Q | +------------+ +------------+ | 10 | | ** ** ** | | ****** | | **** | | ** | | **** | | 10 | +------------+ Dealer Hand Value: 20 You had 18 and the dealer had 20. You lost 6 dollars. Enter 'c' to continue playing or 'q' to quit: c ---- HAND 6 ---- You have 116 dollars. How many dollars do you want to bet? 4 Dealing Cards... Dealer Shows: +------------+ | 3 | | ** ** ** | | ****** | | **** | | ** | | **** | | 3 | +------------+ Player Hand: +------------+ | 2 | | ** ** | | ****** | | ****** | | **** | | ** | | 2 | +------------+ +------------+ | 7 | | ** ** | | ****** | | ****** | | **** | | ** | | 7 | +------------+ Your Hand Value: 9 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: d You chose to hit with a dice. New dice: D5 +---------------+ | * * | | | | * | | | | * * | +---------------+ Your Hand Value: 14 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: d You chose to hit with a dice. New dice: D2 +---------------+ | * | | | | | | | | * | +---------------+ Your Hand Value: 16 Enter 's' to stay, 'h' to hit on a card, or 'd' to hit on a dice: d You chose to hit with a dice. New dice: D5 +---------------+ | * * | | | | * | | | | * * | +---------------+ Your Hand Value: 21 Dealer Hand: +------------+ | 3 | | ** ** ** | | ****** | | **** | | ** | | **** | | 3 | +------------+ +------------+ | 6 | | ** | | **** | | ****** | | **** | | ** | | 6 | +------------+ Dealer Hand Value: 9 Dealer Hits... +------------+ | 3 | | ** ** | | ****** | | ****** | | **** | | ** | | 3 | +------------+ Dealer Hand Value: 12 Dealer Hits... +------------+ | J | | ** ** | | ****** | | ****** | | **** | | ** | | J | +------------+ Dealer Hand Value: 22 You had 21 and the dealer had 22. Dealer busts. You won 4 dollars. Enter 'c' to continue playing or 'q' to quit: q Thank you for playing! Here is your game history: Hand: 6 Player: 21 Dealer: 22 Result: Player won. Dealer bust. Hand: 5 Player: 18 Dealer: 20 Result: Player lost. Score below dealer. Hand: 4 Player: 27 Dealer: 18 Result: Player lost. Bust. Hand: 3 Player: 20 Dealer: 19 Result: Player won. Score above dealer. Hand: 2 Player: 9 Dealer: 20 Result: Player lost. Score below dealer. Hand: 1 Player: 21 Dealer: 5 Result: Player won. Blackjack on deal. Saving bank balance... Bank balance saved. Goodbye!

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/*
* File:   Game.cpp
* Author:
*
*
*/

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <sstream>
#include <string>
#include <iomanip>
#include <iostream>


using namespace std;


#include "Game.h"
#include "Dice.h"
#include "CardClub.h"
#include "CardDiamon.h"
#include "CardHeart.h"
#include "CardSpade.h"

/**
* constructor
* @param bank
* @param handNum
*/
Game::Game(int bank, int handNum) {
    this->bank = bank;
    this->handNum = handNum;
    deckSetup();
    shuffle();
    pushToQueue();
}

/**
* destructor
*/
Game::~Game() {
    // free all allocated memory on heap
    for (int i = 0; i < DECK_SIZE; i++) {
       delete deck[i];
    }
    delete [] deck;
}

/**
* getter
* @return bank
*/
int Game::getBank() {
    return bank;
}

/**
* set up deck
*/
void Game::deckSetup() {
    // create cards
    int k = 0;
    for (int i = 1; i <= GROUP_SIZE; i++) {
       deck[k] = new CardClub(i);
       k++;
       deck[k] = new CardDiamon(i);
       k++;
       deck[k] = new CardHeart(i);
       k++;
       deck[k] = new CardSpade(i);
       k++;
    }
}

/**
* shuffle deck
*/
void Game::shuffle() {
    srand((unsigned int)time(0));
    int index;
    Item * tmp;
    // shuffle cards 1000 times
    for (int i = 0; i < 1000; i++) {
       index = rand() % DECK_SIZE;
       tmp = deck[index];
       deck[index] = deck[0];
       deck[0] = tmp;
    }
}

/**
* push deck to queue
*/
void Game::pushToQueue() {
   
    // pus all card to queue
    for (int i = 0; i < DECK_SIZE; i++) {
       this->items.push(deck[i]);
    }
}

/**
* game play
*/
void Game::play() {

    cout << "---- HAND " << handNum << " ----" << endl;
    cout << "You have " << bank << " dollars." << endl;
    /**
    * Before any cards are dealt, the player makes
    * a wager from their available bank balance.
    */
    int bet = getBet();
    ostringstream os;
    os << "Hand: " << handNum << " ";



    /**
    * The objective is to get as close to 21 without going over.
    * The player and the dealer are each dealt two
    * cards. The player’s cards are dealt face up, and only one of the
    * dealer’s cards are shown.
    */
    cout << "Dealing Cards..." << endl << endl;
    dealer.initialize(items);
    player.initialize(items);

    cout << "Dealer Shows:" << endl;
    dealer.showHand();
    cout << "Player Hand:" << endl;
    player.showHand();

    cout << "Your Hand Value: " << player.getScore() << endl;

    /**
    * On the deal, if the player has 21 showing,
    * then the player scores a “Blackjack” and wins.
    */
    if (player.getScore() == 21) {
       cout << "You hit blackjack on the deal..." << endl;
       bank += bet;
       os << "Player: " << player.getScore() << "\t";
       os << "Dealer: " << dealer.getScore() << "\t";
       os << "Result: Player won.\tBlackjack on deal.";
       gameResult = os.str();
       return;
    }

    /**
    * If the player does not have 21 on the deal, then the player must “hit”
    * (take an extra card) or “stay” (waits for the dealer to complete
    * their hand). If the player goes over 21, then they lose and the dealer
    * wins. If the player chooses to stay, then they must wait for the dealer
    * to close out their hand (a dealer will automatically “hit” on a card
    * if the dealer’s hand is less than 17 (and they will keep hitting provided
    * their hand remains less than 17); if the dealer’s hand is 17 or greater,
    * then they will stay). If the dealer ends up with more than 21,
    * then the dealer busts and the player wins. If the player and dealer end up
    * tying (including 21), then nobody wins and no money changes hands.
    *      * Super Blackjack is “super” because the player can choose to
    * roll a die instead of taking a card. The value
    * (1-6) shown on the die is the value added to the player’s hand.
    */

    while (player.isStay() == false) {
       player.hit(items);
    }...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$84.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats