Question

"Blackjack 40" my teacher created this assignment and I need help. He is using c++ and...

"Blackjack 40" my teacher created this assignment and I need help. He is using c++ and has provided this template: Further instructions are below the template!

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

//Will return a number from 1 to 13, representing the face of a card
int draw_card() { return rand() % 13 + 1; }


int main() {
const int BET = 10;
//This will allow you to control chance, to make testing easier
cout << "Please choose a random seed, or 0 to use the current time:\n";
int seed = 0;
cin >> seed;
if (seed == 0) srand(time(0));
else srand(seed);

cout << "Welcome to Blackjack-40!\n\n";

//YOU: Create an integer named money and set it to 100

//YOU: Do this (and all lines below) in an infinite loop...

cout << "You currently have $" << money << " and are betting $" << BET << endl;

//YOU: For each player...

//YOU: Make an integer called total holding the total value of the players card and initialize it to 0
//YOU: Using a for loop, deal 4 cards to the player
int card = draw_card(); //This is how you deal a card
//YOU: Handle face cards. I.e., if the card is >= 10 set the card to 10

//YOU: Handle aces (a 1 is worth 11)
cout << "You drew a " << card << endl;

//YOU: Add the card's value to the total

//Then we print the total
cout << "The total value of your cards is: " << total << endl;

//YOU: Make an infinite loop for the players drawing cards until they bust or stay or quit
cout << "Do you wish to 1) Hit or 2) Stay or 3) Quit?\n";

//YOU: After the players are done, draw cards for the dealer until they get a 35 or higher, or bust
cout << "Dealer drew a " << card << endl;

//YOU: Check for loss. If the player has 0 dollars or less, they lose
cout << "YOU LOSE! GAME OVER!\n";
//YOU: Check for win. If the player has 200 dollars or more, they win
cout << "A WINNER IS YOU! GAME OVER!\n";
}

For this assignment, you will be playing a variant of Blackjack where the
player tries to get as close to 40 as possible without going over. The player
starts with $100 and always $10 per hand. If they go over, they "go bust" and
pay $10 to the dealer.

Once they finish, the dealer will go, and will draw cards until they get a 36
or higher. If the dealer goes over 40, the dealer goes bust and pays $10 to
the player if they have not gone bust.

If the dealer doesn't go bust, then the dealer's score is compared with the
score of the player. If they player is higher than the dealer's, they gain
$10, they lose $10 if they have less, and no money is exchanged on a tie.

The value of a card is the face value (i.e. an 8 is worth 8 points, a
jack/queen/king is worth 10) except for aces, which count either as 11 or 1,
whichever is more advantageous for the player. I recommend when you start just
always treating it as an 11 and worrying about handling this after doing
everything else.

Playing a game of Blackjack-40:
1) The player is initially dealt four cards
2) If they are dealt a 40, they print "BLACKJACK 40!" and they win $20.
3) They can choose to hit or stay or quit. Hit means they are dealt another card.
If they hit and go over 40, they go bust and pay $10 to the dealer. If they
hit they can continue hitting until they bust or choose to stay or quit.
Stay means they keep their total and the dealer gets to go.
Quit means the game terminates.
4) After the player is done, deal cards for the dealer until they get a 35
or higher.
5) Handle the money payments as described above. If the player wins they gain
$10, if they lose they lose $10, if they tie no money is exchanged.
6) If the player hits $200 they win the game, if they hit $0 they lose the
game, and the game ends.
7) Otherwise, go back up to #1 and do it again

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code :-

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

