Thread: Card shuffle game

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    Card 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

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  3. #3
    Registered User hackterr's Avatar
    Join Date
    Aug 2009
    Location
    INDIA
    Posts
    19
    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...

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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;
    }
    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 examples

    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

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sean View Post
    Ha ha - I was thinking, "Fisher and Yates"? That's the Knuth algorithm! Turns out they're the same - you learn something new everyday!
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  2. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. Card game help
    By aaroroge in forum Game Programming
    Replies: 9
    Last Post: 07-16-2005, 06:37 PM
  5. Replies: 2
    Last Post: 11-07-2003, 12:21 AM