Write a method with the following header which generates a r
Write a method with the following header which generates a random card by randomly generating integers in the appropriate ranges for the card’s kind and suit. (Note that zero is valid for the suit but not for the kind.) The method should return a reference to an array of integers containing two elements – the first element represents the card’s kind and the second the card’s suit. public static int[] drawCard()
Write a method with the following header which generates a random 5-card hand. It should make one or more calls to drawCard()to generate each card in the hand. Before adding a card to the hand, it must check whether a card with the same kind and suit already appears in the hand. If so, it must not add the card to the hand but instead continue calling drawCard()until a card is generated whose combination of kind and suit does not yet appear in the hand. Once a hand consisting of 5 distinct cards has been generated, the method should return a reference to the two dimensional array representing the hand. public static int[][] drawHand()
Next, write a method with the following header that classifies the hand into one of the following categories, which are listed in increasing order of rank. The method should return an integer from 1 to 17 indicating the highest of the 17 ranks below which the hand satisfies. You must remember that when considering whether a hand is a straight or straight flush, an Ace can count as either a 1 or a 14, but not both. A Royal Flush is a Straight Flush in which a 10 is the lowest ranking card in the hand, such as 10S, JS, QS, KS, AS. Again, order does not matter, so QH, 10H, AH, KH, JH is also a Royal Flush. public static int classifyHand(int[][] hand)
Exapmle: 1. Seven high 2. Eight high 3. Nine high 4. Ten high 5. Jack high 6. Queen high 7. King high 8. Ace high 9. One Pair 10. Two pair 11. 3 of a kind 12. Straight 13. Flush 14. Full house 15. 4 of a kind 16. Straight flush 17. Royal Flush
Your main method should generate 10,000,000 random hands by making calls to drawhand and classify each hand by calling classifyHand. It should keep counts of the number of hands that fall into each category above. For each of the first 50 hands (out of the 10,000,000 hands) generated, your program should display the hand number (1-50), the five cards in the hand including the kind and suit of each card using the same notation used here, and the name of the (highest) appropriate rank of the hand from the 17 categories above. After the first 50 hands, display the hand count, hand, and hand rank only if the count of the hands so far for that category is ten or less.
Solution
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
int point[52];
int a[52];
int scoring(int a, int b, int c, int d, int e)
{
int R[5];
int S[5], x[5];
x[0]=a; x[1]=b; x[2]=c; x[3]=d; x[4]=e;
for (int o=0; o<5; o++)
{
R[o] = x[o]%13;
S[o] = x[o]/13;
}
// Sort the R array
bool swapped = false;
do
{
// 1 pass of the bubble sort
swapped = false;
for(int o=0; o<4; o++)
{
if(R[o] > R[o+1])
{
int temp = R[o];
R[o] = R[o+1];
R[o+1] = temp;
swapped = true;
}
}
}
while(swapped == true);
int scoring= 0;
// Check for a straight:
if(R[1]==R[0]+1 && R[2]==R[1]+1 && R[3]==R[2]+1 && R[4]==R[3]+1)
{
cout << \"You got a Straight\" << endl;
scoring=(scoring+19);
}
else
{
scoring=(scoring+0);
}
// Check first 5 cards for any pair
if(R[0] == R[1] || R[1]==R[2] || R[2]==R[3] || R[3]==R[4])
{
// check for twopair
if(R[0]==R[1]&&R[2]==R[3]||R[1]==R[2]&&R[3]==R[4]||R[0]==R[1]&&R[3]==R[4])
{
cout << \"You got a Twopair\" << endl;
scoring=(scoring+17);
}
// check for 3 of a kind
if(R[0] == R[1] == R[2] || R[1] == R[2] == R[3] || R[2] == R[3] == R[4])
{
cout << \"You got a 3 of a kind\" << endl;
scoring=(scoring+18);
}
// Check for 4 of a kind
if(R[0] == R[1] == R[2] == R[3] || R[1] == R[2] == R[3] == R[4])
{
cout << \"You got a 4 of a kind\" << endl;
scoring=(scoring+22);
}
else
{
cout << \"You got a Pair\" << endl;
scoring=(scoring+16);
}
}
else
{
scoring=(scoring+0);
}
// Check for a flush (all the same suit)
if(S[0] == S[1] && S[1]==S[2] && S[2]==S[3] && S[3]==S[4])
{
cout << \"You got a Flush\" << endl;
scoring=(scoring+20);
}
else
{
scoring=(scoring+0);
}
// Check for straight flush
if(S[0] == S[1] && S[1]==S[2] && S[2]==S[3] && S[3]==S[4])
{
if(R[1]==R[0]+1 && R[2]==R[1]+1 && R[3]==R[2]+1 && R[4]==R[3]+1)
{
cout << \"You got a Straight Flush\" << endl;
scoring=(scoring+23);
}
else
{
scoring=(scoring+0);
}
}
// check for royal flush
// Check for full house
if(R[0] == R[1] == R[2] && R[3] == R[4] || R[0] == R[1] && R[2] == R[3] == R[4])
{
cout << \"You got a Full House\" << endl;
scoring = (scoring + 21);
}
else
{
scoring=scoring+0;
}
//check for the highest score
}
int main()
{
srand(time(0));
int deck[52];
int i;
string suitnames[4]={\"spades\", \"hearts\", \"clubs\", \"diamonds\"};
string ranknames[13]={\"ace\", \"king\", \"queen\", \"jack\", \"10\", \"9\", \"8\", \"7\", \"6\", \"5\", \"4\", \"3\", \"2\"};
// create a new deck, with cards in order, but unique
for(i=0; i<52; i++)
{
deck[i] = i;
}
// shuffle the deck:
for(i=0; i<52; i++)
{
// generate a random index to swap with the card at index i.
int j = rand() % 52;
int temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
// print all the cards
for(i=0; i<52; i++)
{
int suitnumber = deck[i] / 13; // 0 - 3
int rank = deck[i] % 13;
cout << ranknames[rank] << \" of \" << suitnames[suitnumber]<< \"\ \";
}
cout << endl;
for(i=0; i<52; i++)
{
int temp = deck[i];
deck[i]=a[i];
a[i]=temp;
}
int f;
cout << \"your card :\"<< endl;
f=0;
while(f<20)
{
cout << f << \". \";
int suitnumber = a[f] / 13; // 0 - 3
int rank = a[f] % 13;
cout << ranknames[rank] << \" of \" << suitnames[suitnumber]<< \"\ \";
f=f+4;
}
int n;
cout << \"do you want to replace your card ? [type 1 for yes, 0 for no]\" << endl;
cin >> n;
switch (n)
{
case 1:
cout << \"which card do you want to replace?[insert the number]\";
int o;
cin >> o;
int suitnumber = a[o] / 13; // 0 - 3
int rank = a[o] % 13;
cout << \"you want to replace \" << ranknames[rank] << \" of \" << suitnames[suitnumber]<< \"\ \";
int temp = a[o];
a[o] = a[20];
a[20] = temp;
cout <<\"your card now:\" << endl;
o=0;
while(o<20)
{
int suitnumber = a[o] / 13; // 0 - 3
int rank = a[o] % 13;
cout << ranknames[rank] << \" of \" << suitnames[suitnumber]<< \"\ \";
o=o+4;
}
}
//sorty(a[0], a[4], a[8], a[12], a[16]);
cout << \"your score : \" << scoring(a[0], a[4], a[8], a[12], a[16]) << endl << endl;
//sorty(a[1], a[5], a[9], a[13], a[17]);
cout << \"score P2 :\"<< scoring(a[1], a[5], a[9], a[13], a[17]) << endl << endl;
//sorty(a[2], a[6], a[10], a[14], a[18]);
cout << \"score P3 :\"<< scoring(a[2], a[6], a[10], a[14], a[18]) << endl << endl;
//sorty(a[3], a[7], a[11], a[15], a[19]);
cout << \"score P4 :\"<< scoring(a[3], a[7], a[11], a[15], a[19]) << endl << endl;
return 0;
}






