Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The game of Twenty-One is a popular card game that can have many players competing against a dealer. In our version, we will only allow

The game of Twenty-One is a popular card game that can have many players competing against a dealer. In our version, we will only allow ONE player vs. the dealer. While this game usually contains betting, we will leave out that aspect and just focus on implementing the rules of play and determining the winner.

The goal of the card game is for the players cards to total more than the dealers without going over a total of 21 (aka a bust). Non face-cards have the value shown on the card (i.e. 2-10) while Jacks, Queens, and Kings have a value of 10. Aces are special in that they have a value of 11 by default unless that value would cause the player to bust (i.e. have a total greater than 21) in which case the Ace may count as just 1.

In this implementation we will only using 1 deck of (52) playing cards. The deck will be shuffled and then two cards will be dealed to the player and dealer in an alternating fashion (i.e. first one card to the player then to the dealer, then a second card). While both of the players cards are dealt face up (visible), the dealers first card is kept hidden, while their second is visible. The player uses this information to help inform their choices.

Gameplay starts with the player who must decide whether to stand (not ask for another card) or hit (ask for another card in an attempt to get closer to a count of 21, or even hit 21 exactly). Thus, a player may stand on the two cards originally dealt him/her, or he/she may ask the dealer for additional cards, one at a time, until the player either decides to stand on the total (if it is 21 or under), or goes bust (if it is over 21). In the latter case, the player loses immediately and the game is OVER (i.e. the dealer need not play or take any cards).

In 21, ace cards can be counted as EITHER 11 or 1. The combination of an ace with another card is known as a soft hand, because the player can count the ace as a 1 or 11, and either draw cards or not. For example with a soft 17 (e.g. an ace and a 6), the total is 7 or 17. While a count of 17 is a good hand, the player may wish to draw for a higher total. If the player draws a card that causes a bust by counting the ace as an 11, the player is allowed to count the ace as a 1 instead and continue playing by standing or hitting (asking the dealer for additional cards, one at a time). Going back to our example, if the player hits and receives a 5 (e.g. their hand contains an ace, 6, and 5), the player can now let their Ace be a 1 for a total of 12 and continue gameplay. If the player were to hit and receive a second ace, then it too can count as a 1. Thus, any ACE can be counted as either 11 or 1, whichever helps make the sum total of the players hand closer to 21 without going over. In your code, you should count an ACE as 11 unless doing so causes the sum to be over 21 in which case you can recalculate the sum, making the ACE worth 1. Again, if there are 2 or more aces in a hand, one of them can count as 11 while the others can be counted as 1 each.

If the player did not bust the dealer now plays. Her face-down card is turned up. If the total is 17 or more, the dealer must stand. If the total is 16 or under, the dealer must take a card. She must continue to take cards until the total is 17 or more, at which point the dealer must stand. If the dealer has an ace, and counting it as 11 would bring her total to 17 or more (but not over 21), she must count the ace as 11 and stand. The dealers decisions, then, are automatic (prescribed) on all plays, whereas the player has always has choice, taking zero or more cards.

To represent the 52 cards of a deck we will just use integers in the range 0 to 51 (which we can store in an array). Each card will be assigned an ID from 0 - 51 in the following order: 0-12 = 2 -> A of hearts, 13-25 = 2 -> A spades, 26 - 38 = 2 -> A diamonds, 39 - 51 = 2 -> A of clubs. This is more explicitly shown in the table below. (Note: H = Hearts, S = Spades, D = Diamonds, C = Clubs)

^ SEE INCLUDED IMAGE

For each new hand, you should initialize the deck of 52 cards to have values 0-51 in that order (i.e. cards[i] = i;) and then shuffle that deck using the Fisher-Yates / Durstenfeld shuffle algorithm (shown below) so that we all get the same card ordering:

for i from n1 downto 1 do j random integer such that 0 j i (i.e. use a modulus operation on the random number) exchange a[j] and a[i]

Initially, the dealers first card should not be shown to the user. Instead display a ?

The player should be presented with the choice to type h to hit or s to stay. However, if the player has 21 from the initial 2 cards of their hand, the dealer should immediately start playing (i.e. no choice should be given to the player to hold or stay). Also, any character other than h or s should cause the program to immediately quit

Playing another hand should cause the deck to be reinitialized to 0-51 (i.e. cards[i] = i;) and then shuffled (but do NOT re-seed the random number generatorjust start again by calling shuffle().)

One application of arrays is to pre-define values your program may want to use so you dont have to hard code a bunch of if statements but instead just look up the desired value from an array. We have provided some arrays in our skeleton that can be used to easily determine the value, type, and suit of a card.

To use them, you must generate the appropriate index to the array and then you can retrieve the value at that index. By using simply arithmetic operations, the card ID (the number from 0-51) that we use to represent each card can be converted to the suit (0-3), type (0-12), and value (0-12).

