In: Computer Science
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!
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: