Question: C++ Part I: Here is one way to draw a random card from a deck. Create a string called suit that contains the characters CDHS,
C++
Part I:
Here is one way to draw a random card from a deck.
Create a string called suit that contains the characters CDHS, which stand for Clubs, Diamonds, Hearts, and Spades.
Create another string called rank that contains numbers 2-9, T for ten, J for Jack, Q for Queen, K for King, and A for Ace.
Use the rand() function to select a random suit (out of 4) and a random rank (out of 13). Store these variables as strings.
Concatenate the strings together to make a new string called card.
string suit[4] = { "C", "D", "H", "S" };
string faces[13] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" };
Part II:
Here is another way to draw a random card from a deck.
First, we will create a stringstream called deck. As before, create strings suit and rank.
Next, use two for loops, one inside the other, to cycle through each rank and suit to create a string called cards as follows:
CA C2 C3 ST SJ SQ SK
Finally, use the rand() function to pick a random card (out of 52). Store the result in a substring called draw.
Hint: Your string should have 156 characters. Each card consists of 3 characters: a suit, a rank, and a space. Make your substring as follows:
draw = cards.substr(3*x, 3);
Part III:
Finally, we will create functions to deal two cards to a Blackjack player.
Create a function called shuffle() with no parameters that creates the deck using a stringstream from Part II.
Create another function called draw with no parameters that will use shuffle() to randomly choose a number out of 52 and create a substring as in Part II.
Lastly create a function called deal() that calls the draw function twice and then creates a string called hand that will concatenate the two cards draw and display a Tab space between them.
The main function should only consist of calling deal(), which will display a players hand, such as the following:
C8 SJ
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