//Will return a number from 1 to 13, representing the face of a card
int draw_card() { return rand() % 13 + 1; }
int main() {
   const int BET = 10;
   //This will allow you to control chance, to make testing easier
   cout << "Please choose a random seed, or 0 to use the current time:\n";
   int seed = 0;
   cin >> seed;
   if (seed == 0) srand(time(0));
   else srand(seed);

   cout << "Welcome to Blackjack-40!\n\n";

   //YOU: Create an integer named money and set it to 100
   int money = 100;
   int option;

   //YOU: Do this (and all lines below) in an infinite loop...
   while (true)
   {
       cout << "-----------------------------------------------------------------------------------"<<endl; //added this for readability
       cout << "You currently have $" << money << " and are betting $" << BET << endl;

       //YOU: For each player...
       //YOU: Make an integer called total holding the total value of the players card and initialize it to 0
       int total=0,dealer_total=0; //---mentioned for each player
      
       //YOU: Using a for loop, deal 4 cards to the player
       for(int i=0;i<4;i++)
       {
           int card = draw_card(); //This is how you deal a card
           //YOU: Handle face cards. I.e., if the card is >= 10 set the card to 10
           if (card>=10) card=10;
           //YOU: Handle aces (a 1 is worth 11)
           if (card==1) card=11;
           cout << "You drew a " << card << endl;
           //YOU: Add the card's value to the total
           total = total + card;
       }
       if (total == 40)
       {
           cout<<"Blackjack 40!!!"<<endl;
           money=money+BET+BET;
       }
       else if(total>40)
       {
           cout << "The total value of your cards is: " << total << endl;
           cout<<"YOU LOST THIS ROUND!"<<endl;
           money = money - BET;
       }
       else
       {
           //---Then we print the total
           cout << "The total value of your cards is: " << total << endl;

           //YOU: Make an infinite loop for the players drawing cards until they bust or stay or quit
           while (true)
           {
               cout << "Do you wish to 1) Hit or 2) Stay or 3) Quit?\n";
               cin>>option;
               if (option==1)
               {
                   int card = draw_card();
                   if (card>=10) card=10;
                   if (card==1) card=11;
                   cout << "You drew a " << card << endl;
                   total = total + card;
                   cout << "The total value of your cards is: " << total << endl;
                   if(total>40)
                   {
                       cout<<"YOU LOST THIS ROUND!"<<endl;
                       money = money - BET;
                       break;
                   }
                   if (total == 40)
                   {
                       cout<<"Blackjack 40!!!"<<endl;
                       money=money+BET+BET;
                       break;
                   }
               }
               else if (option==2)
               {
                   //YOU: After the players are done, draw cards for the dealer until they get a 35 or higher, or bust
                   for(;dealer_total<35;)
                   {
                       int card = draw_card();
                       if (card>=10) card=10;
                       if (card==1) card=11;
                       cout << "Dealer drew a " << card << endl;
                       dealer_total = dealer_total + card;
                   }
                   //---Used this cout to check value of dealers cards
                   cout << "The total value of dealer's cards is: " << dealer_total << endl;
                  
                   //---This means that either the dealer goes bust or both have a total less than 40
                   //---but yours is closer to it

                   if (dealer_total>40 || total>dealer_total)
                   {
                       cout<<"YOU WON THIS ROUND!"<<endl;
                       money=money+BET;
                   }
                   else if(total<dealer_total)
                   {
                       cout<<"YOU LOST THIS ROUND!"<<endl;
                       money = money - BET;
                   }
                   else if(total==dealer_total)
                   {
                       cout<<"ROUND TIED!"<<endl;
                   }
                   break;
               }
               else if (option==3)
               {
                   //---added this to display when user quits
                   cout<<"THANK YOU FOR PLAYING !!!"<<endl;
                   return 0;
               }
               else cout<<"Please enter a valid option"<<endl;
           }
       }
       //YOU: Check for loss. If the player has 0 dollars or less, they lose
       if (money==0)
       {
           cout << "YOU LOSE! GAME OVER!\n";
           break;
       }
       //YOU: Check for win. If the player has 200 dollars or more, they win
       else if (money==200)
       {
           cout << "A WINNER IS YOU! GAME OVER!\n";
           break;
       }
   }
   return 0;
}

Output :-

The last image with "ROUND TIED!" is on another play of the game.

Add a comment
Know the answer?
Add Answer to:
"Blackjack 40" my teacher created this assignment and I need help. He is using c++ and...
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
  • 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...

  • 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...

  • 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...

  • 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...

  • Must be in C++ Question: Blackjack (twenty-one) is a casino game played with cards. The goal...

    Must be in C++ Question: Blackjack (twenty-one) is a casino game played with cards. The goal of the game to draw cards that total as close to 21 points as possible without going over. All face cards count as 10 points, aces count as 1 or 11, and all other cards count their numeric value. The game is played against a dealer. The player tries to get closer to 21 (without going over) than the dealer. If the dealer busts...

  • For this assignment you will create a one-player variation of the game BlackJack. The game will...

    For this assignment you will create a one-player variation of the game BlackJack. The game will be divided into five rounds. The goal of each round is to accumulate a hand of cards that gets as close to 21 without going over. At the beginng of the game you will start with a score of 100 points. After each round, the difference between your hand and 21 will be subtracted from your total score. The object of the entire game...

  • I need help finishing my C++ coding assignment! My teacher has provided a template but I...

    I need help finishing my C++ coding assignment! My teacher has provided a template but I need help finishing it! Details are provided below and instructions are provided in the //YOU: comments of the template... My teacher has assigned a game that is similar to battleship but it is single player with an optional multiplayer AI... In the single player game you place down a destroyer, sub, battleship, and aircraft carrier onto the grid.... Then you attack the ships you...

  • 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...

  • 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);...

  • Player Class Represents a participant in the game of blackjack. Must meet the following requirements: Stores...

    Player Class Represents a participant in the game of blackjack. Must meet the following requirements: Stores the individual cards that are dealt (i.e. cannot just store the sum of the cards, you need to track which specific cards you receive) Provided methods: decide_hit(self): decides whether hit or stand by randomly selecting one of the two options. Parameters: None Returns: True to indicate a hit, False to indicate a stand Must implement the following methods: Hi!! i just need help with...

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