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

C++ Program : BLACK Jack Overloadoperator as class member Needs to include Class

ID: 3772993 • Letter: C

Question

C++ Program : BLACK Jack

Overloadoperator as class member

Needs to include Classes /mambers public privet,mutator ,accessor

enum

arrays

--------------------------------------

The game is based on a deck of 52 playing cards. Cards are given a number value: the Ace has a value of 11 only (in our version of the game the Ace cannot have a value of 1), Face cards (the Jack, Queen, and King) have a value of 10, all other cards have the value as indicated on them.

- The objective for the player is to collect cards that total or nearly total a value of 21 without going over. (The players collection of cards is called a "hand".) The dealer (computer) will also be collecting cards. If both the player and dealer have cards totaling 21 or less the dealer of player's hand with the highest value wins.

- The game starts by shuffling the deck (randomizing the order of cards in the deck.)

- Then the player and dealer are each given two cards from the top of the deck. The dealers card values are hidden. The players card values are shown.

- After being dealt the initial two cards a player may request another random card from the top of the deck. (This is called a 'hit'.) The value of that card is then added to the total card value of the players hand. The dealer is then given the next card on the deck and that cards value is added to its hand.

- The previous step is repeated until one of the following occurs:

The player no longer want to take another card from the top of the deck. (This is called "standing.") Then the dealer or player with the higher hand value wins.

The dealer or player's hand value is greater than 21. If the dealer or players hand value is greater than 21 the game stops and the player or dealer with a hand value greater than 21 loses.

The dealer or players hand is equal to 21. If the dealer or player's hand value is equal to 21 the game stops and the player or dealer with a hand value equal to 21 wins.

Explanation / Answer

#include <iostream>
#include <ctime>
#include <string>

using namespace std;

void play_games(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);


void main(){

       char keepPlaying = 'n'; //loop control variable

       do {
              play_games();
        
              //keep playing?
             cout << "Do you want to play another hand (y/n)?";
             cin >> keepPlaying;
     } while(keepPlaying == 'Y' || keepPlaying == 'y');
}

void play_games(void){
        //play one hand of 21

        //randomize the cards
        srand((int) time(0));

       // deal the cards
         int person = dealCards(2, "Your Cards:");
         cout << " = " << person << endl;
         int house = dealCards(2, "Computers Cards:");
         cout << " = " << house << endl;

        // Ask if human wants a hit and keep hitting...
        hit(person);
        cout << endl;

       //Determine if computer takes a hit
       while ((house < person) && (house <= 21) && (person <= 21))
       {
               house += dealCards(1, "The Computer takes a card ");
               cout << endl;
        }

       //show who won....
       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)
//This function deals 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 << 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