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

Printable View

- 08-13-2009newbie30Card shuffle game
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 - 08-13-2009sean
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. - 08-13-2009hackterr
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... - 08-13-2009MK27
There is something called the "Fisher-Yates" algorithm that you can google. It has some variations. Here's my example:

Code:`#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... - 08-13-2009sean
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 - 08-13-2009MK27
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().