Question
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
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
//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; }
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