C Programming Question: I\'m curious how I might be able to declare a winner out
ID: 3695478 • Letter: C
Question
C Programming
Question: I'm curious how I might be able to declare a winner out of the declared playerAmount.
Here are the original instructions:
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 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 ");
Here is my code so far:
// Needed libraries
#include
#include
#include
// Declare constants
#define SUITS 4
#define FACES 13
#define AVAILABLE 0 // card not drawn from deck
#define TAKEN 1 // card has been drawn from deck
#define SIZE 5
#define TRUE 1
#define FALSE 0
//Needed functions
void dealFunction(char *suits[], char *faces[], int deck[][FACES], int *hand_aux);
void dealHand(char *suits[], char *faces[], int deck[][FACES], int hand[][2]);
void analyzeHand(int suitsInHand[], int facesInHand[]);
void analyze(int suitsInHand[], int facesInHand[], int hand[][2]);
typedef int bool;
bool straight, flush, four, three;
// Possible 0, 1, 2
int pairs;
main(){
// Seed
srand(time(NULL));
// Declare datatypes
char *suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
char *faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
int deck[4][13] = { AVAILABLE }; // using array to make sure the same card is drawn from a single deck
int i;
int suitsInHand[SUITS], facesInHand[FACES];
int hand[SIZE][2]; // using array to store the hand
int playerAmount;
//Print player input
printf("***POKER*** ");
printf("Please enter the number of players: ");
scanf("%d", &playerAmount);
printf(" ");
//Loop + functions for each hand dealt
for(i = 0; i < playerAmount; i++) {
dealHand(suits, faces, deck, hand); //adding the hand parameter
analyze(suitsInHand, facesInHand, hand);
analyzeHand(suitsInHand, facesInHand);
}
system("pause");
}
void dealFunction(char *suits[], char *faces[], int deck[][FACES] , int *hand_aux){
int suitIndex, faceIndex;
suitIndex = rand() % 4;
faceIndex = rand() % 13;
while (deck[suitIndex][faceIndex] == TAKEN){
suitIndex = rand() % 4;
faceIndex = rand() % 13;
}
deck[suitIndex][faceIndex] = TAKEN;
// In hand_aux will be stored the value of the card
hand_aux[0]=faceIndex;
hand_aux[1]=suitIndex;
printf("%s of %s ", faces[faceIndex], suits[suitIndex]);
}
void dealHand(char *suits[], char *faces[], int deck[][FACES], int hand[][2]){
int i;
int hand_aux[2];
for(i = 0; i < 5; i++){
dealFunction(suits, faces, deck, hand_aux);
hand[i][0]=hand_aux[0]; //storing each hand in hand array
hand[i][1]=hand_aux[1];
}
printf(" ");
}
void analyze(int suitsInHand[], int facesInHand[], int hand[][2]){
int i;
//Initializes it to 0
for(i=0;i<5;i++)
suitsInHand[i]=0;
for(i=0;i<13;i++)
facesInHand[i]=0;
// Fill it
for(i=0;i<5;i++){
facesInHand[hand[i][0]]++;
suitsInHand[hand[i][1]]++;
}
//Print it
for(i=0;i<5;i++)
printf("%d ",suitsInHand[i]);
printf(" ");
for(i=0;i<13;i++)
printf("%d ",facesInHand[i]);
printf(" ");
}
void analyzeHand(int suitsInHand[], int facesInHand[]){
int num_consec = 0;
int rank, suit;
straight = FALSE;
flush = FALSE;
four = FALSE;
three = FALSE;
pairs = 0;
for (suit = 0; suit < SUITS; suit++)
if (suitsInHand[suit] == 5)
flush = TRUE;
rank = 0;
while (facesInHand[rank] == 0)
rank++;
for (; rank < FACES && facesInHand[rank]; rank++)
num_consec++;
if (num_consec == 5) {
straight = TRUE;
return;
}
for (rank = 0; rank < FACES; rank++) {
if (facesInHand[rank] == 4)
four = TRUE;
if (facesInHand[rank] == 3)
three = TRUE;
if (facesInHand[rank] == 2)
pairs++;
}
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 ");
}
Explanation / Answer
{
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++;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.