Question

C Programming The following code creates a deck of cards, shuffles it, and deals to players....

C Programming

The following code creates a deck of cards, shuffles it, and deals to players. This program receives command line input [1-13] for number of players and command line input [1-13] for number of cards. Please modify this code to deal cards in a poker game. Command line input must accept [2-10] players and [5] cards only per player. Please validate input. Then, display the sorted hands, and then display the sorted hands - labeling each hand with its poker hand rank. Decks, hands and cards should be clean, clear, aligned and appropriately labeled - do not simply list as a simple column or row of values. Next, display a list of winner(s), or clearly designate the winner(s) with the labeled hands. After the game is concluded, have your program execute a “test function” that passes a sequence of pre-set poker hands, one for each poker-rank hand, in order to validate that each poker-ranking function correctly ranks such hands. YOU MUST USE YOUR POKER HAND-RANKING FUNCTIONS TO RANK EACH PRE-SET POKER HAND. Display each of the pre-set hands and the results of the tests. See sample output below.

Sample Output: from ./MainPoker 5 5 [graphics not required]

The legend is as follows:

Suits: Hearts='♡', Diamonds='♢', Clubs='♧', and Spades='♤'

Ranks: Ace='A', Two='2',..., Ten='T', Jack='J', Queen='Q', and King='K'

Original Ordered Deck:

[ A-♡ ] [ A-♢ ] [ A-♧ ] [ A-♤ ] [ 2-♡ ] [ 2-♢ ] [ 2-♧ ] [ 2-♤ ] [ 3-♡ ] [ 3-♢ ] [ 3-♧ ] [ 3-♤ ] [ 4-♡ ]

[ 4-♢ ] [ 4-♧ ] [ 4-♤ ] [ 5-♡ ] [ 5-♢ ] [ 5-♧ ] [ 5-♤ ] [ 6-♡ ] [ 6-♢ ] [ 6-♧ ] [ 6-♤ ] [ 7-♡ ] [ 7-♢ ]

[ 7-♧ ] [ 7-♤ ] [ 8-♡ ] [ 8-♢ ] [ 8-♧ ] [ 8-♤ ] [ 9-♡ ] [ 9-♢ ] [ 9-♧ ] [ 9-♤ ] [ T-♡ ] [ T-♢ ] [ T-♧ ]

[ T-♤ ] [ J-♡ ] [ J-♢ ] [ J-♧ ] [ J-♤ ] [ Q-♡ ] [ Q-♢ ] [ Q-♧ ] [ Q-♤ ] [ K-♡ ] [ K-♢ ] [ K-♧ ] [ K-♤ ]

Random Shuffled Deck:

[ 4-♢ ] [ 3-♧ ] [ 2-♢ ] [ 6-♤ ] [ Q-♡ ] [ 5-♢ ] [ 7-♡ ] [ T-♤ ] [ 4-♡ ] [ 5-♡ ] [ 8-♡ ] [ 7-♧ ] [ 9-♡ ]

[ A-♡ ] [ Q-♢ ] [ 9-♤ ] [ 8-♧ ] [ 8-♤ ] [ 6-♡ ] [ 5-♧ ] [ 3-♢ ] [ 3-♡ ] [ Q-♧ ] [ T-♧ ] [ T-♢ ] [ 4-♤ ]

[ A-♧ ] [ K-♧ ] [ 7-♤ ] [ 6-♢ ] [ 2-♧ ] [ J-♧ ] [ 5-♤ ] [ 6-♧ ] [ K-♢ ] [ 9-♢ ] [ A-♢ ] [ K-♤ ] [ J-♤ ]

[ 8-♢ ] [ 3-♤ ] [ 2-♤ ] [ 4-♧ ] [ J-♢ ] [ J-♡ ] [ K-♡ ] [ 2-♡ ] [ 7-♢ ] [ T-♡ ] [ Q-♤ ] [ 9-♧ ] [ A-♤ ]

Player Hands: (dealt from top/front of deck)

Player 1] - [ 4-♢ ] [ 3-♧ ] [ 2-♢ ] [ 6-♤ ] [ Q-♡ ]

Player 2] - [ 5-♢ ] [ 7-♡ ] [ T-♤ ] [ 4-♡ ] [ 5-♡ ]

Player 3] - [ 8-♡ ] [ 7-♧ ] [ 9-♡ ] [ A-♡ ] [ Q-♢ ]

