Question

In: Computer Science

Write a C++ program that inputs three integers in the range [1..13] that represent a hand...

Write a C++ program that inputs three integers in the range [1..13] that represent a hand of three cards. Your program should output an English evaluation of the hand.

In the output, cards will be described as follows:

- 2–10 are described by the words for their numeric values: two, three, etc.

- 1 is called an ace, 11 is called a jack, 12 is called a queen, and 13 is called a king.

The evaluation of a hand is based on the first of the following cases that matches the cards:

- All three cards are equal to each other.

Sample output for 4 4 4: You have three fours.

- The three cards form a straight (three successive values).

Sample output for 6 8 7: You have an eight-high straight.

- Two cards (a pair) are equal.

Sample output for 6 3 6: You have a pair of sixes.

- Whatever is the highest card.

Sample output for 11 1 8: You have a jack.

Recommend sorting the card values before trying to evaluate the hand –that makes it much easier. Also, work on one thing at a time: first determine what kind of hand (3 of kind, 2 of kind, straight,...) and then determine what is the “significant” card for that hand (it becomes very messy and exhausting if you try to do both at the same time!). Check for card values being equal to each other (card1 == card2), not specific values (card1 == 1 && card2 == 1 || card1 == 2 && card 2 == 2 || ...). If you check each value, your program will be hundreds of lines long!

Solutions

Expert Solution

Here is the C++ code to the given question.

Sample outputs are added at the end.

Code:

#include <iostream>
#include<string>

using namespace std;


/*function to get english word for card value*/
string getEnglishWord(int number){
    
    string words[]={"none","ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"}; /*defines a string array of english words for card values*/
    
    return words[number];   /*returns appropriate word for a given card*/
    
}



/*function to sort an array*/
void sort(int* inputCards){
    
    int temp;
    
    for(int i=0;i<3;i++)
        {               
                for(int j=i+1;j<3;j++)
                {
                        if(inputCards[i]>inputCards[j])
                        {
                                temp  =inputCards[i];
                                inputCards[i]=inputCards[j];
                                inputCards[j]=temp;
                        }
                }
        }
        
        
}



int main()
{
    
    int inputCards[3];   /*integer array to store input values for cards*/
    
    cout<<"Enter input cards[1:13]: ";
    
    for(int i=0;i<3;i++){
        
        cin>>inputCards[i];  /*takes input from user*/
        
    }
    
    sort(inputCards);   /*calls sort function to sort integer array*/
    
    if(inputCards[0]==inputCards[1] && inputCards[0]==inputCards[2]){     /*check if all three values are equal*/
        
        if(inputCards[0]==6)
           cout<<"\nYou have three "<<getEnglishWord(inputCards[0])<<"es.";
        else
           cout<<"\nYou have three "<<getEnglishWord(inputCards[0])<<"s.";
        
    }
    else if(inputCards[0]==1 && inputCards[1]==12 && inputCards[2]==13){   /*special case for three successive values*/
        
        cout<<"You have an "<<getEnglishWord(inputCards[0])<<"-high straight.";
        
    }
    else if(inputCards[0]+1==inputCards[1] && inputCards[1]+1==inputCards[2]){   /*check if three cards form a straight*/
    
        if(inputCards[2]==8 || inputCards[2]==1)   /*check if value is 8 or 1(to find article 'a' or 'an')*/
           cout<<"\nYou have an "<<getEnglishWord(inputCards[2])<<"-high straight.";
        else
           cout<<"\nYou have a "<<getEnglishWord(inputCards[2])<<"-high straight.";
        
    }
    else if(inputCards[0]==inputCards[1]){   /*check if two cards are equal*/
        
        if(inputCards[0]==6)
           cout<<"\nYou have a pair of "<<getEnglishWord(inputCards[0])<<"es.";
        else
           cout<<"\nYou have a pair of "<<getEnglishWord(inputCards[0])<<"s.";
        
    }
    else if(inputCards[1]==inputCards[2]){    /*check if two cards are equal*/
        
        if(inputCards[1]==6)
           cout<<"\nYou have a pair of "<<getEnglishWord(inputCards[1])<<"es.";
        else
           cout<<"\nYou have a pair of "<<getEnglishWord(inputCards[1])<<"s.";
        
    }
    else{     
        
        if(inputCards[2]==8 || inputCards[2]==1)   /*check if value is 8 or 1(to find article 'a' or 'an')*/
           cout<<"\nYou have an "<<getEnglishWord(inputCards[2])<<".";
        else
           cout<<"\nYou have a "<<getEnglishWord(inputCards[2])<<".";        
    }
    

    
    return 0;
}

Sample output-1:

Sample output-2:

Sample output-3:

Sample output-4:


Related Solutions

Write a program that inputs three integers in the range [1..13] that represent a hand of...
Write a program that inputs three integers in the range [1..13] that represent a hand of three cards. Your program should output an English evaluation of the hand. In the output, cards will be described as follows: - 2–10 are described by the words for their numeric values: two, three, etc. - 1 is called an ace, 11 is called a jack, 12 is called a queen, and 13 is called a king. The evaluation of a hand is based...
Code in C Write a program whose inputs are three integers, and whose outputs are the...
Code in C Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. Ex: If the input is: 7 15 3 the output is: largest: 15 smallest: 3 Your program must define and call the following two functions. The LargestNumber function should return the largest number of the three input values. The SmallestNumber function should return the smallest number of the three input values. int...
Write a program Write a program whose inputs are three integers, and whose output is the...
Write a program Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: 7 15 3 the output is: 3 C++ please
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a program whose inputs are three integers, and whose output is the smallest of the...
Write a program whose inputs are three integers, and whose output is the smallest of the three values
Write a program whose inputs are three integers, and whose output is the smallest of the...
Write a program whose inputs are three integers, and whose output is the smallest of the three values. Use else-if selection and comparative operators such as '<=' or '>=' to evaluate the number that is the smallest value. If one or more values are the same and the lowest value your program should be able to report the lowest value correctly. Don't forget to first scanf in the users input. Ex: If the input is: 7 15 3 the output...
in coral write a program whose inputs are three integers, and whose output is the largest...
in coral write a program whose inputs are three integers, and whose output is the largest of the three values. Ex: If the input is 7 15 3, the output is: 15
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT