Hi, Program logic is working great for five card poker game and hand contains a
ID: 3623184 • Letter: H
Question
Hi,
Program logic is working great for five card poker game and hand contains a pair and iterated the loop to find how many times program ran until pair were dealt.but, it will not display the cards if it was dealt more than 10 times.
But, when i change the program to four of a kind and iterated in do... while loop to find how many times were dealt to determine four of a kind, my output shows no.of times were dealt but not displaying the cards along with suits if it was dealt more than 10 times. pls help me out to display my cards in output!!!
Note: I just highlighted the those two lines in different color.
#include <iostream>
#include <ctime> //Prototype for time
#include <iomanip>
#include <string.h>
using namespace std;
void shuffle(int[],int[]);
void dealcards(int[],int[],int[],int[],int&,int);
bool duplicates(int[]);
long num_of_times_dealt=0;
int main()
{
const int HANDSIZE=5;
int i,j,decks[52],deckn[52];
int count[13]={0};
int suits[HANDSIZE],ranks[HANDSIZE],cards=0,low;
string suit[4]={"hearts","diamonds","spades","clubs"};
string card[13]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
do
{
srand(time(0));
shuffle(decks,deckn);
dealcards(suits,ranks,decks,deckn,cards,HANDSIZE);
num_of_times_dealt++;
}while(!duplicates(ranks));
cout<<num_of_times_dealt<<" no. of times dealt displayed before printing cards "<<endl;
for(j=0;j<HANDSIZE;j++)
cout<<card[ranks[j]]<<" "<<suit[suits[j]]<<endl;
cout<<"The hand contains four of a kind. Number of times dealt: ";
cout<<num_of_times_dealt;
cout<<endl;
system("pause");
//return 0;
}
bool duplicates(int nums[]) //duplicates is a function to determine whether hand contains a four of a kind
{
int i,j;
int count[13]={0};
for(i=0;i<5;i++)
count[nums[i]]++;
for(i=0;i<13;i++)
if(count[i]==4)
return true;
return false;
} //end duplicates function
void dealcards(int hands[],int handn[],int decks[],int deckn[],int & cards,int HANDSIZE)
{
int i,j,t;
int rank[]={13,2,3,4,5,6,7,8,9,10,11,12};
for(i=0;i<5;i++)
{
hands[i]=decks[cards];
handn[i]=deckn[cards];
cards++;
} //end for loop
for(i=0;i<4;i++)
for(j=i+1;j<HANDSIZE;j++)
if(rank[handn[i]]>rank[handn[j]])
{
t=hands[i];
hands[i]=hands[j];
hands[j]=t;
t=handn[i];
handn[i]=handn[j];
handn[j]=t;
}// end if statement
return;
} //end dealcards function
void shuffle(int decks[],int deckn[]) //Shuffle cards in deck
{
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;
} // end for loop
return;
} // end function shuffle
regards,
Bala.
Explanation / Answer
please rate - thanks
you never answered me, but I found the original code
#include <iostream>
#include <ctime>
#include <iomanip>
#include <string.h>
using namespace std;
void shuffle(int[],int[]);
void dealcards(int[],int[],int[],int[],int&,int);
bool duplicates(int[],int);
int main()
{const int HANDSIZE=5; //5 cards per hand
int i,j,decks[52],deckn[52],count=0,count2,count4;
int suits[HANDSIZE],ranks[HANDSIZE],cards=50,low;
bool two=false,four=false;
string suit[4]={"hearts","diamonds","spades","clubs"};
string card[13]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
srand(time(0));
do
{if(cards+5>=51)
{shuffle(decks,deckn); //shuffle the deck
cards=0;
}
count++;
dealcards(suits,ranks,decks,deckn,cards,HANDSIZE); //deal the cards
for(j=0;j<HANDSIZE;j++)
cout<<card[ranks[j]]<<" "<<suit[suits[j]]<<endl; //output the hand
if(duplicates(ranks,2)) //check and report if any pairs
{cout<<"The hand contains a pair";
if(!two)
count2=count;
two=true;
}
else
cout<<"The hand does not contain a pair";
cout<<endl;
if(duplicates(ranks,4)) //check and report if any pairs
{cout<<"The hand contains 4 of a kind";
if(!four)
count4=count;
four=true;
}
else
cout<<"The hand does not contain 4 of a kind";
cout<<endl;
}while(!two || !four);
cout<<"the 1st pair was dealt on deal "<<count2<<endl;
cout<<"the 1st 4 of a kind was dealt on deal "<<count4<<endl;
system("pause");
return 0;
}
bool duplicates(int nums[],int n)
{int i,j;
int count[13]={0}; //set counters, 1 for each number ace throug king to 0
for(i=0;i<5;i++)
count[nums[i]]++; //count how many times a number occurs
for(i=0;i<13;i++)
if(count[i]==n) //if it occurs exactly n times
return true; //there is a match looking for
return false;
}
void dealcards(int hands[],int handn[],int decks[],int deckn[],int & cards,int HANDSIZE)
{int i,j,t;
int rank[]={13,2,3,4,5,6,7,8,9,10,11,12};
for(i=0;i<5;i++) //deal the first 5 cards in the deck
{hands[i]=decks[cards];
handn[i]=deckn[cards];
cards++;
}
/*
for(i=0;i<4;i++) //this loop sorts the cards into order, you can get
for(j=i+1;j<HANDSIZE;j++) //rid of it
if(rank[handn[i]]>rank[handn[j]])
{t=hands[i];
hands[i]=hands[j];
hands[j]=t;
t=handn[i];
handn[i]=handn[j];
handn[j]=t;
}
*/
return;
}
void shuffle(int decks[],int deckn[])
{bool cards[4][13]; //4 suits, 13 cards each
int i,j,num,type;
for(i=0;i<4;i++)
for(j=0;j<13;j++)
cards[i][j]=false; //set all the cards as not used yet
for(j=0;j<52;j++) //52 times, once per card
{do
{
num=rand()%13; //get a random number 0-12 for card
type=rand()%4; //get a random number 0-4 for suite
}while(cards[type][num]); //until that card wasn't generated
decks[j]=type; //put the card just generated
deckn[j]=num; //in the deck
cards[type][num]=true; //mark the card as used
}
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.