Question: Part I: Create a function called total that will read the strings of a player's cards and add up their values. This will require if-else
Part I:
Create a function called "total" that will read the strings of a player's cards and add up their values. This will require if-else statements.
Cards 2-9 will have values equivalent to their rank. Tens and face cards (T, J, Q, K) will have values of 10.
The default value for an Ace A should be 11. Have the function check that the total is not above 21. If it is, then subtract 10 so that the Ace is now valued as 1.
Part II:
Create a function called "choice" where the user chooses to Hit or Stand based on the value of their hand. The user may type in "h" or "H" for hit and "s" or "S" for stand. Output text instructions to the user telling them what their choices are and how to make their selection.
If the user selects Hit, give them another card and display the total of their hand. If the user selects Stand, the program ends and the total of their hand is displayed.
Use a try-catch setup to throw errors. If a user types in an option other than Hit or Stand, throw and error and ask the user to enter their choice again.
Part III:
Put Parts I and II of this program together with your work from the previous project. Altogether the program should do these things:
1) Create a deck of 52 cards.
2) Draw two cards to form the player's hand.
3) Find the total value of the player's hand. The player "Busts" and the game ends if their total goes above 21.
4) Prompt the user to Hit or Stand. Hit draws another card and Stand ends the game.
5) Throw an error if the user types in anything other than the options for Hit or Stand.
My code so far:
// Part 3
#include
string method2ToDraw() {
string suit = "CDHS"; string Rank = "A23456789TJQK";
string cards = ""; for (int i = 0;i < 4;i++) { for (int j = 0;j < 13;j++) { cards.push_back(suit[i]); cards.push_back(Rank[j]); cards.push_back(' '); } } // cout << cards; int selectAtRandom = (rand() % 52); string draw = cards.substr(selectAtRandom * 3, 3); return draw; }
string shuffle() {
return method2ToDraw(); }
string draw() {
return shuffle(); }
void deal() { string hand;
hand = draw() + " " + draw(); cout << hand << endl; } int main() { srand(time(0)); deal();
system("pause"); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
