Question
I need help with my C++ code! My teacher had us make a blackjack game using c++ but i can not get my code to
I need help with my C++ code! My teacher had us make a blackjack game using c++ but i can not get my code to pass his auto-grader system which uses input and output files to grade the code. The code must produce the same exact outputs based on the inputs, both are provided below along with my code! The code can not be hardwired for just these inputs/outputs, it must work universally with all inputs producing correct outputs; the autograder uses similar numbers when grading but they are not exactly the same!
INPUT/OUTPUT FILES:
Input 1:
1
3
Output 1:
Please choose a random seed, or 0 to use the current time: Welcome to Blackjack-40!
You currently have $100 and are betting $10 You drew a 11 You drew a 10 You drew a 10 You drew a 5 The total value of your cards is: 36 Do you wish to 1) Hit or 2) Stay or 3) Quit?
Input 2:
1 1 1 1 2 3
Output 2:
Please choose a random seed, or 0 to use the current time: Welcome to Blackjack-40!
You currently have $100 and are betting $10 You drew a 11 You drew a 10 You drew a 10 You drew a 5 The total value of your cards is: 36 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 2 The total value of your cards is: 38 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 2 The total value of your cards is: 40 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 10 The total value of your cards is: 40 Do you wish to 1) Hit or 2) Stay or 3) Quit? Dealer drew a 4 Dealer drew a 2 Dealer drew a 8 Dealer drew a 3 Dealer drew a 4 Dealer drew a 11 Dealer drew a 5 The total value of the dealer's cards is: 37 Player wins 10 dollars You currently have $110 and are betting $10 You drew a 10 You drew a 2 You drew a 10 You drew a 10 The total value of your cards is: 32 Do you wish to 1) Hit or 2) Stay or 3) Quit?
Input 3:
1 1 2 2 1 1 1 2 1 2 1 1 2 1 2 1 2 3
Output 3:
Please choose a random seed, or 0 to use the current time: Welcome to Blackjack-40!
You currently have $100 and are betting $10 You drew a 11 You drew a 10 You drew a 10 You drew a 5 The total value of your cards is: 36 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 2 The total value of your cards is: 38 Do you wish to 1) Hit or 2) Stay or 3) Quit? Dealer drew a 2 Dealer drew a 10 Dealer drew a 4 Dealer drew a 2 Dealer drew a 8 Dealer drew a 3 Dealer drew a 4 Dealer drew a 11 Dealer drew a 5 The total value of the dealer's cards is: 39 Dealer wins 10 dollars You currently have $90 and are betting $10 You drew a 10 You drew a 2 You drew a 10 You drew a 10 The total value of your cards is: 32 Do you wish to 1) Hit or 2) Stay or 3) Quit? Dealer drew a 10 Dealer drew a 10 Dealer drew a 8 Dealer drew a 9 The total value of the dealer's cards is: 37 Dealer wins 10 dollars You currently have $80 and are betting $10 You drew a 4 You drew a 4 You drew a 4 You drew a 6 The total value of your cards is: 18 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 3 The total value of your cards is: 21 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 4 The total value of your cards is: 25 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 10 The total value of your cards is: 35 Do you wish to 1) Hit or 2) Stay or 3) Quit? Dealer drew a 5 Dealer drew a 10 Dealer drew a 10 Dealer drew a 2 Dealer drew a 10 The total value of the dealer's cards is: 37 Dealer wins 10 dollars You currently have $70 and are betting $10 You drew a 6 You drew a 5 You drew a 10 You drew a 6 The total value of your cards is: 27 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 10 The total value of your cards is: 37 Do you wish to 1) Hit or 2) Stay or 3) Quit? Dealer drew a 10 Dealer drew a 2 Dealer drew a 10 Dealer drew a 4 Dealer drew a 2 Dealer drew a 5 Dealer drew a 6 The total value of the dealer's cards is: 39 Dealer wins 10 dollars You currently have $60 and are betting $10 You drew a 6 You drew a 11 You drew a 5 You drew a 3 The total value of your cards is: 25 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 2 The total value of your cards is: 27 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 10 The total value of your cards is: 37 Do you wish to 1) Hit or 2) Stay or 3) Quit? Dealer drew a 11 Dealer drew a 5 Dealer drew a 3 Dealer drew a 4 Dealer drew a 10 Dealer drew a 7 The total value of the dealer's cards is: 40 Dealer wins 10 dollars You currently have $50 and are betting $10 You drew a 7 You drew a 10 You drew a 10 You drew a 3 The total value of your cards is: 30 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 10 The total value of your cards is: 40 Do you wish to 1) Hit or 2) Stay or 3) Quit? Dealer drew a 10 Dealer drew a 2 Dealer drew a 5 Dealer drew a 5 Dealer drew a 10 Dealer drew a 10 DEALER BUSTED! The total value of the dealer's cards is: 42 Player wins 10 dollars You currently have $60 and are betting $10 You drew a 11 You drew a 10 You drew a 3 You drew a 11 The total value of your cards is: 35 Do you wish to 1) Hit or 2) Stay or 3) Quit? You drew a 3 The total value of your cards is: 38 Do you wish to 1) Hit or 2) Stay or 3) Quit? Dealer drew a 4 Dealer drew a 6 Dealer drew a 10 Dealer drew a 10 Dealer drew a 8 The total value of the dealer's cards is: 38 Push. You currently have $60 and are betting $10 You drew a 2 You drew a 3 You drew a 10 You drew a 10 The total value of your cards is: 25 Do you wish to 1) Hit or 2) Stay or 3) Quit?
MY CODE:
#include #include #include 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: "; int seed = 0; cin >> seed;
if (seed == 0) srand(time(0)); else srand(seed);
cout << "Welcome to Blackjack-40! ";
//YOU: Create an integer named money and set it to 100 int money = 100; //YOU: Do this (and all lines below) in an infinite loop...
while (true) { 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; int aces_count = 0;
//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 it to 10 if (card >= 10) card = 10;
//YOU: Handle aces (a 1 is worth 11) if (card == 1) { aces_count++; card = 11; }
cout << "You drew a " << card << endl;
//YOU: Add the cards value to the total total += card; }
if (total > 40) { total -= 10; aces_count--; }
//Then we print the total
cout << "The total value of your cards is: " << total << endl;
//YOU: Check for BLACKJACK-40
if (total == 40) { cout << "BLACKJACK 40!" << endl; money += 20; continue; }
//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? "; int choice = 0;
cin >> choice;
if (!cin or choice > 3 or choice < 1) { cout << "BAD INPUT! "; exit(0); }
if (choice == 3) exit(0); if (choice == 2) break;
//HIT int card = draw_card(); if (card >= 10) card = 10;
if (card == 1) { aces_count++; card = 11; }
cout << "You drew a " << card << endl; total += card;
while (total > 40 and aces_count) { aces_count--; total -= 10; } cout << "The total value of your card is: " << total << endl;
if (total > 40) { cout << "BUSTED! "; break; } }
//YOU: After the players are done, draw cards for the dealer until the get a 35 or higher, or bust
int dealer_total = 0; aces_count = 0;
while (dealer_total < 35) { int card = draw_card();
if (card >= 10) card = 10;
if (card == 1) { aces_count++; card = 11; }
cout << "Dealer drew a " << card << endl; dealer_total += card;
while (total > 40 and aces_count) { aces_count--; dealer_total -= 10; }
cout << "The total value of the dealers cards is: " << dealer_total << endl;
if (total > 40) { cout << "DEALER BUSTED! "; break; }
} //YOU: Check for loss. If the player has 0 dollars or less, they lose if (total <= 0) { cout << "YOU LOSE! GAME OVER! "; break; }
//YOU: Check for win. If the player has 200 dollars or more, they win
else if (total >= 200) { cout << "A WINNER IS YOU! GAME OVER! "; break; } } return 0; }
GAME RULES
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started