cards

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

cards

 
0
  #1
Aug 21st, 2007
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;
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 170
Reputation: TkTkorrovi is on a distinguished road 
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: cards

 
0
  #2
Aug 21st, 2007
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.
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: cards

 
0
  #3
Aug 21st, 2007
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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 170
Reputation: TkTkorrovi is on a distinguished road 
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: cards

 
0
  #4
Aug 21st, 2007
I see now that this code doesn't generate random numbers, but some kind of distribution, still the same applies.
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: cards

 
0
  #5
Aug 22nd, 2007
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. }
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: cards

 
0
  #6
Aug 23rd, 2007
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 684 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC