Can someone explain this to me?
typedef struct
{
char *value;
char *suit;
}card;
void fillDeck(card *Deck, char *Value[], char *Suit[])
{
int i;
for(i=0;i%26lt;52;i++)
{
Deck[i].value=Value[i%13];
Deck[i].suit=Suit[i/13];
}
}
void shuffle(card *Card)
{
int i, j;
card temp;
for(i=0;i%26lt;52;i++)
{
j=rand()%52;
temp=Card[i];
Card[i]=Card[j];
Card[j]=temp;
}
}
void display(card *Deck)
{
int i;
for(i=0;i%26lt;52;i++)
printf("%s %s\t", Deck[i].value, Deck[i].suit);
printf("\n\n");
}
int main()
{
card deck[52], card[52], discard;
int i, j, k, loop;
char ans;
char *value[]={"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"};
char *suit[]={"C", "S", "H", "D"};
srand(time(NULL));
fillDeck(deck,value,suit);
shuffle(deck);
display(deck);
}
This is a code that shuffles a deck of cards, now, can you explain what the subprogram does? I can't understand the fillDeck subprogram, pls help...
C language help!?
Hmm, I think fillDeck is doing just that, filling the entire deck. When you start your main program "main()", you have 2 arrays.
1) every card
2) every suit,
However, if you kept going (without fillDeck) you wouldn't have any cards at all. You have to fill the entire "deck" with 13 cards of each suit...
A, 1, 2 ... J,Q,K of Clubs, then Spades, then Hearts and finally Diamonds. It just creates all 4 suits, all 13 cards for a total of 52 cards in the deck...You can confirm this if you set a "Watch" or "Step" through as it gets called...
HTH
Reply:The program is written to shuffle a deck of playing cards.
Strcture card contains the information about value of each card and the suit it belongs to.
Function fillDeck fills the deck i.e. it assigns the value %26amp; suit to each of the 52 cards. For 0%26lt;i%26lt;52 , (i%13) gives modulus of division i.e. a number from 0 to 12 which represents a value from *value[] defined in main(). Also, i/13 represents a no from 0 to 3 which gives the suit for a card numbered i from *suit[] in main(). In this way, the cards numbered from 0 to 51 are assigned a value from A to K and every group of 13 cards is assigned a suit from C,S,H %26amp; D.
In function shuffle, rand() returns a random no..So, rand()%52 gives us any no. from 0 to 51. The card at this position and the card at ith position are interchanged. The process is repeated for 52 times due to for loop to shuffle the cards at random.
Reply:first of all , fillDeck is a function(that's what they call in C:))Anyway , fillDeck takes a pointer to an array of cards called *Deck , a pointer to the array of characters called Value and Suits.Then it fills the struct card's fields with:
for(i=0;i%26lt;52;i++)
{
Deck[i].value=Value[i%13];
Deck[i].suit=Suit[i/13];
}
that's it , i hope iam useful
bridal flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment