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

Program in C++ the following: Find the probabilities of each type of hand in fiv

ID: 3859749 • Letter: P

Question

Program in C++ the following:

Find the probabilities of each type of hand in five-card poker (see below) and rank the types of hands by their probability

A SINGLE PAIR

This the hand with the pattern AABCD, where A, B, C and D are from the distinct "kinds" of cards: aces, twos, threes, tens, jacks, queens, and kings (there are 13 kinds, and four of each kind, in the standard 52 card deck). The number of such hands is (13-choose-1)*(4-choose-2)*(12-choose-3)*[(4-choose-1)]^3. If all hands are equally likely, the probability of a single pair is obtained by dividing by (52-choose-5). This probability is 0.422569.

TWO PAIR

This hand has the pattern AABBC where A, B, and C are from distinct kinds. The number of such hands is (13-choose-2)(4-choose-2)(4-choose-2)(11-choose-1)(4-choose-1). After dividing by (52-choose-5), the probability is 0.047539.

A TRIPLE

This hand has the pattern AAABC where A, B, and C are from distinct kinds. The number of such hands is (13-choose-1)(4-choose-3)(12-choose-2)[4-choose-1]^2. The probability is 0.021128.

A FULL HOUSE

This hand has the pattern AAABB where A and B are from distinct kinds. The number of such hands is (13-choose-1)(4-choose-3)(12-choose-1)(4-choose-2). The probability is 0.001441.

FOUR OF A KIND

This hand has the pattern AAAAB where A and B are from distinct kinds. The number of such hands is (13-choose-1)(4-choose-4)(12-choose-1)(4-choose-1). The probability is 0.000240.

A STRAIGHT

This is five cards in a sequence (e.g., 4,5,6,7,8), with aces allowed to be either 1 or 13 (low or high) and with the cards allowed to be of the same suit (e.g., all hearts) or from some different suits. The number of such hands is 10*[4-choose-1]^5. The probability is 0.003940. IF YOU MEAN TO EXCLUDE STRAIGHT FLUSHES AND ROYAL FLUSHES (SEE BELOW), the number of such hands is 10*[4-choose-1]^5 - 36 - 4 = 10200, with probability 0.00392465

A FLUSH

Here all 5 cards are from the same suit (they may also be a straight). The number of such hands is (4-choose-1)* (13-choose-5). The probability is approximately 0.00198079. IF YOU MEAN TO EXCLUDE STRAIGHT FLUSHES, SUBTRACT 4*10 (SEE THE NEXT TYPE OF HAND): the number of hands would then be (4-choose-1)*(13-choose-5)-4*10, with probability approximately 0.0019654.

A STRAIGHT FLUSH

All 5 cards are from the same suit and they form a straight (they may also be a royal flush). The number of such hands is 4*10, and the probability is 0.0000153908. IF YOU MEAN TO EXCLUDE ROYAL FLUSHES, SUBTRACT 4 (SEE THE NEXT TYPE OF HAND): the number of hands would then be 4*10-4 = 36, with probability approximately 0.0000138517.

A ROYAL FLUSH

This consists of the ten, jack, queen, king, and ace of one suit. There are four such hands. The probability is 0.00000153908.

NONE OF THE ABOVE

We have to choose 5 distinct kinds (13-choose-5) but exclude any straights (subtract 10). We can have any pattern of suits except the 4 patterns where all 5 cards have the same suit: 4^5-4. The total number of such hands is [(13-choose-5)-10]* (4^5-4). The probability is 0.501177.

Explanation / Answer

#include<stdio.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>

using namespace std;

int handType[10] = {0};
// 0-> A SINGLE PAIR
// 1-> TWO PAIR
// 2-> A TRIPLE
// 3-> A FULL HOUSE
// 4-> FOUR OF A KIND
// 5-> A STRAIGHT
// 6-> A FLUSH
// 7-> A STRAIGHT FLUSH
// 8-> A ROYAL FLUSH
// 9-> NONE


