Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming In this assignment we will work on the creation of a poker game. At a minimum your game must deal 2 hands of

C Programming

In this assignment we will work on the creation of a poker game. At a minimum your game must deal 2 hands of cards, and print out what poker hand each has. It must also determine which hand has won the game but breaking a tie will be for bonus points. For example, if both hands have 3 of a kind, that can be considered a tie. If you want the bonus points write some code that determines which 3 of a kind is higher.

Here is the output from dealing a hand of cards:

Ace of Hearts

Ten of Clubs

Ace of Clubs

Eight of Hearts

Seven of Diamonds

2 2 1 0 <>

0 0 0 0 0 1 1 0 1 0 0 0 2

You have a PAIR!

Use lots of small functions to help reduce the complexity of the code.

You must use the code from the video to get started, and you must use the following representation for a poker hand:

make 2 arrays: suitsInHand[4], and facesInHand[14].

suitsInHand is 4 counters that represents how many hearts, clubs, diamonds, spades are in the hand. These 4 counters must add up to 5 for a hand of 5 cards. For example if you have 5 hearts in the hand of cards, the array would have the values 5,0,0,0

facesInHand is 13 counters, that represent how many twos, threes, etc. you have in the hand. This must also total 5. For example, if you have a pair of 3s, and three Kingss, this array would have the values 0,2,0,0,0,0,0,0,0,0,3,0

While dealing a hand of cards, keep a count of the suitsInHand, and facesInHand.

I will include some code below that will help you to figure out what poker hand you have dealt.

This function will use the 2 arrays described above to determine if the hand has a flush, straight, etc. I have not included the parameters. You will need to pass in the hand of cards, and have the function pass back if there is a flush, straight, three of a kind, etc.

/*

analyzeHand: Determines whether the hand contains a

straight, a flush, four-of-a-kind,

and/or a three-of-a-kind; determines the

number of pairs; stores the results into

the external variables straight, flush,

four, three, and pairs.

*/

void analyzeHand(

{

int num_consec = 0;

int rank, suit;

straight = FALSE;

flush = 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

for (; rank < FACES && facesInHand[rank]; rank++)

num_consec++;

if (num_consec == 5) {

straight = TRUE;

return;

}

/* check for 4-of-a-kind, 3-of-a-kind, and pairs */

for (rank = 0; rank < NUM_RANKS; rank++) {

if (facesInHand[rank] == 4)

four = TRUE;

if (facesInHand[rank] == 3)

three = TRUE;

if (facesInHand[rank] == 2)

pairs++;

}

}

Here is a block of code that might help you determine what hand is better than another. These are in order. A straight-flush is the best hand, and a high-card is the worst hand.

if (straight && flush)

printf("Straight flush ");

else if (four)

printf("Four of a kind ");

else if (three && pairs == 1)

printf("Full house ");

else if (flush)

printf("Flush ");

else if (straight)

printf("Straight ");

else if (three)

printf("Three of a kind ");

else if (pairs == 2)

printf("Two pairs ");

else if (pairs == 1)

printf("Pair ");

else

printf("High card ");

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions