I am doing question 7.13 which takes the program from 7.12 and now deal another
ID: 3827697 • Letter: I
Question
I am doing question 7.13 which takes the program from 7.12 and now deal another hand of five cards and "compare them". I can post my code from 7.12 but I am stuck on how to deal an extra and and compare and evaluate them. Also, is there a way I can change my current code to make the output more understandable and organized? Thank you!
My code is:
// C program header files being used
#include <stdio.h>
#include <time.h>
// Function prototypes and their returning variables
void shuffle (int deck[][13]);
void deal (int deck[][13], int hand[][2], char *suit[], char *face[]);
void pair (int hand[][2], char *suit[], char *face[]);
void threeOfAKind (int hand[][2], char *suit[], char *face[]);
void fourOfAKind (int hand[][2], char *suit[], char *face[]);
void straightHand (int hand[][2], char *suit[], char *face[]);
void flushHand (int hand[][2], char *suit[], char *face[]);
// Fuction main starts off any C program
int main (void)
{
// Declaring suits in a deck of cards
char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
// Declaring faces in a deck of cards
char *face[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int deck[4][13]; // represents deck of cards
int hand[5][2]; // represents user's hand
int row, col;
for (row = 0; row <= 3; row++)
{
for (col = 0; col <= 12; col++)
{
deck[row][col] = 0;
} // end of for loop
} // end of for loop
srand (time(NULL));
shuffle (deck); // calling function to shuffle the deck of cards
deal (deck, hand, suit, face); // function to deal the 5-card hands
// fuctions used to find the poker value of the user's hand
pair (hand, suit, face);
threeOfAKind (hand, suit, face);
fourOfAKind (hand, suit, face);
straightHand (hand, suit, face);
flushHand (hand, suit, face);
getchar();
} // end of int main
// Function to shuffle the deck of cards
void shuffle (int deck[][13])
{
int card, row, col;
for (card = 1; card <= 52; card++)
{
do
{
row = rand() % 4;
col = rand() % 13;
} while (deck[row][col] != 0);
// place the card number in a randomly selected slot
deck[row][col] = card;
} // end of for loop
} // end of function shuffle
// Fuction to deal the poker hand
void deal (int deck[][13], int hand[][2], char *suit[], char *face[])
{
// Variables needed to track the hand
int r = 0, card, row, col;
printf("The hand is: ");
// Distribute the cards
for (card = 1; card < 6; card++)
{
for (row = 0; row <= 3; row++)
{
for (col = 0; col <= 12; col++)
{
if (deck[row][col] == card)
{
hand[r][0] = row;
hand[r][1] = col;
printf("%5s of %-8s", face[col], suit[row]);
++r;
} // end of if statement
} // end of for loop
} // end of for loop
printf(" ");
} // end of for loop
} // end of function deal
// Fuction to find pairs
void pair(int hand[][2], char *suit[], char *face[])
{
// Record the number of cards and their hands
int count[13] = { 0 };
int r, pair;
for (r = 0; r < 5; r++)
{
count[hand[r][1]]++;
}
// If a pair exists, then output it
for (pair = 0; pair < 13; pair++)
{
if (count[pair] == 2)
{
printf("The hand contains a pair of %ss.", face[pair]);
} // end of if statement
} // end of for loop
} // end of function pair
// Fuction to find three of a kind
void threeOfAKind(int hand[][2], char *suit[], char *face[])
{
// Record the number of cards and their hands
int count[13] = { 0 };
int r, three;
for (r = 0; r < 5; r++)
{
count[hand[r][1]]++;
}
// If a three of a kind exists, then output it
for (three = 0; three < 13; three++)
{
if (count[three] == 3)
{
printf("The hand contains a three of a kind of %ss.", face[three]);
} // end of if statement
} // end of for loop
} // end of function threeOfAKind
// Fuction to find four of a kind
void fourOfAKind(int hand[][2], char *suit[], char *face[])
{
// Record the number of cards and their hands
int count[13] = { 0 };
int r, four;
for (r = 0; r < 5; r++)
{
count[hand[r][1]]++;
}
// If four of a kind exists, then output it
for (four = 0; four < 13; four++)
{
if (count[four] == 4)
{
printf("The hand contains four of a kind of %ss.", face[four]);
} // end of if statement
} // end of for loop
} // end of function fourOfAKind
// Function to find a straight hand
void straightHand(int hand[][2], char *suit[], char *face[])
{
int sH[5] = { 0 };
int temp, r, pass, comp, j;
// Copy the column location for bubble sorting
for (r = 0; r < 5; r++)
{
sH[r] = hand[r][1];
} // end of for loop
// Use bubble sorting for column locations
for (pass = 1; pass < 5; pass++)
{
for (comp = 0; comp < 4; comp++)
{
if (sH[comp] > sH[comp + 1])
{
temp = sH[comp];
sH[comp] = sH[comp + 1];
sH[comp + 1] = temp;
} // end of if statement
} // end of for loop
} // end of for loop
// Making sure sorted columns are straight
// after bubble sorted
if (sH[4] - 1 == sH[3] && sH[3] - 1 == sH[2] && sH[2] - 1 == sH[1] && sH[1] - 1 == sH[0])
{
printf("The hand contains a straight from %s to %s.", face[sH[0]], face[sH[4]]);
} // end of if statement
} // end of function straightHand
// Fuction to find a flush in the hand
void flushHand(int hand[][2], char *suit[], char *face[])
{
// Record the number of cards and their hands
int count[4] = { 0 };
int r, flush;
for (r = 0; r < 5; r++)
{
count[hand[r][0]]++;
}
// If flush exists, then output it
for (flush = 0; flush < 4; flush++)
{
if (count[flush] == 5)
{
printf("The hand contains flush of %ss.", suit[flush]);
} // end of if statement
} // end of for loop
} // end of function flushHand
Explanation / Answer
#include <stdio.h>
#include <time.h>
// Function prototypes and their returning variables
void shuffle (int deck[][13]);
void deal (int deck[][13], int hand[][2], char *suit[], char *face[]);
void pair (int hand[][2], char *suit[], char *face[]);
void threeOfAKind (int hand[][2], char *suit[], char *face[]);
void fourOfAKind (int hand[][2], char *suit[], char *face[]);
void straightHand (int hand[][2], char *suit[], char *face[]);
void flushHand (int hand[][2], char *suit[], char *face[]);
// Fuction main starts off any C program
int main (void)
{
// Declaring suits in a deck of cards
char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
// Declaring faces in a deck of cards
char *face[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int deck[4][13]; // represents deck of cards
int hand[5][2]; // represents user's hand
int row, col;
for (row = 0; row <= 3; row++)
{
for (col = 0; col <= 12; col++)
{
deck[row][col] = 0;
} // end of for loop
} // end of for loop
srand (time(NULL));
shuffle (deck); // calling function to shuffle the deck of cards
deal (deck, hand, suit, face); // function to deal the 5-card hands
// fuctions used to find the poker value of the user's hand
pair (hand, suit, face);
threeOfAKind (hand, suit, face);
fourOfAKind (hand, suit, face);
straightHand (hand, suit, face);
flushHand (hand, suit, face);
getchar();
} // end of int main
// Function to shuffle the deck of cards
void shuffle (int deck[][13])
{
int card, row, col;
for (card = 1; card <= 52; card++)
{
do
{
row = rand() % 4;
col = rand() % 13;
} while (deck[row][col] != 0);
// place the card number in a randomly selected slot
deck[row][col] = card;
} // end of for loop
} // end of function shuffle
// Fuction to deal the poker hand
void deal (int deck[][13], int hand[][2], char *suit[], char *face[])
{
// Variables needed to track the hand
int r = 0, card, row, col;
printf("The hand is: ");
// Distribute the cards
for (card = 1; card < 6; card++)
{
for (row = 0; row <= 3; row++)
{
for (col = 0; col <= 12; col++)
{
if (deck[row][col] == card)
{
hand[r][0] = row;
hand[r][1] = col;
printf("%5s of %-8s", face[col], suit[row]);
++r;
} // end of if statement
} // end of for loop
} // end of for loop
printf(" ");
} // end of for loop
} // end of function deal
// Fuction to find pairs
void pair(int hand[][2], char *suit[], char *face[])
{
// Record the number of cards and their hands
int count[13] = { 0 };
int r, pair;
for (r = 0; r < 5; r++)
{
count[hand[r][1]]++;
}
// If a pair exists, then output it
for (pair = 0; pair < 13; pair++)
{
if (count[pair] == 2)
{
printf("The hand contains a pair of %ss.", face[pair]);
} // end of if statement
} // end of for loop
} // end of function pair
// Fuction to find three of a kind
void threeOfAKind(int hand[][2], char *suit[], char *face[])
{
// Record the number of cards and their hands
int count[13] = { 0 };
int r, three;
for (r = 0; r < 5; r++)
{
count[hand[r][1]]++;
}
// If a three of a kind exists, then output it
for (three = 0; three < 13; three++)
{
if (count[three] == 3)
{
printf("The hand contains a three of a kind of %ss.", face[three]);
} // end of if statement
} // end of for loop
} // end of function threeOfAKind
// Fuction to find four of a kind
void fourOfAKind(int hand[][2], char *suit[], char *face[])
{
// Record the number of cards and their hands
int count[13] = { 0 };
int r, four;
for (r = 0; r < 5; r++)
{
count[hand[r][1]]++;
}
// If four of a kind exists, then output it
for (four = 0; four < 13; four++)
{
if (count[four] == 4)
{
printf("The hand contains four of a kind of %ss.", face[four]);
} // end of if statement
} // end of for loop
} // end of function fourOfAKind
// Function to find a straight hand
void straightHand(int hand[][2], char *suit[], char *face[])
{
int sH[5] = { 0 };
int temp, r, pass, comp, j;
// Copy the column location for bubble sorting
for (r = 0; r < 5; r++)
{
sH[r] = hand[r][1];
} // end of for loop
// Use bubble sorting for column locations
for (pass = 1; pass < 5; pass++)
{
for (comp = 0; comp < 4; comp++)
{
if (sH[comp] > sH[comp + 1])
{
temp = sH[comp];
sH[comp] = sH[comp + 1];
sH[comp + 1] = temp;
} // end of if statement
} // end of for loop
} // end of for loop
// Making sure sorted columns are straight
// after bubble sorted
if (sH[4] - 1 == sH[3] && sH[3] - 1 == sH[2] && sH[2] - 1 == sH[1] && sH[1] - 1 == sH[0])
{
printf("The hand contains a straight from %s to %s.", face[sH[0]], face[sH[4]]);
} // end of if statement
} // end of function straightHand
// Fuction to find a flush in the hand
void flushHand(int hand[][2], char *suit[], char *face[])
{
// Record the number of cards and their hands
int count[4] = { 0 };
int r, flush;
for (r = 0; r < 5; r++)
{
count[hand[r][0]]++;
}
// If flush exists, then output it
for (flush = 0; flush < 4; flush++)
{
if (count[flush] == 5)
{
printf("The hand contains flush of %ss.", suit[flush]);
} // end of if statement
} // end of for loop
} // end of function flushHand
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.