943,664 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 852
  • C RSS
Aug 21st, 2007
0

cards

Expand Post »
can anyone help me with this code i just cant understand !
its about filling a deck of cards
thx
  1. for (i = 0; i < 52; i++)
  2. {
  3. deck[i] = (i % 13)<<2;
  4. deck[i] += i / 13;
  5. if(i/13 < 2)
  6. deck[i] += 64;
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Aug 21st, 2007
0

Re: cards

Yes. As i understand it's something about card game, these calculations are likely used for generating some random numbers. The % operator gives the remainder, so i % 13 always gives a number from 0 to 12, if i is positive, no matter what value i has. The << operator increases the number by shifting it left by one binary place, which means that << 1 is the same as multiplying by 2, << 2 the same as multiplying by 4, but a left shift is much faster than multiplying. What this gives is a random number which changes by a certain bigger unit, say by 4, which is sometimes necessary for a certain random behaviour. And then, i += i / 13 is just the same as i = i + i / 13...
Last edited by TkTkorrovi; Aug 21st, 2007 at 3:02 pm.
Reputation Points: 85
Solved Threads: 13
Junior Poster
TkTkorrovi is offline Offline
170 posts
since Mar 2005
Aug 21st, 2007
0

Re: cards

The OP's code is also missing a closing curly bracket (}) at the end of the for loop.

This code is equivalent, but simpler:
  1. for (i = 0; i < 52; i++)
  2. {
  3. deck[i] = i % 13; /* i modulo 13 */
  4. deck[i] = i * 2;
  5. deck[i] = deck[i] + i / 13;
  6. if(i/13 < 2)
  7. deck[i] = deck[i] + 64;
  8. }
The modulo operator is explained in detail here: http://en.wikipedia.org/wiki/Modulo_operation
Last edited by dwks; Aug 21st, 2007 at 4:35 pm.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Aug 21st, 2007
0

Re: cards

I see now that this code doesn't generate random numbers, but some kind of distribution, still the same applies.
Reputation Points: 85
Solved Threads: 13
Junior Poster
TkTkorrovi is offline Offline
170 posts
since Mar 2005
Aug 22nd, 2007
0

Re: cards

wel this code fit inside a fuction which fill a deck of cards (52 cards)
knowing that ->
  1. static char *s[] = {"Hearts","Diamonds","Clubs","Spades"};
  2. static char *v[] = {"Ace","Two","Three","Four","Five","Six",\
  3. "Seven","Eight","Nine","Ten","Jack",\
  4. "Queen","King"};
  5. 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).
  1. for (i = 0; i < 52; i++)
  2. {
  3. deck[i] = (i % 13)<<2;
  4. deck[i] += i / 13;
  5. if(i/13 < 2)
  6. deck[i] += 64;
  7. }
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Aug 23rd, 2007
0

Re: cards

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, if i 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.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Turbo C++ explorer 2006
Next Thread in C Forum Timeline: how to skip n bits of a byte





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC