Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulates shuffling and dealing a standard deck of 52 cards

ID: 3836495 • Letter: W

Question

Write a program that simulates shuffling and dealing a standard deck of 52 cards Task 1: Shuffle the 52 cards: 13 each, Ace through King, of 4 suits (hearts, spades, clubs, and diamonds) Task 2: Ask the user how many cards to deal per hand, and how many players Chands to deal. Example output How many cards per hand? 5 How many s? 4 player Player 1: Four of Hearts Ace of Hearts King of Diamonds Six of Hearts Seven of Clubs Player 2: Three of Hearts Ace of Diamonds Nine of Hearts Nine of Clubs Deuce of Spades Player 3: Deuce of Clubs Three of Diamonds Ten of Hearts Eight of Hearts Deuce of Hearts Player 4: Three of Spades Five of Clubs Jack of Clubs Four of Clubs Ten of Diamonds

Explanation / Answer

NOTE: I have added more detailed comments in your code wherever necessary. Hope this will clearly let you understand the code.

#include<stdio.h>   
// Header file for Standard I/O functions like printf and scanf

#include<stdlib.h>
// Header file containing random value functions like rand(), srand() and other utilities

#include<time.h>
// Header file for Time-related data types and functions (time_t)

#define MAX_CARDS 52   
// Maximum cards are 52, thats why we are defining MAX_CARDS as 52 here
// #define is a preprocessor statement which can be used to define constants and is global. So wherever MAX_CARDS is used in the program it is replaced
// by 52

// function to generate random number from 0 to 51
int random_number()   
{
   // rand() generates random numbers and since we are doing random number modulus 52, the values generated are in the range of 0 to 51
   // some example rand() values for your understanding
   // Assume rand() generates 100 then rand()%52 which is 100%52 will give you 48
   // Assume rand() generates 200 then rand()%52 which is 200%52 will give you 44
   // Assume rand() generates 52 then rand()%52 which is 52%52 will give 0
   // As you can see any rand() value generated when rand()%52 generates value between 0 to 51
return (rand()%52);   
// the above return function returns the random value generated to the calling function
}

// Function to display card distribution for all the players
// Receives arguments for all card values, number of hands, no of players
// int* values - array representing card distribution. We can also declare it as int[] values
// hands - number of hands(cards) for each player
// players - number of players in the game
void display_cards(int* values,int hands,int players)
{

   // declaration variables using in loops
int i,j,k;
// variable to store suit of the card depending on the range of card values. It is divided into 4 groups of 1-13, 14-26, 27-39, 40-52
int suits;
// variable to store the card value depending on relative value in each range (1-13)
int card;   
k=0;
// iterate over each player, to display the card distribution they got
for(i=0;i<players;i++)   
{
   // display player number
printf("Player :%d ",(i+1));
// iterate over each hand (card) of the player “i” being considered   
for(j=0;j<hands;j++)   
{
   // To get the suit of card from the value from the range (1-13 (Clubs),14-26 (Hearts),27-39 (Spades),40-52 (Diamonds))for the kth card.
           // Clubs = 1, Hearts = 2, Spades = 3, Diamonds = 4
           // we are diving by the values[k] by 13 so we can get four groups(clubs, hearts, spades, diamonds). Since values array will have values from // 0 to 51 we are adding 1 to (values[k]/13) + 1
suits = (values[k]/13)+1;
// To get the card value from (1-13) range. How we will get the numbers within range of 13 by using %. The logic is same as what we have
// discussed in random_number function
card=(values[k]%13)+1; // To get the card value (1-13)
// switch case for checking each card number obtained ( 1 - ACE , 2-10 are Two to Ten, 11- Jack, 12-Queen, 13- King)
switch(card)

{
   //if card number is 1 -> ACE
case 1:   
printf("Ace ");
// break out from the switch case. break statement in 'C' will break out from the particular decision or control statement. Here in
// this case it comes out from switch statement. In case of loops it comes out of the looping statement
// So here whenever break statement is encountered you can understand it takes you out of the switch statement based on whether that // particular case matches. Like for example, if card value is 2, the "case 2" matches and breaks out from switch statement
break;
//if card number is 2 -> TWO
case 2:   
printf("Two ");
break;
//if card number is 3 -> THREE
case 3:   
printf("Three ");
break;
//if card number is 4 -> FOUR
case 4:   
printf("Four ");
break;
//if card number is 5 -> FIVE
case 5:   
printf("Five ");
break;
//if card number is 6 -> SIX
case 6:   
printf("Six ");
break;
//if card number is 7 -> SEVEN
case 7:   
printf("Seven ");
break;
//if card number is 8 -> EIGHT
case 8:
printf("Eight ");
break;
//if card number is 9 -> NINE
case 9:   
printf("Nine ");
break;   
//if card number is 10 -> TEN   
case 10:
printf("Ten ");
break;
//if card number is 11 -> JACK   
case 11:
printf("Jack ");
break;
//if card number is 12 -> QUEEN   
case 12:
printf("Queen ");
break;
//if card number is 13 -> KING
case 13:
printf("King ");
break;
//invalid option. If card value is not within the range of 1-13, the switch statement comes to default case prints the statement "WRONG" // and breaks/comes out from switch statement   
default:
printf("WRONG");
break;
}

// switch cases for suit’s name
switch(suits)
{
   //suit number 1 = CLUB
case 1:   
printf("of Clubs");
break;
//suit number 2 = HEART
case 2:   
printf("of Hearts");
break;
//suit number 3 = SPADE
case 3:   
printf("of Spades");
break;
//suit number 4 = DIAMOND
case 4:   
printf("of Diamonds");
break;
// suits should be in the range of 1-4. otherwise switch comes to the default case and breaks/comes out from switch statement
default:
printf("Nothing");
break;
}
//next line is printed using printf statement
printf(" ");   
//update k value for card number
k++;
}
//for next player to distinguish easily we are printing two lines
printf(" ");   
}
}

