Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// 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

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

// 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, if desired. * It is up to you if and how you want to use these */ 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 ****************/ int arrindex[52]; int index; for(int i=0;i=51;i++) { arrindex[i]=0; } for(int i=51;i=0;i--) { do{ index=rand()%52; } while(arrindex[index]!=0); arrindex[index]=1; cards[i]=index; } }

/** * 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 */ void printCard(int id) { /******** You complete ****************/ if(id>=0 && id10&&id=13&&id21) { cout=26&&id=35) { cout=39&&id=48&&id

}

/** * 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. */ int cardValue(int id) { /******** You complete ****************/ int value1[52]; for(int i=0;i=12;i++) { value1[i]=value[i]; } for(int i=13;i=25;i++) { value1[i]=value[i-13]; } for (int i=26;i=38;i++) { value1[i]=value[i-26]; } for(int i=39;i=51;i++ ) { value1[i]=value[i-39]; } return value1[id]; }

/** * 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 ****************/ for(int i=0;i=51;i++) { cards[i]=i; } shuffle(cards); return 0; }

CS 103 Lab - TwentyOne 1 Introduction In this assignment you will use write program to implement the basic rules of the card game, Twenty-One (aka Blackjack). This program will require you to utilize the following knowledge and skills: for and while loops conditionals functions arrays and passing arguments 2 Background 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. 3 The Rules 3.1 The Goal The goal of the card game is for the player's cards to total more than the dealer's 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. 3.2 Setup 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 player's cards are dealt face up (visible), the dealer's first card is kept hidden, while their second is visible. The player uses this information to help inform their choices. 3.3 The Player 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 Last Revised: 8/20/2018 CS 103 Lab - Twenty One 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). 3.4 Handling Aces 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 player's 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. 3.5 The Dealer If the player did not "bust" the dealer now plays. His/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. He/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 his total to 17 or more (but not over 21), he must count the ace as 11 and stand. The dealer's decisions, then, are automatic on all plays, whereas the player always has the option of taking one or more cards. 4 Representation 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) Cara Meger 2-H 2-S 26 27 3-C 2 15 Cara meger 13 3-S 14 4-S 5-S 16 6-S 7-S 18 8-S 19 29 3 4 5 17 6-C 31 Cara meger 2-D 3-D 4-D 28 5-D 6-D 30 7-D 8-D 9-D 10-D 34 J-D Q-D 36 K-D 37 A-D 38 3-H 4-H 5-H 6-H 7-H 8-H 9-H 10-H J-H Q-H K-H A-H 32 Carameger ID 2-C 39 40 4-C 41 5-C 42 43 7-C 44 8-C 9-C 10-C J-C 48 Q-C 49 K-C A-C 51 9-S 33 22 35 8 9 10 11 12 10-S J-S Q-S K-S A-S 24 50 25 5 Requirements 1. Shuffling: For each new hand, you should initialize the deck of 52 cards to have values 0-51 in that order (i.e. cards[i] = 1;) and then shuffle that deck using the Fisher-Yates / Durstenfeld shuffle algorithm (shown below) so that we all get the same card ordering: // To shuffle an array a of n elements (indices 0..n-1): for i from n-1 downto 1 do j. random integer such that OsjSi (i.e. use a modulus operation on the random number) exchange a [j] and a[i] As a check to ensure you are shuffling correctly, if you were to seed the random number generator with a value of 37 (which is the seed used in the first sample run shown below) and then printed out the contents of the deck after shuffling (for debug purposes), then they should be: 6 14 2 39 24 23 32 4 12 49 27 36 21 42 10 8 38 51 46 47 22 5 37 41 16 15 43 40 3 31 44 34 17 18 9 26 1 35 19 11 30 48 13 20 25 33 29 28 0 45 50 7 2. Initially, the dealer's first card should not be shown to the user. Instead display a 3. 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. Last Revised: 8/19/2018 v o @ Q Search CS 103 Lab - TwentyOne 4. You must generate the following output based on the results of the game: o Output Player busts on a separate line if the player goes over 21 OR output Dealer busts on a separate line if the dealer goes over 21. Also, output Win, Tie, or Lose indicating if the player won, tied, or lost to the dealer, respectively, followed the player score and the dealer score separate by a space (all on one line). 5. When the hand ends the player should be allowed to play again (without restarting the program or re-seeding the random number generator) by typing y to play another hand or n to quit. If the user types any character other than y the program should immediately quit. 6. Playing another hand should cause the deck to be reinitialized to 0-51 (i.e. cards[i] = 1;) and then shuffled (but do NOT re-seed the random number generator...just start again by calling shuffle().) 7. If the player busts, the dealer does not play and the desired output should be displayed. 6 Additional Background 1. Command line arguments: One other way we can provide input to our programs other than cin is via command line arguments. In this method, we provide input at the command line when we start our program (the information comes after the name of the executable). Then our program can access that info and use it. We will use this technique for the seed value to use. So now you will start your program by typing a seed value after the name of the executable. For example: $./twentyone 37 $./twentyone 20134 In the first example the program will use 37 as its seed and in the second program 20134 will be used as the seed. We will learn more about how to use these command line arguments shortly in our class, but for now we have provided all the code to deal with them in the skeleton file. You need not change/adjust that code. Simply realize you must type in the seed value at the command line when you start the program. 2. Using arrays to lookup values: One application of arrays is to pre-define values your program may want to use so you don't 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 Last Revised: 8/19/2018 CS 103 Lab - Twenty One value, type, and suit of a card. You do not have to use these arrays, though they will make your life easier if you do. To use them, you must generate the appropriate index to the array and then you can retrieve the value at that index. For example, const char* type [13] = {"2","3","4","5","6","7","8","9","10","J","0", "K","A"); can be used to print out the value of a card by writing: cout 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) Cara Meger 2-H 2-S 26 27 3-C 2 15 Cara meger 13 3-S 14 4-S 5-S 16 6-S 7-S 18 8-S 19 29 3 4 5 17 6-C 31 Cara meger 2-D 3-D 4-D 28 5-D 6-D 30 7-D 8-D 9-D 10-D 34 J-D Q-D 36 K-D 37 A-D 38 3-H 4-H 5-H 6-H 7-H 8-H 9-H 10-H J-H Q-H K-H A-H 32 Carameger ID 2-C 39 40 4-C 41 5-C 42 43 7-C 44 8-C 9-C 10-C J-C 48 Q-C 49 K-C A-C 51 9-S 33 22 35 8 9 10 11 12 10-S J-S Q-S K-S A-S 24 50 25 5 Requirements 1. Shuffling: For each new hand, you should initialize the deck of 52 cards to have values 0-51 in that order (i.e. cards[i] = 1;) and then shuffle that deck using the Fisher-Yates / Durstenfeld shuffle algorithm (shown below) so that we all get the same card ordering: // To shuffle an array a of n elements (indices 0..n-1): for i from n-1 downto 1 do j. random integer such that OsjSi (i.e. use a modulus operation on the random number) exchange a [j] and a[i] As a check to ensure you are shuffling correctly, if you were to seed the random number generator with a value of 37 (which is the seed used in the first sample run shown below) and then printed out the contents of the deck after shuffling (for debug purposes), then they should be: 6 14 2 39 24 23 32 4 12 49 27 36 21 42 10 8 38 51 46 47 22 5 37 41 16 15 43 40 3 31 44 34 17 18 9 26 1 35 19 11 30 48 13 20 25 33 29 28 0 45 50 7 2. Initially, the dealer's first card should not be shown to the user. Instead display a 3. 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. Last Revised: 8/19/2018 v o @ Q Search CS 103 Lab - TwentyOne 4. You must generate the following output based on the results of the game: o Output Player busts on a separate line if the player goes over 21 OR output Dealer busts on a separate line if the dealer goes over 21. Also, output Win, Tie, or Lose indicating if the player won, tied, or lost to the dealer, respectively, followed the player score and the dealer score separate by a space (all on one line). 5. When the hand ends the player should be allowed to play again (without restarting the program or re-seeding the random number generator) by typing y to play another hand or n to quit. If the user types any character other than y the program should immediately quit. 6. Playing another hand should cause the deck to be reinitialized to 0-51 (i.e. cards[i] = 1;) and then shuffled (but do NOT re-seed the random number generator...just start again by calling shuffle().) 7. If the player busts, the dealer does not play and the desired output should be displayed. 6 Additional Background 1. Command line arguments: One other way we can provide input to our programs other than cin is via command line arguments. In this method, we provide input at the command line when we start our program (the information comes after the name of the executable). Then our program can access that info and use it. We will use this technique for the seed value to use. So now you will start your program by typing a seed value after the name of the executable. For example: $./twentyone 37 $./twentyone 20134 In the first example the program will use 37 as its seed and in the second program 20134 will be used as the seed. We will learn more about how to use these command line arguments shortly in our class, but for now we have provided all the code to deal with them in the skeleton file. You need not change/adjust that code. Simply realize you must type in the seed value at the command line when you start the program. 2. Using arrays to lookup values: One application of arrays is to pre-define values your program may want to use so you don't 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 Last Revised: 8/19/2018 CS 103 Lab - Twenty One value, type, and suit of a card. You do not have to use these arrays, though they will make your life easier if you do. To use them, you must generate the appropriate index to the array and then you can retrieve the value at that index. For example, const char* type [13] = {"2","3","4","5","6","7","8","9","10","J","0", "K","A"); can be used to print out the value of a card by writing: cout

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

Computer Aided Database Design

Authors: Antonio Albano, Valeria De Antonellis, A. Di Leva

1st Edition

0444877355, 978-0444877352

More Books

Students also viewed these Databases questions

Question

Describe the functions of banks and the banking system.

Answered: 1 week ago

Question

Draw a schematic diagram of I.C. engines and name the parts.

Answered: 1 week ago

Question

Guidelines for Informative Speeches?

Answered: 1 week ago