Player 4] - [ 9-♤ ] [ 8-♧ ] [ 8-♤ ] [ 6-♡ ] [ 5-♧ ]

Player 5] - [ 3-♢ ] [ 3-♡ ] [ Q-♧ ] [ T-♧ ] [ T-♢ ]

Player Hands: sorted

Player 1] - [ 2-♢ ] [ 3-♧ ] [ 4-♢ ] [ 6-♤ ] [ Q-♡ ]

Player 2] - [ 4-♡ ] [ 5-♡ ] [ 5-♢ ] [ 7-♡ ] [ T-♤ ]

Player 3] - [ A-♡ ] [ 7-♧ ] [ 8-♡ ] [ 9-♡ ] [ Q-♢ ]

Player 4] - [ 5-♧ ] [ 6-♡ ] [ 8-♧ ] [ 8-♤ ] [ 9-♤ ]

Player 5] - [ 3-♢ ] [ 3-♡ ] [ T-♧ ] [ T-♢ ] [ Q-♧ ]

Player Hands: ranked

Player 1] - [ 2-♢ ] [ 3-♧ ] [ 4-♢ ] [ 6-♤ ] [ Q-♡ ] - High Card

Player 2] - [ 4-♡ ] [ 5-♡ ] [ 5-♢ ] [ 7-♡ ] [ T-♤ ] - One Pair

Player 3] - [ A-♡ ] [ 7-♧ ] [ 8-♡ ] [ 9-♡ ] [ Q-♢ ] - High Card

Player 4] - [ 5-♧ ] [ 6-♡ ] [ 8-♧ ] [ 8-♤ ] [ 9-♤ ] - One Pair

Player 5] - [ 3-♢ ] [ 3-♡ ] [ T-♧ ] [ T-♢ ] [ Q-♧ ] - Two Pairs

Player Hands: winner(s)

Player 1] - [ 2-♢ ] [ 3-♧ ] [ 4-♢ ] [ 6-♤ ] [ Q-♡ ] - High Card

Player 2] - [ 4-♡ ] [ 5-♡ ] [ 5-♢ ] [ 7-♡ ] [ T-♤ ] - One Pair

Player 3] - [ A-♡ ] [ 7-♧ ] [ 8-♡ ] [ 9-♡ ] [ Q-♢ ] - High Card

Player 4] - [ 5-♧ ] [ 6-♡ ] [ 8-♧ ] [ 8-♤ ] [ 9-♤ ] - One Pair

Player 5] - [ 3-♢ ] [ 3-♡ ] [ T-♧ ] [ T-♢ ] [ Q-♧ ] - Two Pairs - Winner

Poker Hands: test

Hand: [ 2-♢ ] [ 3-♧ ] [ 4-♢ ] [ 6-♤ ] [ Q-♡ ] - High Card

Hand: [ 4-♡ ] [ 5-♡ ] [ 5-♢ ] [ 7-♡ ] [ T-♤ ] - One Pair

Hand: [ 3-♢ ] [ 3-♡ ] [ T-♧ ] [ T-♢ ] [ Q-♧ ] - Two Pairs

Hand: [ 3-♢ ] [ 3-♡ ] [ 3-♤ ] [ T-♢ ] [ Q-♧ ] - Three of a Kind

Hand: [ A-♤ ] [ 2-♢ ] [ 3-♧ ] [ 4-♢ ] [ 5-♢ ] - Straight

Hand: [ 2-♧ ] [ 3-♧ ] [ 4-♧ ] [ 6-♧ ] [ Q-♧ ] - Flush

Hand: [ 3-♢ ] [ 3-♡ ] [ 3-♤ ] [ T-♢ ] [ T-♧ ] - Full House

Hand: [ 3-♢ ] [ 3-♡ ] [ 3-♤ ] [ 3-♧ ] [ Q-♧ ] - Four of a Kind

Hand: [ T-♢ ] [ J-♢ ] [ Q-♢ ] [ K-♢ ] [ A-♢ ] - Straight Flush

Existing code begins here

#include<stdio.h>
#include <time.h> //time function
#include <stdlib.h> //random number generator functions
#include <string.h>
#define MAX 9
#define MAX_CARDS 52
#define MAX_RANKS 13
#define MAX_SUITS 4
#define COLS 3 //number of columns to display in output
//structure definition
struct card
{
   char *rank;
   char suit[MAX];
};
typedef struct card Card;
//array of pointers to strings for ranks
char *ranks[MAX_RANKS] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
   "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
