Page 1 of 1

Can you please help me finish this code, last functions like flush and straight and full house is where I need help #inc

Posted: Fri Jul 08, 2022 6:16 am
by answerhappygod
Can you please help me finish this code, last functions likeflush and straight and full house is where I need help
#include <conio.h> // needed for _getch() call atend of main()#include <stdio.h>#include <stdlib.h>#include <time.h>
#define SUITS 4#define FACES 13#define CARDS 52#define HAND_SIZE 5#define HANDS_IN_DECK 10#define NUMBER_OF_HANDS_PLAYED 10000000 //Currently set to 10million#define NUMB_HANDS_STR "10 million" //Currently set to 10million#define TRUE 1#define FALSE 0
// prototypes of functions suppliedvoid deal(const unsigned int wDeck[], const char* wFace[],//display all cards in deck const char* wSuit[]);void dealNextHand(unsigned int wDeck[], unsigned int hand[]);//deal out next hand from the deckint isFourOfAKind(const unsigned int hand[]); // return trueif hand contains four of a kind and false otherwise
// prototypes of functions you must implementvoid swap(unsigned int* const, unsigned int* const); //swap the twocards pointed to by the 2 pointersvoid shuffle(unsigned int wDeck[]); //shuffle deckint isPair(const unsigned int hand[]); // return true if handcontains a pair and false otherwiseint isTwoPair(const unsigned int hand[]); // return true ifhand contains a two pair and false otherwiseint isThreeOfAKind(const unsigned int hand[]); // return trueif hand contains three of a kind and false otherwiseint isStraight(const unsigned int hand[]); // return true ifhand is a straight and false otherwiseint isFlush(const unsigned int hand[]); // return true ifhand is a flush and false otherwiseint isFullHouse(const unsigned int hand[]); // return true ifhand is a full house and false otherwise
int main(void){ // define and initialize deck array unsigned int deck[CARDS];
// initialize deck with values 0 to CARDS-1 // value / 13 caluclates suit # {"Hearts", "Diamonds", "Clubs", "Spades" }; // value % 13 calculates face card {Ace, 2, 3,... 10, Jack, Queen, King} for (size_t card = 0; card < CARDS; ++card) { deck[card] = card; }
srand((unsigned int)time(NULL)); // seedrandom-number generator
// initialize suit array const char* suit[SUITS] = { "Hearts", "Diamonds", "Clubs", "Spades" };
// initialize face array const char* face[FACES] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
/* shuffle(deck); // uncomment after completingthe implementation // of the swap() and shuffle() functions */ deal(deck, face, suit); // display the deckunshuffled
unsigned int hand[HAND_SIZE]; // will contain thecards in the hand.
//Define and initialize variables used to counteach type of hand unsigned int pairCount = 0; unsigned int twoPairCount = 0; unsigned int threeOfAKindCount = 0; unsigned int straightCount = 0; unsigned int flushCount = 0; unsigned int fullHouseCount = 0; unsigned int fourOfAKindCount = 0; unsigned int straightFlushCount = 0; //NOTE: Thiscount includes both straight flushes and royal flushes.
// Shuffle the deck for the first time // After this, we shuffle deck every time we do nothave enough undealth cards // for a complete hand which will be every 10 dealsassuming five card hands shuffle(deck);
// Deal out NUMBER_OF_HANDS_PLAYED hands for (size_t hands = 1; hands <NUMBER_OF_HANDS_PLAYED; ++hands) { dealNextHand(deck, hand); // Deal outnext 5 cards from the deck into the array hand
// Does hand have a pair? if (isPair(hand)) { ++pairCount; //Yes, increment pair count }
// Does hand have two pair? if (isTwoPair(hand)) { ++twoPairCount; }
// Does hand have three of akind? if (isThreeOfAKind(hand)) { ++threeOfAKindCount; }
// Does hand have a straight? if (isStraight(hand)) { // Check if also aflush if (isFlush(hand)) { ++straightFlushCount; //both straight and flush } else { ++straightCount; // nope, just a straight } }
// Does hand have a flush? else if (isFlush(hand)) //not astraight, how about a flush? { ++flushCount; }
// Does hand have a fullhouse? if (isFullHouse(hand)) { ++fullHouseCount; }
// Does hand have four of akind? if (isFourOfAKind(hand)) { ++fourOfAKindCount; } }
printf("\nA pair occurred %d times in"NUMB_HANDS_STR" hands, which means the probability of a pair is%.2f %c", pairCount, pairCount * 100.0 / NUMBER_OF_HANDS_PLAYED,'%'); printf("\nTwo pair occurred %d times in"NUMB_HANDS_STR" hands, which means the probability of two pair is%.2f %c", twoPairCount, twoPairCount * 100.0 /NUMBER_OF_HANDS_PLAYED, '%'); printf("\nThree of a kind occurred %d times in"NUMB_HANDS_STR" hands, which means the probability of three of akind is %.2f %c", threeOfAKindCount, threeOfAKindCount * 100.0 /NUMBER_OF_HANDS_PLAYED, '%'); printf("\nA straight occurred %d times in"NUMB_HANDS_STR" hands, which means the probability of a straightis %.3f %c", straightCount, straightCount * 100.0 /NUMBER_OF_HANDS_PLAYED, '%'); printf("\nA flush occurred %d times in"NUMB_HANDS_STR" hands, which means the probability of a flush is%.3f %c", flushCount, flushCount * 100.0 / NUMBER_OF_HANDS_PLAYED,'%'); printf("\nA full house occurred %d times in"NUMB_HANDS_STR" hands, which means the probability of a full houseis %.4f %c", fullHouseCount, fullHouseCount * 100.0 /NUMBER_OF_HANDS_PLAYED, '%'); printf("\nFour of a kind occurred %d times in"NUMB_HANDS_STR" hands, which means the probability of four of akind is %.4f %c", fourOfAKindCount, fourOfAKindCount * 100.0 /NUMBER_OF_HANDS_PLAYED, '%'); printf("\nStraight flush occurred %d times in"NUMB_HANDS_STR" hands, which means the probability of a straightflush is %.5f %c", straightFlushCount, straightFlushCount * 100.0 /NUMBER_OF_HANDS_PLAYED, '%'); puts("");
}
// Display all of the cards in the deckvoid deal(const unsigned int wDeck[], const char* wFace[], const char* wSuit[]){ // deal each of the cards for (size_t card = 0; card < CARDS; ++card){ size_t suit = wDeck[card] /FACES; size_t face = wDeck[card] %FACES; printf("%5s of %-8s%c", wFace[face],wSuit[suit], card % 4 == 3 ? '\n' :'\t'); // 4-column format }}
// Deal out the next HAND_SIZE cards into the array hand[]// If not enough undealt cards left in wDeck, shuffle wDeckand // start dealing from beginning of the wDeckagain.void dealNextHand(unsigned int wDeck[], unsigned int hand[]){ static unsigned int currentCard = 0; //rememberswhich is the next card to be dealt.
// Shuffle deck and start from beginning of thedeck if not enough cards left for a whole hand if ((currentCard + HAND_SIZE) >= CARDS) { shuffle(wDeck); currentCard = 0; }
// Deal out HAND_SIZE cards from wDeck into thehand array for (size_t card = 0; card < HAND_SIZE;++card) { hand[card] =wDeck[currentCard++]; }}
/*
Below is the complete implementation of the isFourOfAKind()function.
You should be able to use a variation of this function tocompute two of a kind, three of a kind and full house.
The straight, flush and royal flush(optional) calculations willrequire different logic.
After you have completed the swap() and shuffle() functions anduncommented the call to shuffle(),this start code should run without errors and the your result for 4of a kind should be very close tomy results in my sample output. Then you will be ready totackle completing the other function stubs.
*/
// Returns true if hand contains four of kind and falseotherwiseint isFourOfAKind(const unsigned int hand[]){ unsigned int faceCnt[FACES] = { 0 };
// Calculate the frequency of each face card inthe hand // In other words, it calculates how many Aces,deuces, 3's, etc. in the hand of 5 cards for (size_t card = 0; card < HAND_SIZE;++card) { ++faceCnt[hand[card] % FACES]; }
// Iterate through all of the frequency counts tofind out if // any face card occurred 4 times inthe hand for (size_t face = 0; face < FACES; ++face) { if (faceCnt[face] == 4) { return TRUE; // Yes!!!Return true. } }
return FALSE; //Nope ;-(}
// Student implements the 8 functions below
// Swap the two unsigned ints pointed to by the pointers card1and card2void swap(unsigned int* const card1, unsigned int* constcard2){ // Student implements this function}
// Shuffle cards in deck// Must invoke swap() function abovevoid shuffle(unsigned int wDeck[]){ // Student implements this function}
// Returns true if hand contains only a pair and false otherwise// NOTE: Will return FALSE if the hand contains any of these handtypes as well:// 1) Hand has 2 pairs// 2) Hand has 3 of a kind// 2) Hand is a full house// 3) Hand has 4 of a kindint isPair(const unsigned int hand[]){ // Student implements this function return FALSE;}
// Returns true if hand contains two pairs and falseotherwise// NOTE: Will return FALSE if the hand contains any of these handtypes as well:// 1) Hand has 3 of a kind// 2) Hand is a full house// 3) Hand has 4 of a kindint isTwoPair(const unsigned int hand[]){ // Student implements this function return FALSE;}
// Returns true if hand contains 3 of a kind and falseotherwise// NOTE: Will return FALSE if the hand contains any of these handtypes as well:// 1) Hand has a full house// 2) Hand has 4 of a kindint isThreeOfAKind(const unsigned int hand[]){ // Student implements this function return FALSE;}
// Returns true if hand is a straight and false otherwise// This function does NOT check if also a flush. That is done inmain()
// VERY IMPORTANT: The Ace can be used to create a straight in 2different ways:// Ace, 2, 3, 4, 5 and // 10, Jack, Queen, King, Ace// You need to check for both uses of the Ace to create astraightint isStraight(const unsigned int hand[]){ // Student implements this function return FALSE;}
// Returns true if hand is a flush and false otherwiseint isFlush(const unsigned int hand[]){ // Student implements this function return FALSE;}
// return true if hand is a full house and false otherwiseint isFullHouse(const unsigned int hand[]){ // Student implements this function return FALSE;}