Question
I've already completed card.c but I need help completing poker.c. All I need to do is populate the deck with the 52 standard cards in
I've already completed card.c but I need help completing poker.c. All I need to do is populate the deck with the 52 standard cards in the "void populateDeck()" method
The header file card.h defines a card structure and specifies two functions that operate on cards. The source file card.c has the bodies for the specified functions, but some code is missing. Finally, the source file poker.c supposed to generate a poker hand of five cards, print those five cards, and then print what kind of hand it is but much of its code is missing. To compile the program, type: gcc -std=c99 -Wall -o poker poker.c card.c
1 Completing card.c
Look over the rest of the code in card.c and work at understanding anything that you dont initially understand. When you have done so, add the missing code to create Card() to populate a cards fields. Finally, change the first two lines of display Card()so that this function uses the fields from the card argument that is passed to the function.You may want to add a main() function to card.c and compile only card.c to check that you made the correct changes.
2 Completing poker.c
If you added a main()function to card.c, remove it so that there is only one main() function when you compile the full program. In poker.c, the first thing youll want to do is write the code for populateDeck(). Using createCard() from card.c, create cards corresponding to the 52 standard playing cards and add them to the deck[] array. You might put code in main()to print out all 52 cards in deck[] using display Card(), to confirm that you wrote populate Deck() correctly. Now write the code for is ThreeOfKind(), isFullHouse(), and is FourOfKind().
Here is the starter code:
card.h
#ifndef CARD_HEADER #define CARD_HEADER
typedef enum { CLUBS, DIAMONDS, HEARTS, SPADES } suits;
typedef struct { int value; // 1=Ace, 11=Jack, 12=Queen, 13=Queen. 2-10 as that number. No other values allowed. suits suit; } card;
card* createCard( int face, suits suit, card* blankCard ); char* displayCard( card* theCard, char *displayString );
#endif
card.c
#include #include #include "card.h"
card* createCard( int value, suits suit, card* blankCard ) /* Assigns the provided values to a blank card, making it no longer blank. * Returns the no-longer-blank card. */{ blankCard->value = value; blankCard->suit = suit; return blankCard; }
char* displayCard( card* theCard, char *displayString ) /* Places the printable representation of theCard into displayString and returns the string. * The argument displayString must have at least 21 bytes allocated. */{ /* REPLACE WITH AN ASSIGNMENT TO THECARD'S VALUE */ int value = theCard->value; /* REPLACE WITH AN ASSIGNMENT TO THECARD'S SUIT */ int suit = theCard->suit;
char *valueString; char *suitString; switch (suit) { case CLUBS: suitString = "CLUBS"; break; case DIAMONDS: suitString = "DIAMONDS"; break; case HEARTS: suitString = "HEARTS"; break; case SPADES: suitString = "SPADES"; break; default: suitString = "UNKNOWN"; } if ((value<1)||(value>13)) // Illegal values { valueString = "UNKNOWN"; } else if ((value>1)&&(value<11)) // Number card { valueString = malloc(3); /* PLACE THE CONTROL STRING IN THE SECOND ARGUMENT THAT YOU WOULD USE TO PRINT AN INTEGER */ sprintf(valueString, "%d", value); } else // Ace or face card { switch (value) { case 1: valueString = "ACE"; break; case 11: valueString = "JACK"; break; case 12: valueString = "QUEEN"; break; case 13: valueString = "KING"; break; default: valueString = "DEADCODE"; // This line is unreachable } } sprintf(displayString, "%s of %s", valueString, suitString); if ((value>1)&&(value<11)) free(valueString); return displayString; }
// int main() // { // card *c = malloc(sizeof(card)); // c = createCard(3,HEARTS,c); // char *s = malloc(21); // printf("%s ",displayCard(c,s)); // }
poker.c
#include #include #include #include "card.h"
card deck[52]; // a "deck" of 52 cards
void populateDeck() { //complete this
}
}
card* sort(card* subdeck, int size) { card temp; for (int i = 0; i < size-1; i++) for (int j = i+1; j < size; j++) if (subdeck[i].value > subdeck[j].value) { temp = subdeck[i]; subdeck[i] = subdeck[j]; subdeck[j] = temp; } return subdeck; }
card* getHand(card* emptyHand, int sizeOfHand) /* Randomly selects "sizeOfHand" cards and returns them. The emptyHand argument must * have space for at least "sizeOfHand" cards. */{ int selection; for (int i = 0; i < sizeOfHand; i++) { selection = rand() % 52; emptyHand[i] = deck[selection]; // if this were more robust, we'd check for duplicates } return sort(emptyHand,sizeOfHand); }
int isPair(card* hand, int sizeOfHand) /* Returns 1 if two of the cards in "hand" have the same value; returns 0 otherwise. * "hand" must be sorted from least value to greatest value. */{ /* STUDY THIS CODE. WHY DOES IT PRODUCE THE INTENDED RESULT? */ int pair = 0; for (int i = 0; i < sizeOfHand-1; i++) pair = pair || (hand[i].value == hand[i+1].value); // because hand is sorted, a pair must be two adjacent cards return pair; }
int isTwoPair(card* hand, int sizeOfHand) /* Returns 1 if two of the cards in "hand" are a pair and two other are also a pair; returns 0 otherwise. * "hand" must be sorted from least value to greatest value. */{ int numberOfPairs = 0; card* partialHand; int i=0; while (i < sizeOfHand) { /* RECALL THAT ARRAYS ARE POINTERS */ partialHand = hand + i; /* THIS IS CHANGING THE ADDRESS IN THE "PARTIALHAND" POINTER TO A DIFFERENT PART OF THE ARRAY */ if (isPair(partialHand,2)) { numberOfPairs++; i += 2; } else { i++; } } return (numberOfPairs == 2); }
int isThreeOfKind(card* hand, int sizeOfHand) /* Returns 1 if three of the cards in "hand" have the same value; returns 0 otherwise. * "hand" must be sorted from least value to greatest value. */{ //finish this
return -1; }
int isStraight(card* hand, int sizeOfHand) /* Returns 1 if the cards have contiguous values; returns 0 otherwise. * "hand" must be sorted from least value to greatest value. */{ /* STUDY THIS CODE. WHY DOES IT PRODUCE THE INTENDED RESULT? */ int notStraight = 0; for (int i = 0; i < sizeOfHand-1; i++) notStraight = notStraight + (hand[i+1].value - hand[i].value - 1); return !notStraight; }
int isFlush(card* hand, int sizeOfHand) /* Returns 1 if the cards all have the same suit; returns 0 otherwise. */{ /* STUDY THIS CODE. WHY DOES IT PRODUCE THE INTENDED RESULT? */ int flush = 1; int suit = hand[0].suit; for (int i = 1; i < sizeOfHand; i++) flush = flush && (hand[i].suit == suit); return flush; }
int isFullHouse(card* hand, int sizeOfHand) /* Returns 1 if three of the cards in "hand" are three of a kind and another two are a pair; returns 0 otherwise. * "hand" must be sorted from least value to greatest value. */{ /* WRITE THIS FUNCTION */ return -1; }
int isFourOfKind(card* hand, int sizeOfHand) /* Returns 1 if four of the cards in "hand" have the same value; returns 0 otherwise. * "hand" must be sorted from least value to greatest value. */{ /* WRITE THIS FUNCTION */ return -1; }
int isStraightFlush(card* hand, int sizeOfHand) /* Returns 1 if the cards in "hand" are both a straight and a flush; returns 0 otherwise. * "hand" must be sorted from least value to greatest value. */{ return isStraight(hand,sizeOfHand) && isFlush(hand,sizeOfHand); }
//use the main function for testing int main(int argc, char const *argv[]) { srand(time(NULL));
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started