| | |
cards
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
can anyone help me with this code i just cant understand !
its about filling a deck of cards
thx
its about filling a deck of cards
thx
C Syntax (Toggle Plain Text)
for (i = 0; i < 52; i++) { deck[i] = (i % 13)<<2; deck[i] += i / 13; if(i/13 < 2) deck[i] += 64;
DarkCoder+
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
The OP's code is also missing a closing curly bracket (}) at the end of the for loop.
This code is equivalent, but simpler:
The modulo operator is explained in detail here: http://en.wikipedia.org/wiki/Modulo_operation
This code is equivalent, but simpler:
C Syntax (Toggle Plain Text)
for (i = 0; i < 52; i++) { deck[i] = i % 13; /* i modulo 13 */ deck[i] = i * 2; deck[i] = deck[i] + i / 13; if(i/13 < 2) deck[i] = deck[i] + 64; }
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
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
wel this code fit inside a fuction which fill a deck of cards (52 cards)
knowing that ->
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).
knowing that ->
C Syntax (Toggle Plain Text)
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"};
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).
C Syntax (Toggle Plain Text)
for (i = 0; i < 52; i++) { deck[i] = (i % 13)<<2; deck[i] += i / 13; if(i/13 < 2) deck[i] += 64; }
DarkCoder+
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
If you want to shuffle the deck, you can then yse the random generator to move cards to different random positions.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Graphics cards question? (Monitors, Displays and Video Cards)
- 2 nic cards (Networking Hardware Configuration)
- Help needed In creating application to make business cards (Java)
- Myriad of Graphic Cards (Monitors, Displays and Video Cards)
- Graphic cards (Monitors, Displays and Video Cards)
- wireless net cards (*nix Hardware Configuration)
Other Threads in the C Forum
- Previous Thread: Turbo C++ explorer 2006
- Next Thread: how to skip n bits of a byte
Views: 684 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
* adobe api append array arrays bash binarysearch centimeter char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest homework i/o ide infiniteloop initialization interest intmain() kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings student suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32 win32api windows.h






