Write a program that will generate a random poker hand and then display it, plus
ID: 3622105 • Letter: W
Question
Write a program that will generate a random poker hand and then display it, plus determine the lowest rank card (display rank and suit, either one if there is a tie). Every run should be different. Display messages that there is pair, or three, or four, on none (so the tester knows you have this feature).Some suggestions:
Use two parallel arrays of int of length 5 - one for suit (random 4 values) and one for rank (random 13 values).
const int HANDSIZE=5;
int ranks[HANDSIZE];
int suits[HANDSIZE];
etc.
Another array of size 4 lists the 4 strings for the suits, and another array of size 13 lists the 13 strings for ranks (for display).
You may use global arrays (all four mentioned arrays).
Use a function to initialize the hand (use rand() to generate 5 ranks and 5 suits (all are integers see above). Use a function to display one card by referencing the other arrays, and call this function HANDSIZE times to display all cards.
For the extra, have a function that searches the array of ranks for repetitions.
Example run (should be reproduced):
Random hand generated as
2 Hearts
10 Spades
3 Clubs
Q Hearts
2 Spades
Lowest rank card is 2 Hearts // or 2 Spades
The hand has Pair// extra
..
Explanation / Answer
please rate - thanks
#include <iostream>
#include <ctime>
#include<iomanip>
using namespace std;
const int HANDSIZE=5;
void shuffle(int[],int[]);
void dealcards(int[],int[],int[],int[]);
bool checkflush(int[]);
bool checkstraight(int[]);
void same(int [],bool &,bool &,bool &,bool &,bool &);
int lowest(int[]);
int main()
{
int i,j,flushes,decks[52],deckn[52],low;
int suits[HANDSIZE],ranks[HANDSIZE],scored,count;
bool flush,straight;
bool pair,pair2,three,four,nothing;
string suit[4]={"hearts","diamonds","spades","clubs"};
string card[13]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
int deal;
srand(time(0));
scored=0;
shuffle(decks,deckn);
dealcards(suits,ranks,decks,deckn);
cout<<"Hand: ";
for(j=0;j<HANDSIZE;j++)
cout<<card[ranks[j]]<<" "<<suit[suits[j]]<<endl;
cout<<endl;
low=lowest(ranks);
cout<<"The lowest card is "<<card[low] <<" ";
for(i=0;i<HANDSIZE;i++)
if(ranks[i]==low)
cout<<suit[suits[i]]<<" ";
flush=checkflush(suits);
straight=checkstraight(ranks);
same(ranks,nothing,pair,pair2,three,four);
cout<<" scoring: ";
if(straight&&flush)
cout<<"straight flush ";
else if(pair&&three)
cout<<"full house ";
else if(straight)
cout<<"straight ";
else if(flush)
cout<<"flush ";
else if(four)
cout<<"four of a kind ";
else if(three)
cout<<"Three of a kind ";
else if(pair2)
cout<<"2 pair ";
else if(pair)
cout<<"one pair ";
else
cout<<"nothing ";
system("pause");
return 0;
}
int lowest(int card[])
{int i,low=card[0];
for(i=1;i<HANDSIZE;i++)
if(card[i]<low)
low=card[i];
return low;
}
bool checkstraight(int ranks[])
{int i,j,t;
for(i=0;i<4;i++)
for(j=i+1;j<HANDSIZE;j++)
if(ranks[i]>ranks[j])
{t=ranks[i];
ranks[i]=ranks[j];
ranks[j]=t;
}
for(i=1;i<5;i++)
if(ranks[i]!=ranks[i-1]+1)
return false;
return true;
}
void same(int ranks[],bool ¬hing,bool &pair,bool &pair2,bool &three,bool &four)
{bool used[13]={false};
nothing=false;
pair=false;
pair2=false;
three=false;
four=false;
int b[13]={0};
int i,j;
for( i = 0; i < HANDSIZE; i++)
{if(!used[ranks[i]])
{int count = 1;
for(int j = i+1; j <HANDSIZE; j++)
{
if(ranks[j] == ranks[i])
{
count++;
}
}
used[ranks[i]]=true;
b[ranks[i]]=count;
}
}
cout<<endl;
for(i=0;i<13;i++)
if(b[i]==4)
{four=true;
return;
}
else if(b[i]==3)
{three=true;
for(j=0;j<13;j++)
if(b[j]==2)
pair=true;
return;
}
else if(b[i]==2)
{pair=true;
for(j=0;j<13;j++)
if(b[j]==3)
three=true;
for(j=i+1;j<13;j++)
if(b[j]==2)
{pair2=true;
return;
}
return;
}
nothing=true;
return;
}
void dealcards(int suits[],int ranks[],int decks[],int deckn[])
{int i,j,cards=0;
for(i=0;i<5;i++)
{suits[i]=decks[cards];
ranks[i]=deckn[cards];
cards++;
}
return;
}
bool checkflush(int suits[])
{int j,count=0,type,handcount;
type=suits[0];
handcount=1;
for(j=1;j<HANDSIZE;j++)
if(suits[j]==type)
handcount++;
if(handcount==HANDSIZE)
return true;
else
return false;
}
void shuffle(int decks[],int deckn[])
{bool cards[4][13];
int i,j,num,type;
for(i=0;i<4;i++)
for(j=0;j<13;j++)
cards[i][j]=false;
for(j=0;j<52;j++)
{do
{
num=rand()%13;
type=rand()%4;
}while(cards[type][num]);
decks[j]=type;
deckn[j]=num;
cards[type][num]=true;
}
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.