// main function where your 'c' program starts running
int main()
{
   // create a time object to provide better random numbers otherwise same random numbers will be generated at each run
time_t t;

// initialize random number with time seed so that it provides randomness each time the rand() function is called. The reason for choosing time is // because it changes every second.
srand((unsigned) time(&t));
// array for Total Cards
int cards[MAX_CARDS];
//Variables for hands and players
int hands,players;
// 2 loop variables
int loop1,loop2;
// flag to check if a number generated is unique. By default we are assigning it to 1 and if unique number is already generated then it will set to 0
int unique_no=1;

printf("How Many Cards Per Hand ? ");
// taking input for hands
scanf("%d",&hands);
printf("How Many Players ? ");
// taking input for players
scanf("%d",&players);
// check if cards are exceeding the max limit i.e. 52
// Here we are checking if the input provided for hands and players in the above scanf statement are not exceeding the value 52
// Remember MAX_CARDS are coming from the #define statement in beginning of this program where we specified MAX_CARDS to value 52
if( hands*players <= MAX_CARDS)
{
   // loop1 iteration for all random values the total required cards are (hands x players) -> “hand” number of cards for each player
// loop1 is not incremented here so as to ensure uniqueness of the generated card. Because same card can not be generated twice in a
// distribution.
for(loop1=0;loop1<hands*players;)

{
   // calling random number generation function where it will give values from 0 to 51
int rand_num=random_number();
// loop2 iteration to check if the number has already been generated for an earlier card
for(loop2=0;loop2<loop1;loop2++)
{
   // checking if previous card has same value as this by using flag unique_no which is initialized to 1 and will set to 0 if unique number
   // is already generated
if(cards[loop2]==rand_num)
{
   //set flag variable unique_no to 0
unique_no=0;
// If the same number already exists we should come out of the loop. Thats why using break here.
break;
}
}
// If unique number does not exists, that means we can pick the random value generated between 0 to 51
if(unique_no==1)
{
   // unique no and not exists
cards[loop1]=rand_num;
// loop1 is incremented here after ensuring that the generated number is unique.
loop1++;
}
else
   // resetting flag unique_no to default value 1 after identifying if unique number already generated
unique_no=1;
}
// display result
display_cards(cards,hands,players);
}
// if hands * players > 52 then the below printf statement will be printed
else
{
printf("CARDS EXCEEDS LIMIT OF MAX CARDS");
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote