// main program // Repeatedly plays any number of hands of Blackjack, // until p
ID: 3548286 • Letter: #
Question
// main program // Repeatedly plays any number of hands of Blackjack, // until player runs out of money, or voluntarily leaves the game // The deck is shuffled periodically when it gets below SHUFFLE cards // In each round, players make their bets, the cards are dealt, // the players play their cards, and then the winners get paid // Calls: initializeDeck, deal, playerPlay, dealerPlay, payoff // these next few are just simple utilities to evaluate cards // decoding and interpreting the values encoded within integers. // An implementation using struct's could actually store these values // within the data so these functions would then not be necessary. // cardValue // determines the value of a card for purposes of totaling a hand // all face cards count as 10; aces count here as 1 // Parameter: // card (input int) encoded card value 0-51 // Returns: // an integer value in the range 1-10 // cardName // Determines the name of a card, such as "Three of Spades" // Parameter: // card (input int) encoded card value 0-51 // Return Value // a string value, obtained with the help of the assign() and // and append() methods described in <string> // handTotal // computes the value of a hand, given the cards within it // aces will count either as 1 or 11, depending on which is more favorable // Parameters: // hand (input int array) the cards in hand, encoded in 0-51 // handSize (input int) the number of cards in the hand // Returns: // integer holding the total value of the cards as counted most favorably // Calls: cardValue // these functions describe simple operations involving the deck of cards // these functions would behave very similarly for arrays of structs // initializeDeck // Creates a large deck of cards (multiple decks) and shuffles it // the total number of decks to use is a global constant // Parameters: // shoe (output int array) the shuffled deck of all the cards // shoeSize (output int) the number of cards in this deck // drawCard // Draws a card from the shoe and places it into a player's (or dealer's) hand // The shoe array is interpreted to have element 0 at the bottom of the deck // Parameters: // shoe (modified int array) overall deck to draw from // shoeSize (modified int) number of cards remaining in that deck // hand (modified int array) hand to draw into // handSize (modified int) number of cards in the hand // dealCard // Deals cards at the beginning of each round // A player will get one hand with two cards, and the dealer gets two cards // Parameters: // shoe (modified int array) overall deck to draw from // shoeSize (modified int) number of cards remaining in the deck // playerCards (output 2D int array) initial deal of cards for players // playerHandSize (output int array) sizes of all player hands (i.e. 2 cards) // playerHands (output int) number of hands per player (i.e. 1) // dealerCards (output int array) cards dealt to the dealer (two cards) // Calls: drawCard, cardName // these functions relate more closely to the application as a whole // playerPlay // handles the player's turn and decisions involving his cards // the player starts with just one hand, but may get more hands by splitting pairs // Parameters: // shoe (modified int array) deck to draw from // shoeSize (modified int) number of cards in the deck // playerCards (modified 2D int array) player's hand(s) // playerHandSize (modified int array) number of cards in each hand // playerHands (modified int) number of hands (initially 1) // bets (modified int array) wagers per hand // stake (modified int) money to play with, in case new bets are made // Calls: cardName, cardValue, handTotal, dealCard // dealerPlay // handles the dealer's turn, hitting until he reaches Soft 17 // Parameters: // shoe (modified int array) deck to draw from // shoeSize (modified int) number of cards in the deck // dealerCards (modified int array) hand to draw into // dealerTotal (output int) value of dealer's hand // NOTE: The size of the dealerCards array is not included here. // It is known to be 2 initially, and does not need to be known afterwards // Calls: cardName, handTotal, dealCard // payoff // Pays off the winning hands at the end of each round // Blackjack (21 on two cards) pays of at 3:2 odds // Other wins pay at 1:1 odds. // Since all the bets have already been taken out of the stake, // only winnings and reimbursements for push (tie) will be added to it // and nothing further will be subtracted. // Parameters: // playerCards (input 2D int array) the cards the player is holding // playerHandSize (input int array) number of cards in each hand // playerHands (input int) the number of hands // dealer (input int) value of dealer's hand // bets (input int array) wagers made on each hand // stake (modified int) player's cash total (may be increased) // Calls: handTotal
Explanation / Answer
#include <iostream>#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;
int dealOneCard();
int main() {
int i = 1;
srand(time(NULL));
string player1; // used to know player1's name;
string comp; // used to know player2's name;
string choice1; // used to decide hit or stay
string choice2; // used to decide hit or stay
int player1Total = dealOneCard() + dealOneCard(); //Player 1 is dealt two cards;
int compTotal = dealOneCard() + dealOneCard(); //Player 2 is dealt two cards;
string aceChoice;
int cardCharlie = 0;
// asks and stores name
cout << "enter your name please player 1 " << endl;
cin >> player1;
cin.get();
// test for a blackjack
if (player1Total == 21) {
i = 2;
cout << "congratulations " << player1 << " you win with Blackjack." << endl;
cin.get();
}
if (compTotal == 21) {
cout << "Computer wins with blackjack."<< endl;
cin.get();
}
// shows original hand of player1
while (i == 1) {
cout << "Hello " << player1 << " you currently have a total of " << player1Total << " points. Would you like to hit or stay." << endl;
cin >> choice1;
////////////// handles what player1 wants to do
if (choice1 == "hit") {
player1Total += dealOneCard();
cardCharlie ++;
} if (choice1 == "stay") {
i = 2;
cout << "You still have a total of " << player1Total << " points" << endl;
} if (choice1 == "hit" && dealOneCard() == 11) {
cardCharlie ++;
if (player1Total + 11 < 22) {
player1Total += 11;
cout << "ace is automaticall an 11 " << player1Total;
} else {
player1Total += 1;
cout << "ace is automatically a 1 " << player1Total;
}
}
// handles 5 card charlie
if (cardCharlie == 5) {
cout << player1 << "got 5 card charlie and wins";
}
// handles if player 1 busts
if (player1Total > 21) {
i = 2;
cout << player1 << " has busted. Computer Wins" << endl;
cin.get();
}
}
// descides when the computer should hit
while (compTotal < 17 && compTotal < player1Total && player1Total < 22) {
compTotal = compTotal + dealOneCard();
} if (compTotal > 16) {
}
if (compTotal > 21) {
cout << "Computer has busted with " << compTotal << " points. You win." << endl;
}
// descides who wins
if (player1Total > compTotal && player1Total < 22) {
cout << "Congratulations " << player1 << ". You win." << endl;
} else if (compTotal > player1Total && compTotal < 22) {
cout << "Computer wins with " << compTotal << " points." << endl;
}
// recognizes a tie
if (player1Total == compTotal) {
cout << "Tie Game." << endl;
}
cin.get();
return 0;
}
int dealOneCard()
{
return rand() % 10 + 2;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.