Answer to Question #176791 in C++ for simisola oluwaseyitan alabi

Question #176791

Convert the Deal Opening Hand program from Chapter 5 to incorporate functions to perform the two core tasks of 1) creating a card, and 2) determining the winner. The solution to Chapter 5 (Version 2 – Looping version) is attached, and you should use it as a baseline. You will need to take the functionality that is used to create a card and incorporate it into a function (create_card) that generates a card (suit, face, value) and returns these values to the calling/main program, that will then use these “card values” to 1) Print the card as in previous versions, and 2) use the value of each card as part of the calculation for the total hand value for each of the hands (total value for player hand and total value for dealer's hand) that is then passed to a function (highest_hand). The highest_hand function will then determine who has the highest hand and return who this is to the calling function, which will then print who has the highest hand, as in previous versions. 


1
Expert's answer
2021-03-30T06:56:00-0400
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    // Variables
    int suit_num, card_num;
    int player_card1_value, player_card2_value, player_hand_value;
    string player_card1_suit;
    string player_card2_suit;
    string player_card1_name;
    string player_card2_name;
    int dealer_card1_value, dealer_card2_value, dealer_hand_value;
    string dealer_card1_name;
    string dealer_card2_name;
    string dealer_card1_suit;
    string dealer_card2_suit;
    // Seed the random number generator
    srand(time(0));
    
    while(true){
        
        
        // Deal Player Hand
        // For loop for cards
        for(int loop =1;loop<=4;loop++){
            int card_value;
            string card_name,card_suit;
            suit_num = rand() % 4 + 1; // Generate a random number 1-4
            switch(suit_num)
            {
                case 1:
                    card_suit = "Hearts";
                    break;
                case 2:
                    card_suit = "Diamonds";
                    break;
                case 3:
                    card_suit = "Spades";
                    break;
                case 4:
                    card_suit = "Clubs";
                    break;
            }
            
            card_num = rand() % 13 + 1;
    
            switch(card_num)
            {
                case 1:
                    card_value = 11;
                    card_name = "Ace";
                    break;
                case 2:
                    card_value = 2;
                    card_name = "Two";
                    break;
                case 3:
                    card_value = 3;
                    card_name = "Three";
                    break;
                case 4:
                    card_value = 4;
                    card_name = "Four";
                    break;
                case 5:
                    card_value = 5;
                    card_name = "Five";
                    break;
                case 6:
                    card_value = 6;
                    card_name = "Six";
                    break;
                case 7:
                    card_value = 7;
                    card_name = "Seven";
                    break;
                case 8:
                    card_value = 8;
                    card_name = "Eight";
                    break;
                case 9:
                    card_value = 9;
                    card_name = "Nine";
                    break;
                case 10:
                    card_value = 10;
                    card_name = "Ten";
                    break;
                case 11:
                    card_value = 10;
                    card_name = "Jack";
                    break;
                case 12:
                    card_value = 10;
                    card_name = "Queen";
                    break;
                case 13:
                    card_value = 10;
                    card_name = "King";
                    break;
            }
            //Alloting card to respective variables based on loop count
            
            switch(loop){
                
                case 1:
                    player_card1_name = card_name;
                    player_card1_suit = card_suit;
                    player_card1_value = card_value;
                    break;
                case 2:
                    player_card2_name = card_name;
                    player_card2_suit = card_suit;
                    player_card2_value = card_value;
                    break;
                case 3:
                    dealer_card1_name = card_name;
                    dealer_card1_suit = card_suit;
                    dealer_card1_value = card_value;
                    break;
                case 4:
                    dealer_card2_name = card_name;
                    dealer_card2_suit = card_suit;
                    dealer_card2_value = card_value;
                    break;
            }
                
        }
        
    
        // Display the hands
            // Use the card names assigned when card was generated and dealt
        cout << "* Player Hand *" << endl;
        cout << player_card1_name << " of " << player_card1_suit << endl;
        cout << player_card2_name << " of " << player_card2_suit << endl << endl;
    
        cout << "* Dealer Hand *" << endl;
        cout << dealer_card1_name << " of " << dealer_card1_suit << endl;
        cout << dealer_card2_name << " of " << dealer_card2_suit << endl << endl;
        //Determine who has the highest opening hand
        player_hand_value = player_card1_value + player_card2_value;
        dealer_hand_value = dealer_card1_value + dealer_card2_value;
    
        if(player_hand_value > dealer_hand_value) {
            cout << "Player has the highest opening hand." << endl;
        } else if(player_hand_value < dealer_hand_value) {
            cout << "Dealer has the highest opening hand." << endl;
        } else {
            cout << "Player and Dealer Push" << endl;
        }
        
        string command;
        while(true){
            cout<<"Do you wish to deal another opening hand(Y/N): "<< endl;
            cin>>command;
            if(command!="Y" && command!="N"){
                cout<<"Enter Y/N only"<< endl;
            }
            if(command =="Y" || command=="N"){
                break;
            }
        }
        if(command == "N"){
            break;
        }
    }


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS