Assignment 5 C Programming In this assignment we will work on the creation of a
ID: 3693766 • Letter: A
Question
Assignment 5 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 <<These are explained below>>
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 two’s, three’s, etc. you have in the hand. This must also total 5. For example, if you have a pair of 3’s, and three Kings’s, 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 ");
Deliverables:
1. Paste your code below, in this document.
2. Include the output of running the program 10 times. Include several different cases, for example 2 pair, High Card. Each case that you expect points for will be demonstrated in the output.
Explanation / Answer
/* Classifies a poker hand */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5
/* external variables */
int num_in_rank[NUM_RANKS];
int num_in_suit[NUM_SUITS];
bool straight, flush, four, three;
bool straight1, flush1, four1, three1;
int pairs; /* can be 0, 1, or 2 */
int pairs1;
/**********************************************************
* main *
**********************************************************/
int main(void)
{
/**********************************************************
* read_cards: Reads the cards into the external *
* variables num_in_rank and num_in_suit; *
* checks for bad cards and duplicate cards. *
**********************************************************/
bool card_exists[NUM_RANKS][NUM_SUITS];
char ch, rank_ch, suit_ch;
int rank, rank1, suit;
bool bad_card;
int cards_read = 0;
for (rank = 0; rank < NUM_RANKS; rank++) {
num_in_rank[rank] = 0;
for (suit = 0; suit < NUM_SUITS; suit++)
card_exists[rank][suit] = false;
}
char inHand[2][5];
int card;
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5
/* external variables */
int num_in_rank[NUM_RANKS];
int num_in_suit[NUM_SUITS];
bool straight, flush, four, three;
int pairs; /* can be 0, 1, or 2 */
/**********************************************************
* main *
**********************************************************/
int main(void)
{
/**********************************************************
* read_cards: Reads the cards into the external *
* variables num_in_rank and num_in_suit; *
* checks for bad cards and duplicate cards. *
**********************************************************/
bool card_exists[NUM_RANKS][NUM_SUITS];
char ch, rank_ch, suit_ch;
int rank, suit;
bool bad_card;
int cards_read = 0;
for (rank = 0; rank < NUM_RANKS; rank++) {
num_in_rank[rank] = 0;
for (suit = 0; suit < NUM_SUITS; suit++)
card_exists[rank][suit] = false;
}
char inHand[2][5];
int card;
int handvalue( type hand[5] );
while (cards_read < NUM_CARDS) {
bad_card = false;
printf("Enter player1 and player2's cards, respectively ");
printf("(Example: '23456(hit tab here)45678') : ");
for(card=0;card<5;card++) //player1
scanf("%c", &inHand[0][card]);
for(card=0;card<5;card++) //player2
scanf(" %c", &inHand[1][card]);
for(card=0;card<5;card++)
{
switch (inHand[0][card]) {
case '0': exit(EXIT_SUCCESS);
case '2': rank = 0; break;
case '3': rank = 1; break;
case '4': rank = 2; break;
case '5': rank = 3; break;
case '6': rank = 4; break;
case '7': rank = 5; break;
case '8': rank = 6; break;
case '9': rank = 7; break;
case 't': case 'T': rank = 8; break;
case 'j': case 'J': rank = 9; break;
case 'q': case 'Q': rank = 10; break;
case 'k': case 'K': rank = 11; break;
case 'a': case 'A': rank = 12; break;
default: bad_card = true;
}
switch (inHand[1][card]) {
case '0': exit(EXIT_SUCCESS);
case '2': rank = 0; break;
case '3': rank = 1; break;
case '4': rank = 2; break;
case '5': rank = 3; break;
case '6': rank = 4; break;
case '7': rank = 5; break;
case '8': rank = 6; break;
case '9': rank = 7; break;
case 't': case 'T': rank = 8; break;
case 'j': case 'J': rank = 9; break;
case 'q': case 'Q': rank = 10; break;
case 'k': case 'K': rank = 11; break;
case 'a': case 'A': rank = 12; break;
default: bad_card = true;
}
if (bad_card)
printf("Bad card; ignored. ");
else {
num_in_rank[rank]++;
num_in_suit[suit]++;
card_exists[rank][suit] = true;
cards_read++;
}
}
}
//**********************************************************
//******************************************************************************
//Printout Cards
//******************************************************************************
for(card=0; card<5; card++)
{
printf("%c", inHand[0][card]);
}
printf(" ");
for(card=0; card<5; card++)
{
printf("%c", inHand[1][card]);
}
//**********************************************************
//**********************************************************
// analyze_hand: *
// **********************************************************/
{
int num_consec = 0;
bool straights = false;
flush = false;
four = false;
three = false;
pairs = 0;
/* check for straight */
rank = 0;
while (num_in_rank[rank] == 0) rank++;
for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
num_consec++;
if (num_consec == NUM_CARDS) {
straight = true;
}
/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
for (rank = 0; rank < NUM_RANKS; rank++) {
if (num_in_rank[rank] == 4) four = true;
if (num_in_rank[rank] == 3) three = true;
if (num_in_rank[rank] == 2) pairs++;
}
}
/**********************************************************
* print_result *
**********************************************************/
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");
getch();
printf(" ");
}
while (cards_read < NUM_CARDS) {
bad_card = false;
printf("Enter player1 and player2's cards, respectively ");
printf("(Example: '23456(hit tab here)45678') : ");
for(card=0;card<5;card++) //player1
scanf("%c", &inHand[0][card]);
for(card=0;card<5;card++) //player2
scanf(" %c", &inHand[1][card]);
for(card=0;card<5;card++)
{
switch (inHand[0][card]) {
case '0': exit(EXIT_SUCCESS);
case '2': rank = 0; break;
case '3': rank = 1; break;
case '4': rank = 2; break;
case '5': rank = 3; break;
case '6': rank = 4; break;
case '7': rank = 5; break;
case '8': rank = 6; break;
case '9': rank = 7; break;
case 't': case 'T': rank = 8; break;
case 'j': case 'J': rank = 9; break;
case 'q': case 'Q': rank = 10; break;
case 'k': case 'K': rank = 11; break;
case 'a': case 'A': rank = 12; break;
default: bad_card = true;
}
switch (inHand[1][card]) {
case '0': exit(EXIT_SUCCESS);
case '2': rank1 = 0; break;
case '3': rank1 = 1; break;
case '4': rank1 = 2; break;
case '5': rank1 = 3; break;
case '6': rank1 = 4; break;
case '7': rank1 = 5; break;
case '8': rank1 = 6; break;
case '9': rank1 = 7; break;
case 't': case 'T': rank1 = 8; break;
case 'j': case 'J': rank1 = 9; break;
case 'q': case 'Q': rank1 = 10; break;
case 'k': case 'K': rank1 = 11; break;
case 'a': case 'A': rank1 = 12; break;
default: bad_card = true;
}
if (bad_card)
printf("Bad card; ignored. ");
else {
num_in_rank[rank]++;
num_in_rank[rank1]++;
num_in_suit[suit]++;
card_exists[rank][suit] = true;
card_exists[rank1][suit] = true;
cards_read++;
}
}
}
//**********************************************************
//******************************************************************************
//Printout Cards
//******************************************************************************
for(card=0; card<5; card++)
{
printf("%c", inHand[0][card]);
}
printf(" ");
for(card=0; card<5; card++)
{
printf("%c", inHand[1][card]);
}
//**********************************************************
//**********************************************************
// analyze_hand: player 1 *
// **********************************************************/
{
int num_consec = 0;
bool straights = false;
flush = false;
four = false;
three = false;
pairs = 0;
/* check for straight */
rank = 0;
while (num_in_rank[rank] == 0) rank++;
for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
num_consec++;
if (num_consec == NUM_CARDS) {
straight = true;
}
/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
for (rank = 0; rank < NUM_RANKS; rank++) {
if (num_in_rank[rank] == 4) four = true;
if (num_in_rank[rank] == 3) three = true;
if (num_in_rank[rank] == 2) pairs++;
}
}
//**********************************************************
// analyze_hand: player 2 *
// **********************************************************/
{
int num_consec = 0;
bool straights1 = false;
bool flush1 = false;
bool four1 = false;
bool three1 = false;
int pairs1 = 0;
/* check for straight */
rank1 = 0;
while (num_in_rank[rank1] == 0) rank1++;
for (; rank1 < NUM_RANKS && num_in_rank[rank1] > 0; rank1++)
num_consec++;
if (num_consec == NUM_CARDS) {
straight1 = true;
}
/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
for (rank1 = 0; rank1 < NUM_RANKS; rank1++) {
if (num_in_rank[rank1] == 4) four1 = true;
if (num_in_rank[rank1] == 3) three1 = true;
if (num_in_rank[rank1] == 2) pairs1++;
}
}
/**********************************************************
* print_result 1 *
**********************************************************/
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");
/**********************************************************
* print_result 2 *
**********************************************************/
if (four1) printf(" Four of a kind");
else if (three1 &&
pairs1 == 1) printf(" Full house");
else if (flush1) printf("Flush");
else if (straight1) printf(" Straight");
else if (three1) printf(" Three of a kind");
else if (pairs1 == 2) printf(" Two pairs");
else if (pairs1 == 1) printf(" Pair");
else printf(" High card");
getch();
printf(" ");
}
int handvalue( type hand[5] )
{
if hand is a straight flush
return highest value
if hand is four of a kind
return one less than what a straight flush returns
... repeat for rest of hands
return highest card from hand
}
...
for( x = 0; x < PLAYERS; x++ )
hval[ x ] = handvalue( inHand[ x ] );
winner = highestvaluein( hval );
NOTE: cant take scrrenshots of output. do necessary changes wherever required. I have not used your code