// struct of a hand
struct hand{
bool straight;
bool flush;
bool four;
bool three;
int pairs;
int suitsInHand[4];
int facesInHand[13];
};

//function to print hand arrays
void print_hand(struct hand *H){
int i;
for(i=0;i<4;i++){
  printf("%d ",H->suitsInHand[i]);
}
printf("here ");
for(i=0;i<13;i++){
  printf("%d ",H->facesInHand[i]);
}
printf("here1 ");
}

// function to analyze a hand
void analyzeHand(int suitsInHand[], int facesInHand[], int SUITS, int FACES, bool straight, bool flush, bool royalflush, bool four, bool three, int pairs)
{
int num_consec = 0;
int rank, suit;

straight = false;
flush = false;
royalflush = false;
four = false;
three = false;
pairs = 0;

// check for flush – 5 cards of the same suit
for (suit = 0; suit < SUITS; suit++)
  if (suitsInHand[suit] == 5)
   flush = true;

// check for straight – eg. One each of 5,6,7,8,9
// locate the first card
rank = 0;
while (facesInHand[rank] == 0)
  rank++;

//count the consecutive non-zero faces
int royal = -1;
for (; rank < FACES && facesInHand[rank]; rank++){
  if(royal==-1){
   royal = rank;
  }
  num_consec++;
}

if (num_consec == 5) {
  if(royal == 8 && flush){
   royalflush = true;
  }
  straight = true;
  return;
}

/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
for (rank = 0; rank < FACES; rank++) {
  if (facesInHand[rank] == 4)
   four = true;
  if (facesInHand[rank] == 3)
   three = true;
  if (facesInHand[rank] == 2)
   pairs++;
}

}


// Java program to print all generate all possible hands of length k


// The method that generate all possible hands of length k. It is
// mainly a wrapper over recursive function generate_All_Possible_Hands
void generate_All_Possible_Hands(char cards[], int k, int n) {
int suits[4] = {0};
int faces[13] = {0};
int total = 0;
generate_All_Possible_Hands(cards, "", n, 5, 0, suits, faces, total);
}

// The main recursive method to generate all possible hands of length k
void generate_All_Possible_Hands(char cards[], string prefix, int n, int k, int start, int suits[], int faces[], int total) {
if (k == 0 ) {
  bool straight = false;
  bool flush = false;
  bool four = false;
  bool three = false;
  bool royalflush= false;
  int pairs = 0;
  analyzeHand(suits, faces, 4, 13, straight, flush, royalflush, four, three, pairs);
  if(pairs == 1){
   handType[0]++;
  }
  if(pairs == 2){
   handType[1]++;
  }
  if(three){
   handType[2]++;
  }
  if(three && pairs==1){
   handType[3]++;
  }
  if(four){
   handType[4]++;
  }
  if(straight){
   handType[5]++;
  }
  if(flush){
   handType[6]++;
  }
  if(straight && flush){
   handType[7]++;
  }
  if(royalflush){
   handType[8]++;
  }
  if(pairs==0 && !flush && !straight && !royalflush && !three && !four){
   handType[9]++;
  }
  total++;
  return;
}

if (start >= n ) {
  return;
}
// does not include card i
generate_All_Possible_Hands(cards, prefix, n, k, start+1, suits, faces, total);
// include a card i, k is decreased, because we have added a new card
suits[start%13] += 1;
faces[start/13] += 1;
generate_All_Possible_Hands(cards, prefix+cards[start], n, k - 1, start+1, suits, faces, total);
suits[start%13] -= 1;
faces[start/13] -= 1;
}


int main() {
srand(time(0));
int SUITS = 4;
int FACES = 13;

struct hand hand1;
struct hand hand2;
//populate first hand
populate_hand(&hand1);
// print_hand(hand1);
//populate second hand
populate_hand(&hand2);

//printing arrays of a hand
//   print_hand(hand1);
//   print_hand(hand2);

int ans = compare_hands(&hand1,&hand2);
if(ans==1){
  printf("hand1 won!");
}
else{
  printf("hand2 won!");
}

}

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote