Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the program is to be written in C++. It also must use iostream library. any help

ID: 3606680 • Letter: T

Question

the program is to be written in C++. It also must use iostream library. any help would be greatly appreciated!

Write a program that analyzes a poker hand for 5 card stud. The following link may be helpful if you are a bit unfamiliar with poker rules: http://en.wikipedia.org/wiki/List_of_poker hands For this program, assume there are no jokers, and the highest hand possible is a "Royal Flush", which is a Straight Flush with an Ace High. Hands in order of decreasing value are thus Royal Flush Straight Flush Four of a Kind Full House Flush Straight Three of a Kind Two Pairs One Pair High Card

Explanation / Answer

// sorry for no indentaion. i could not re indent it here in the answer. it was indented well in the IDE where i wrote the //code
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool cust_comp_func1(pair<int,int> a,pair<int,int> b){
return a.first<=b.first;
}
bool cust_comp_func2(pair<int,int> a,pair<int,int> b){
return a.second<=b.second;
}
bool ss(vector<pair<int,int>> &v){
return (v[0].second==v[1].second)&&(v[2].second==v[1].second)&&(v[2].second==v[3].second)&&(v[3].second==v[4].second);
}
bool seq(vector<pair<int,int>> &v){
sort(v.begin(),v.end(),cust_comp_func1);
return (v[0].first+1==v[1].first)&&(v[1].first+1==v[2].first)&&(v[2].first+1==v[3].first)&&(v[3].first+1==v[4].first);
}
bool mseq(vector<pair<int,int>> &v){
sort(v.begin(),v.end(),cust_comp_func1);
return (v[0].first==1)&&(v[1].first==10)&&(v[2].first==11)&&(v[3].first==12)&&(v[4].first==13);
}
bool op(vector<pair<int,int>> &v){
sort(v.begin(),v.end(),cust_comp_func1);
int arr[13]={0},i;
for(i=0;i<5;i++){
arr[v[i].first-1]++;
}
int count=0;
for(i=0;i<13;i++){
if(arr[i]==2){
count++;
}
}
return count==1;
}
bool tp(vector<pair<int,int>> &v){
sort(v.begin(),v.end(),cust_comp_func1);
int arr[13]={0},i;
for(i=0;i<5;i++){
arr[v[i].first-1]++;
}
int count=0;
for(i=0;i<13;i++){
if(arr[i]==2){
count++;
}
}
return count==2;
}
bool toak(vector<pair<int,int>> &v){
sort(v.begin(),v.end(),cust_comp_func1);
int arr[13]={0},i;
for(i=0;i<5;i++){
arr[v[i].first-1]++;
}
int count=0;
for(i=0;i<13;i++){
if(arr[i]==3){
count++;
}
}
return count;
}
bool foak(vector<pair<int,int>> &v){
sort(v.begin(),v.end(),cust_comp_func1);
int arr[13]={0},i;
for(i=0;i<5;i++){
arr[v[i].first-1]++;
}
int count=0;
for(i=0;i<13;i++){
if(arr[i]==4){
count++;
}
}
return count;
}
void highcard(vector<pair<int,int>> &v){
sort(v.begin(),v.end(),cust_comp_func1);
cout<<"high card:";
if(v[0].first==1){
cout<<" ACE ";
}
else{
int k=v[4].first;
if(k<11){
cout<<" "<<k<<endl;
}
else{
if(k==11){
cout<<" J ";
}
else{
if(k==12){
cout<<" K ";
}
else{
cout<<" Q ";
}
}
}
}
}
int main(){
cout<<"this is a program to 2 analyse a poker hand of 5 cards. assuming no jokers: enter cards as :";
int i=1,num,suit;
vector<pair<int,int>> v; //<num,suit>
for(;i<6;i++){
cout<<"card no. "<<i<<": enter number (1 for ace, 2, 3,...10, 11 for J, 12 for K, 13 for Q ): ";
cin>>num;
cout<<"suit( 1 for heart, 2 for spade, 3 for diamond , 4 for club) :";
cin>>suit;
v.push_back(make_pair(num,suit));
}
cout<<"input recieved: ";
pair<int,int> p;
for(i=0;i<5;i++){
p=v[i];
if(p.first==1){
cout<<"Ace ";
}
else{
if(p.first==11){
cout<<"J ";
}
else{
if(p.first==12){
cout<<"K ";
}
else{
if(p.first==13){
cout<<"Q ";
}
else{
cout<<p.first<<" ";
}
}
}
}
if(p.second==1){
cout<<"Hearts ";
}
else{
if(p.second==2){
cout<<"Spades ";
}
else{
if(p.second==3){
cout<<"Diamonds ";
}
else{
cout<<"Clubs ";
}
}
}
}
cout<<" ANALYSIS:... ";
bool samesuit=ss(v),insequence=seq(v),maxseq=mseq(v),onepair=op(v),twopair=tp(v),threeofakind=toak(v),fourofakind=foak(v);
if(samesuit){
if(maxseq){
cout<<"ROYAL FLUSH ";
return 0;
}
if(insequence){
cout<<"STRAIGHT FLUSH ";
}
else{
cout<<"FLUSH ";
}
return 0;
}
if(insequence){
cout<<"STRAIGHT ";
}
else{
if(fourofakind){
cout<<"FOUR OF A KIND";
return 0;
}
if(threeofakind){
if(onepair){
cout<<"FULL HOUSE ";
}
else{
cout<<"three of a kind ";
}
}
else{
if(twopair){
cout<<"TWO PAIR ";
}
else{
if(onepair){
cout<<"ONE PAIR ";
}
else{
highcard(v);
}
}
}
}
}