Hi All;
Does anyone know where there is code for a card shuffle game?
This is not a homework assignment just getting close to bed time and fancy a look before i go to pass last 20 mins by
Thanks
Hi All;
Does anyone know where there is code for a card shuffle game?
This is not a homework assignment just getting close to bed time and fancy a look before i go to pass last 20 mins by
Thanks
What do you mean by a card shuffle game? Do you want to know how to shuffle a series of data structures? Or is this some kind of game that I just haven't heard of.
Anyway - if you are looking for a shuffling algorithm, I always use a Knuth shuffle - there's pseudo code on wikipedia. It's fast, simple and effective.
shuffle has to be random to prevent card counting an all ryt?
and with no repeating patterns........but the nature of mathematics is such that everything repeats...... except pi (22/7)(3.14...) incorporate that into ur shuffle....and u hv got a killer app...
There is something called the "Fisher-Yates" algorithm that you can google. It has some variations. Here's my example:
If you are like me, you will find all the examples confusing until you write one yourself, and realize it looks like one of the examplesCode:#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> void FYshuffle (int *ray, int len) { int i, j, tmp, x; srand(time(0)); for (i=len-1;i>1;i--) { x = RAND_MAX/(i-1); j=rand()/x; printf("%d Max: %d Actual: %d\n",i,RAND_MAX/x, j); if (j==i) continue; tmp = ray[i]; ray[i] = ray[j]; ray[j] = tmp; } } int main(void) { int ray[10] = {0,1,2,3,4,5,6,7,8,9}, i; FYshuffle(ray,10); for (i=0;i<10;i++) printf("%d\n",ray[i]); return 0; }
The idea is to count backward thru the array and swap with a preceding element. You could also count forward and swap with a subsequent element I guess -- I think the calculations/necessary operations are actually simpler this way.
Of course, probably just counting thru and swapping with any other element would be random too...
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Ha ha - I was thinking, "Fisher and Yates"? That's the Knuth algorithm! Turns out they're the same - you learn something new everyday!
Fisher–Yates shuffle - Wikipedia, the free encyclopedia
I think if you google "algorithm Knuth" you will simply get a list of every other C code description on the web, so maybe that is just as well
ps. my example can be optimized by removing some variables but it is probably easier to understand this way, I hope, WRT using rand().
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge