Need help with more detailed comments. Hello, the code is completed and successf
ID: 3836489 • Letter: N
Question
Need help with more detailed comments. Hello, the code is completed and successfully compiling, but I would like MORE detailed comments in each step thoroughly explaining more about the code, why I'm using certain codes, significance of each step, etc to make it EASIER to understand. I want the steps to be explained to me as if I were a 10 year old learning C programming teaching it to people NEW to C programming. There are a lot of comments written in the code, but I want them to be MUCH easier and detailed to understand for each step please. Thank you.
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 DiamondsExplanation / Answer
#include<stdio.h>
// stdio stands for standard input output header
// Above statement tells the compiler to insert the contents of stdio at the particular place
// Library file for Standard IO Functions (printf , scanf)
// The functons are not defined by the programmer. They are predefined functions defined inside the library. Inorder to use these
// functions we have to add the above mentioned header file
#include<stdlib.h>
// stdlib stands for standard library definitions header.
// Library file for Random Value functions (rand() , srand())
// The functions are not defined by the programmer. They are predefined functions defined inside the library. Inorder to use these
// functions we have to add the above mentioned header file
#include<time.h>
// Library file for Time-related data types and functions (time_t)
#define MAX_CARDS 52
// define Maximum Cards in a Deck = 52
// #define is used to define a macro, so wherever MAX_CARDS is used in the program, it is replaced in the compilation by its value 52
// advantage of using #define is the value of MAX_CARDS will remain constant,same throughout the execution program and user can change the value,if required, through single location
// function to return a random number from 0-51
// int with function name represents the data type in which output will be provided by the function random_number().Also known as return type
// data type represents various type of data that can be used in program like int which stands for Integer, char stands for Character
int random_number()
{
return (rand()%52);
// rand() is a predefined function mentioned in <stdlib.h> file. It returns a random integer between 0 to 32767
// returning Random Numbers modulus 52, so range = 0-51. For example: 3000 % 52 = 36 , 1000 % 52 = 12
}//end of function random_number()
// Function to display the cards distribution for all the players
// void represents function display_cards will not return anything after executing.
// receives arguments(input required to run the function) for all card values, no. of hands and no. of players
// values - array representing card distribution. Array is a group of elements which are of same data type.
// hands - number of hands (cards) for each player
// players - number of players in the game
void display_cards(int* values,int hands,int players)
{
int i,j,k; // declaring iteration variables.
int suits; // variable to store the suit of the card depending on the range of card values, divided intp 4 groups of 1-13, 14-26, 27-39, 40-52
int card; // variable to store the card value depending on relative value in each range (1-13)
// all the declared variables are of type integer, they will store integer values only.
k=0; // k is initialized with value 0
/* explaining for loop mentioned below:
loop is controlled by variable i
loop starts with i = 0. it will be used in the first iteration of 'for' loop only. Purpose of using it is to initialise the value of i that it can be used in next iterations in loop
i<players is the condition on which 'for' loop decides whether to execute next iteration or not. If value 'i' becomes > players, then for loop execution would stop
i++ is used to increment the value of i. i++ is i = i+1. for example: if i = 0 then i++ = 0 + 1 =1
*/
for(i=0;i<players;i++) // loop would iterate over each player, to display the card distribution they have got
{
printf("Player :%d ",(i+1)); /* predefined function in <stdio.h>
display player number. i+1 is the player number. Value of 'i' has been increased by 1 in printf as we have initialized the value of i to 0. And player number should not be 0.
if i would have been initialized by 1 then loop would have been 'for(i=1;i<=players;i++)'*/
for(j=0;j<hands;j++) // loop would iterate over each hand (card) of the player “i” being considered. For example: loop would iterate over cards held by player 1, when i is 1.
{
suits = (values[k]/13)+1;
// To get the suit of the 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
// for example: value of kth card is 29 , the suit of that card will be (29/13)+1 = 2+1 = 3. The card belongs to Spades.
card=(values[k]%13)+1;
// To get the card value (1-13)
// Lets consider the above example of value of kth card as 29. The card belonged to Spades. Lets find out the value of the spades card.
// (29%13)+1 = 3+1 = 4. So we have 4 of Spades.
/*switch controls which code case to execute depending on the input provided.
Think of it as a lift in buildings. Lift would stop at floor 3 if the user has pressed 3.
Similarly switch would execute code for case 1 if the input provided is 1. that is the value of card is 1.
*/
switch(card) //switch case for card number as obtained ( 1 - ACE , 2-10 are Two to Ten, 11- Jack, 12-Queen, 13- King)
{
case 1: //if card number is 1 -> ACE
printf("Ace ");
break; //break out from the switch case
case 2: //if card number is 2 -> TWO
printf("Two ");
break; //break out from the switch case
case 3: //if card number is 3 -> THREE
printf("Three ");
break; //break out from the switch case
case 4: //if card number is 4 -> FOUR
printf("Four ");
break; //break out from the switch case
case 5: //if card number is 5 -> FIVE
printf("Five ");
break; //break out from the switch case
case 6: //if card number is 6 -> SIX
printf("Six ");
break; //break out from the switch case
case 7: //if card number is 7 -> SEVEN
printf("Seven ");
break; //break out from the switch case
case 8: //if card number is 8 -> EIGHT
printf("Eight ");
break; //break out from the switch case
case 9: //if card number is 9 -> NINE
printf("Nine ");
break; //break out from the switch case
case 10: //if card number is 10 -> TEN
printf("Ten ");
break; //break out from the switch case
case 11: //if card number is 11 -> JACK
printf("Jack ");
break; //break out from the switch case
case 12: //if card number is 12 -> QUEEN
printf("Queen ");
break; //break out from the switch case
case 13: //if card number is 13 -> KING
printf("King ");
break; //break out from the switch case
default: //invalid option. The input provided is other than all the mentioned cases. Generally used to handle incorrect inputs.
printf("WRONG");
break; //break out from the switch case
}
switch(suits) // switch cases for suit’s name
{
case 1: //suit number 1 = CLUB
printf("of Clubs");
break; //break out from the switch case
case 2: //suit number 2 = HEART
printf("of Hearts");
break; //break out from the switch case
case 3: //suit number 3 = SPADE
printf("of Spades");
break; //break out from the switch case
case 4: //suit number 4 = DIAMOND
printf("of Diamonds");
break; //break out from the switch case
default: // invalid option
printf("Nothing");
break; //break out from the switch case
}
printf(" "); //next line. ' ' is used to print new line.
k++; //increment value of k for card number
}
printf(" "); //for next player. prints 2 new lines.
}
}
/*
Entry point in program. It is always the first code to be executed when the program starts. All the functions defined above will be used inside main.
*/
int main() // main function
{
time_t t;
// create a time object to provide better random numbers otherwise same random numbers will be generated at each run
srand((unsigned) time(&t)); // initialize random number with time seed this seed provides randomness because time is changing over each simulations. It returns time since epoch(00:00:00 UTC, January 1, 1970),measured in seconds
int cards[MAX_CARDS]; // array for Total Cards
int hands,players; //Variables for hands and players
int loop1,loop2; // 2 loop variables
int unique_no=1; // flag to check uniqueness of number
printf("How Many Cards Per Hand ? ");
scanf("%d",&hands); // input for hands // scanf() is defined in <stdio.h>. used to read user input
printf("How Many Players ? ");
scanf("%d",&players); // input for players
/*
check the cards are exceeds from the max limit. This is for validating correct input.
lets say user entered cards per hands as 20 and players as 4 then total cards required would 80 which is greater than MAX_CARDS 52.
*/
if(hands*players<=MAX_CARDS)
{
for(loop1=0;loop1<hands*players;) // loop1 iteration for all random values the total required is 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.
{
int rand_num=random_number(); // calls the random_number function defined above. It return a random number that is consumed by int rand_number
for(loop2=0;loop2<loop1;loop2++) // loop2 iteration to check if the number has already been generated for an earlier card
{
if(cards[loop2]==rand_num) // check if previous card has same value as this
{
unique_no=0; //set flag variable to 0
break; // breaks out from the current for loop. because same number already exists
}
}
if(unique_no==1)
{
cards[loop1]=rand_num; // unique no and not exists
loop1++; // loop1 is incremented here after ensuring that the generated number is unique.
}
else
unique_no=1; // flag reset to default value of unique card
}
display_cards(cards,hands,players); // calls display_cards function defined above and displays the distribution of cards per players.
}
else // if the validation fails then else is used to handle the scenario.
{
printf("CARDS EXCEEDS LIMIT OF MAX CARDS");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.