//two-dimensional array of strings for suits
char suits[MAX_SUITS][MAX] = {"Clubs", "Diamonds", "Hearts", "Spades"};

void initialize(Card []);
void shuffle(Card []);
void display(const Card[],const int );
void removeTop(Card deck[],int length);

//arguments to store.
int main(int argc, char *argv[])
{
   //check for valid number of arguments and validation of arguments
   if(argc != 3)
   {
       printf("\nInvalid Number of arugments given\nUSAGE: MainCardHands cards_per_hand players");
       return 1;
   }
  
   int cardsPerHand = atoi(argv[1]);
   int numOfPlayers = atoi(argv[2]);
   printf("\nNumber of Cards per Hand: %d Number of Players: %d\n\n",cardsPerHand,numOfPlayers);
   if(cardsPerHand <= 0 || cardsPerHand>13 || numOfPlayers >13 || numOfPlayers <= 0)
   {
       printf("\nError: Cards Per Hand and Number of Players must between 1 to 13 both inclusive;");
       return 1;
   }
  
   if(numOfPlayers * cardsPerHand >52)
   {
       printf("\nError: Can't dealt %d cards for each person",cardsPerHand);
       return 1;
   }
  
  
  
   char newline = '\n'; //to repeat while loop
   //declare an array of 52 cards
   Card deck[MAX_CARDS];// = {"",""};
   initialize(deck);
   printf("Display an ordered deck of cards:\n");
   int length = MAX_CARDS;
  
   //MODIFIED THE display() function to take length
   //which helps in displaying remaining deck after dealing
   display(deck,length);
  
   while('\n' == newline)
   {
       printf("\nshuffling deck ... \n");
       shuffle(deck);
       display(deck,length);
       printf("Would you like to shuffle again?\nIf so, press \"Enter\" key. If not, press any other key. ");
       newline = getchar();
      
   }
  
   printf("\n\n------------- Dealing %d Cards Per Hand to %d persons -----------------\n",cardsPerHand,numOfPlayers);
  
   int i,j;
   printf("\n");
  
   //till cardsPerHand
   for(i =0 ;i<cardsPerHand;i++)
   {
       printf("\n*********************** Deal : %d *************************\n\n",(i+1));
       //deal card to each person.
       for(j = 0;j<numOfPlayers;j++)
       {
           //store top of the deck, here deck[0] is the top of the deck.
           Card top = deck[0];
           //remove the deck.
           removeTop(deck,length);
           //print max three persons per line
           printf("Player - %3d --> %-6s of %-12s",(j+1), top.rank, top.suit);
           if(0==((j+1)%COLS))
           {
               printf("\n");
           }
           length--;
       }
       printf("\n");
   }
   printf("\n\n----------- Deck after Dealing Cards ---------------\n\n");
   display(deck,length);
   return 0;
}
void removeTop(Card deck[],int length)
{
   int i;
   for(i =0;i<length-1;i++)
   {
       deck[i] = deck[i+1];
   }
}
/*
initialize the deck of cards to string values
deck: an array of structure cards
*/
void initialize(Card deck[])
{
   int i = 0;
   for(i=0;i<MAX_CARDS;i++)
   {
       deck[i].rank = ranks[i%MAX_RANKS];
       strncpy(deck[i].suit, suits[i/MAX_RANKS], MAX);
   }
}
/*
use the pseudo-random number generator to shuffle the cards
deck: an array of structure cards
*/
void shuffle(Card deck[])
{
   int swapper = 0; //index of card to be swapped
   int i = 0; //counter
   Card temp = {"", ""}; //temp holding place for swap
   srand(time(NULL)); //seed the random numbers with current time
   for(i=0;i<MAX_CARDS;i++)
   {
       //generate a pseudo-random number from 0 to 51
       swapper = rand() % MAX_CARDS;
       //swap current card with da swapper
       temp = deck[i];
       deck[i] = deck[swapper];
       deck[swapper] = temp;
   }
}
/*
display the deck of cards
deck: an array of structure cards
*/
void display(const Card deck[],int length)
{
   int i = 0;
   for(i=0;i<length;i++)
   {
       printf("%5s of %-12s", deck[i].rank, deck[i].suit);
       //put in a newline every %x loops
       if(0==((i+1)%COLS))
       {
           printf("\n");
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include <time.h> //time function
#include <stdlib.h> //random number generator functions
#include <string.h>
#define MAX 9
#define MAX_CARDS 52
#define MAX_RANKS 13
#define MAX_SUITS 4
#define COLS 3 //number of columns to display in output
//structure definition
struct card
{
   char *rank;
   char suit[MAX];
};
typedef struct card Card;
//array of pointers to strings for ranks
char *ranks[MAX_RANKS] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
   "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
//two-dimensional array of strings for suits
char suits[MAX_SUITS][MAX] = {"Clubs", "Diamonds", "Hearts", "Spades"};

void initialize(Card []);
void shuffle(Card []);
void display(const Card[],const int );
void removeTop(Card deck[],int length);

//arguments to store.
int main(int argc, char *argv[])
{
   //check for valid number of arguments and validation of arguments
   if(argc != 3)
   {
       printf("\nInvalid Number of arugments given\nUSAGE: MainCardHands cards_per_hand players");
       return 1;
   }
  
   int cardsPerHand = atoi(argv[1]);
   int numOfPlayers = atoi(argv[2]);
   printf("\nNumber of Cards per Hand: %d Number of Players: %d\n\n",cardsPerHand,numOfPlayers);
   if(cardsPerHand <= 0 || cardsPerHand>13 || numOfPlayers >13 || numOfPlayers <= 0)
   {
       printf("\nError: Cards Per Hand and Number of Players must between 1 to 13 both inclusive;");
       return 1;
   }
  
   if(numOfPlayers * cardsPerHand >52)
   {
       printf("\nError: Can't dealt %d cards for each person",cardsPerHand);
       return 1;
   }
  
  
  
   char newline = '\n'; //to repeat while loop
   //declare an array of 52 cards
   Card deck[MAX_CARDS];// = {"",""};
   initialize(deck);
   printf("Display an ordered deck of cards:\n");
   int length = MAX_CARDS;
  
   //MODIFIED THE display() function to take length
   //which helps in displaying remaining deck after dealing
   display(deck,length);
  
   while('\n' == newline)
   {
       printf("\nshuffling deck ... \n");
       shuffle(deck);
       display(deck,length);
       printf("Would you like to shuffle again?\nIf so, press \"Enter\" key. If not, press any other key. ");
       newline = getchar();
      
   }
  
   printf("\n\n------------- Dealing %d Cards Per Hand to %d persons -----------------\n",cardsPerHand,numOfPlayers);
  
   int i,j;
   printf("\n");
  
   //till cardsPerHand
   for(i =0 ;i<cardsPerHand;i++)
   {
       printf("\n*********************** Deal : %d *************************\n\n",(i+1));
       //deal card to each person.
       for(j = 0;j<numOfPlayers;j++)
       {
           //store top of the deck, here deck[0] is the top of the deck.
           Card top = deck[0];
           //remove the deck.
           removeTop(deck,length);
           //print max three persons per line
           printf("Player - %3d --> %-6s of %-12s",(j+1), top.rank, top.suit);
           if(0==((j+1)%COLS))
           {
               printf("\n");
           }
           length--;
       }
       printf("\n");
   }
   printf("\n\n----------- Deck after Dealing Cards ---------------\n\n");
   display(deck,length);
   return 0;
}
void removeTop(Card deck[],int length)
{
   int i;
   for(i =0;i<length-1;i++)
   {
       deck[i] = deck[i+1];
   }
}
/*
initialize the deck of cards to string values
deck: an array of structure cards
*/
void initialize(Card deck[])
{
   int i = 0;
   for(i=0;i<MAX_CARDS;i++)
   {
       deck[i].rank = ranks[i%MAX_RANKS];
       strncpy(deck[i].suit, suits[i/MAX_RANKS], MAX);
   }
}
/*
use the pseudo-random number generator to shuffle the cards
deck: an array of structure cards
*/
void shuffle(Card deck[])
{
   int swapper = 0; //index of card to be swapped
   int i = 0; //counter
   Card temp = {"", ""}; //temp holding place for swap
   srand(time(NULL)); //seed the random numbers with current time
   for(i=0;i<MAX_CARDS;i++)
   {
       //generate a pseudo-random number from 0 to 51
       swapper = rand() % MAX_CARDS;
       //swap current card with da swapper
       temp = deck[i];
       deck[i] = deck[swapper];
       deck[swapper] = temp;
   }
}
/*
display the deck of cards
deck: an array of structure cards
*/
void display(const Card deck[],int length)
{
   int i = 0;
   for(i=0;i<length;i++)
   {
       printf("%5s of %-12s", deck[i].rank, deck[i].suit);
       //put in a newline every %x loops
       if(0==((i+1)%COLS))
       {
           printf("\n");
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
C Programming The following code creates a deck of cards, shuffles it, and deals to players....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A standard poker deck of 52 cards has four suits, (symbols C, H, S, and D) and thirteen ranks (sy...

    A standard poker deck of 52 cards has four suits, (symbols C, H, S, and D) and thirteen ranks (symbols A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, and K). Every card in the deck has both a value and a suit.1 A poker hand is any set of 5 cards from the standard poker deck. There are some special hands in poker, and these have ranks (i.e. some are better, some are worse). From best...

  • Can someone please answer this by Friday? A poker deck consists of cards ranked 2; 3;...

    Can someone please answer this by Friday? A poker deck consists of cards ranked 2; 3; 4; 5; 6; 7; 8; 9; 10; J; Q;K;A (13 different ranks), each in four suits, for a total of 52 distinct cards. (a) What is the probability that a five-card poker hand drawn from a poker deck consists only of cards ranked 8; 9; 10; J; Q;K;A? (b) Find a probability of Three of a kind. This is, three cards of the same...

  • Java programing Write a program that deals a deck of card into 4 hands – Each...

    Java programing Write a program that deals a deck of card into 4 hands – Each hand having 13 cards. The program should be doing the followings use an array to randomly select 52 cards and deal it into 4 hands. Print each hand unsorted Identify the face value of each hand and display it. You should create a class called Card, and all the methods should be defined within the card class. Hints – Import below java utility to...

  • 1. Probabilities of Poker Hands These questions refer to a standard deck of 52 cards having...

    1. Probabilities of Poker Hands These questions refer to a standard deck of 52 cards having 4 suits , A, 0,♡ and 13 ranks 2,3,4,5,6,7,8,9,10,J,Q,K, A. You are dealt a 5-card hand from such a deck (so there are (53) possible hands, each one is equally likely). 1. A flush is a hand consisting of 5 cards that all have the same suit. For example {24,54, 104, JA, A$} is a flush. What is the probability that your hand is...

  • Create A Header file and A CPP File for this we want to simulate drawing cards...

    Create A Header file and A CPP File for this we want to simulate drawing cards from a deck,with or without replacement. With replacement means that the card is placed back in the deck after having been drawn. You will want to design a class that represents a deck of card (52 cards. 13 cards: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) The class is to be called “aDeckOfCards”. And it should generate a...

  • Consider a regular deck of cards with 52 cards in total. (1) How many ways can...

    Consider a regular deck of cards with 52 cards in total. (1) How many ways can we have a poker hand of 5 cards? (2) Four of a kind is a poker hand that contains all four cards of one rank and any other (unmatched) card. For example, 94 949 9♡ JA is a "four of a kind”. How many ways can we have "four of a kind" ? (3) In a poker hand of 5 cards, what's the probability...

  • 8. A dealer shuffles a deck of cards and deals you 5 cards off of the...

    8. A dealer shuffles a deck of cards and deals you 5 cards off of the top of the deck (A deck consists of 52 cards, divided into 4 suits of 13 cards each, so each of the 13 values' appears 4 times). What is the probability that your hand consists of 2 Queens and 3 Kings? Write out the full calculation, and then compute your final answer as a decimal.

  • 1. From a standard deck of 52 cards, how many 5-card poker hands are there, that...

    1. From a standard deck of 52 cards, how many 5-card poker hands are there, that have at least 3 spades? (Hint: divide the problem in cases: a poker hand has 3 spades, 4 spades, 5 spades.)

  • R. Given a standard deck of 52 cards with 5 cards being dealt to a player....

    R. Given a standard deck of 52 cards with 5 cards being dealt to a player. (a) Find the probability that the player's hand will have all 5 cards as spades. (4 marks) (b) Now find the probability that the player's hand is a flush. Note that a flush is a 5 card poker hand with all 5 cards being the same suit. (4 marks)

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT