cards
can anyone help me with this code i just cant understand !
its about filling a deck of cards
thx
for (i = 0; i < 52; i++)
{
deck[i] = (i % 13)<<2;
deck[i] += i / 13;
if(i/13 < 2)
deck[i] += 64;
rowly
Junior Poster in Training
65 posts since Sep 2006
Reputation Points: 10
Solved Threads: 3
wel this code fit inside a fuction which fill a deck of cards (52 cards)
knowing that ->
static char *s[] = {"Hearts","Diamonds","Clubs","Spades"};
static char *v[] = {"Ace","Two","Three","Four","Five","Six",\
"Seven","Eight","Nine","Ten","Jack",\
"Queen","King"};
static char *c[]= {"Black","Red"};
actually i cant understand the logic behind this function
is it like creating a memory space for 52 cards so we can deal with it later or just creating cards...
because there is 2 other functions that show you the deck, shuffle the deck and print the deck again(shuffled).
for (i = 0; i < 52; i++)
{
deck[i] = (i % 13)<<2;
deck[i] += i / 13;
if(i/13 < 2)
deck[i] += 64;
}
rowly
Junior Poster in Training
65 posts since Sep 2006
Reputation Points: 10
Solved Threads: 3
Actually, I don't understand it, either. The shifting and adding 64 makes no sense.
Since a deck has 52 cards, the loop is going through all the possible cards of the deck (0-51). Then (i / 13) gives you 0-4, representing the suit of the given card. (i % 13) gives the value or rank of the card (0-12). Therefore, ifi is 5 you get the 6 of Hearts.
If you want to shuffle the deck, you can then yse the random generator to move cards to different random positions.
WaltP
Posting Sage w/ dash of thyme
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943