In the implementation of the printCard and cardValue functions, you must use the specified arrays to lookup the appropriate values and characters. Specifically, you should not use a sequence of if..else if statements to determine the value or characters to print. Some portion of credit will be based on your adherence to these rules (see rubric on the last page).

Your output must match exactly like the format in the sample runs below. Including spaces, commas, colons, etc.

Samples included below:

$./twentyone 37 Dealer: ? 2-C Player: 8-H 4-H Type 'h' to hit and 's' to stay: h Player: 8-H 4-H K-S Player busts Lose 22 5

Play again? [y] y Dealer: ? J-H Player: 8-S 3-D Type 'h' to hit and 's' to stay: h Player: 8-S 3-D A-S Type 'h' to hit and 's' to stay: h Player: 8-S 3-D A-S Q-C Player busts Lose 22 20

Play again? [y] n

Sample 2:

$./twentyone 29 Dealer: ? 9-C Player: K-D J-D Type 'h' to hit and 's' to stay: s Dealer: Q-H 9-C Win 20 19

Play again? [y] y Dealer: ? 10-C Player: 6-C 6-H Type 'h' to hit and 's' to stay: h Player: 6-C 6-H 4-H Type 'h' to hit and 's' to stay: s Dealer: J-H 10-C Lose 16 20

Play again? [y] n

Sample 3:

$./twentyone 20132 Dealer: ? 3-D Player: 10-S A-S Dealer: 2-H 3-D 3-S 9-D Win 21 17

Play again? [y] n

Sample 4:

$./twentyone 4 Dealer: ? 9-C Player: 7-D 5-C Type 'h' to hit and 's' to stay: h Player: 7-D 5-C 7-C Type 'h' to hit and 's' to stay: s Dealer: 4-C 9-C 6-S Tie 19 19

Play again? [y] n

Sample 5:

$./twentyone 11 Dealer: ? 10-D Player: 7-D A-S Type 'h' to hit and 's' to stay: h Player: 7-D A-S 7-H Type 'h' to hit and 's' to stay: s Dealer: 2-S 10-D 3-S J-D Dealer busts Win 15 25

Play again? [y] n

/******************************************************************************* Twenty-One (Blackjack) Project /******************************************************************************/

// Add other #includes if you need #include #include

using namespace std;

/* Prototypes */ void shuffle(int cards[]); void printCard(int id); int cardValue(int id); void printHand(int hand[], int numCards); int getBestScore(int hand[], int numCards);

const int NUM_CARDS = 52;

/** * Global arrays to be used as look-up tables. You */ const char suit[4] = {'H','S','D','C'}; const char* type[13] = {"2","3","4","5","6","7", "8","9","10","J","Q","K","A"}; const int value[13] = {2,3,4,5,6,7,8,9,10,10,10,10,11};

/** * Should permute the deck of cards, effectively shuffling it. * You must use the Fisher-Yates / Durstenfeld shuffle algorithm * described in the assignment writeup. */ void shuffle(int cards[]) { /******** You complete ****************/

}

/** * Prints the card in a "pretty" format: "type-suit" * Examples: K-C (King of clubs), 2-H (2 of hearts) * Valid Types are: 2,3,4,5,6,7,8,9,10,J,Q,K,A * Valid Suits are: H, D, C, S * * Must use the suit and type arrays to lookup * the appropriate characters to print. */ void printCard(int id) { /******** You complete ****************/

}

/** * Returns the numeric value of the card. * Should return 11 for an ACE and can then * be adjusted externally based on the sum of the score. * * Must use the values array to lookup the appropriate * card return value. */ int cardValue(int id) { /******** You complete ****************/

}

/** * Should print out each card in the hand separated by a space and * then print a newline at the end. * Should use printCard() to print out each card. */ void printHand(int hand[], int numCards) { /******** You complete ****************/

}

/** * Should return the total score of the provided hand. * ACES should be treated as 11s to make the highest possible hand * and only being reduced to 1s as needed to avoid busting. */ int getBestScore(int hand[], int numCards) { /******** You complete ****************/

}

/** * Main program logic for the game of 21 */ int main(int argc, char* argv[]) { //--------------------------------------- // Do not change this code -- Begin //--------------------------------------- if(argc

int cards[52]; int dhand[9]; int phand[9]; //--------------------------------------- // Do not change this code -- End //---------------------------------------

/******** You complete ****************/

} image text in transcribed

image text in transcribed

J-HQ-HK-HA-H9101112J-SQ-SK-SA-S22232425J-DQ-DK-DA-D35363738J-CQ-CK-CA-C48495051

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

Imagine you remain in the job listed under point

Answered: 1 week ago

Question

Discuss cross-cultural differences in perception

Answered: 1 week ago

Question

Compare and contrast families and family roles across cultures

Answered: 1 week ago

Question

Compare and contrast sex and gender roles across cultures

Answered: 1 week ago