Thread: Shuffle and Deal cards in C

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    28

    Shuffle and Deal cards in C

    I'm trying to write C code to shuffle a 52 card deck with 2 character arrays. One for the suit and the other for the value. The arrays are really killing me and I've searched for about 2 hours now and just not finding what I need. I'm not sure how to shuffle the cards within the array. Professor said to try switching 2 numbers within the array by running a for loop 1000 times, but just confused. I'm guessing just initialize the arrays like this:
    Code:
    char number[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'J', 'Q', 'K', 'A' };
    char suit[] = { 'c', 'd', 'h', 's' };
    I've read about pointers? Not sure exactly what that means and saw others with "const" in front of the arrays but haven't learned anything about those yet. I honestly don't even know where to go from there. I just need to write a couple functions to shuffle and deal the cards and I can go from there. This is what I have so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    int shufflenumber(int[]);
    char shufflesuit(char[]);
    int dealnumber(int[]);
    char dealsuit(char[]);
    
    int main(void)
    {
    char number[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'J', 'Q', 'K', 'A' };
    char suit[] = { 'c', 'd', 'h', 's' };
        
      
      system("PAUSE");    
      return 0;
    }
    
    int shufflenumber(int number)
    {
    int i=0;
    srand(time(NULL));
    for(i=0; i<1000; i++)
    I'm not sure what to do within the for loop which is why it isn't completed

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can't do it with the two arrays you have. You need a single 52 element array. You could load it with the numbers 0 to 51 and interpret the card value as n % 13 + 1 and the suit as n / 13.

    E.g., the number 21 gives
    21 % 13 + 1 = 9
    21 / 13 = 1 (remember integer division truncates any fractional part)
    So the value is 9 and the suit is 1.

    You could order the suits where C, D, H, S are 0, 1, 2, 3 respectively.

    With that suit ordering, the card with the value 21 is the 9 of Diamonds.

    To shuffle an array you could certainly loop a thousand times picking two random numbers 0 to 51 and swap those elements, but that's inefficient. Another way is to simply loop through the deck once, picking a random number 0 to 51 and swapping the current index with the random one.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You have to break this down and work on one issue at a time. A standard way to shuffle (randomize) an array is the Fisher-Yates shuffle, which is fairly simple, but first you need a deck to shuffle.

    It's not clear if using those two arrays is a constraint in the assignment. If not, why do you think this is a way to proceed (notice, oogabooga's idea does not require that)?

    In any case, a deck of 52 is a deck of 52. You could use those two arrays in creating it, but the deck itself is a third entity, eg:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    const char suits[4] = { 'D', 'C', 'H', 'S' };
    const char values[13] =
    	{ '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K', 'A' };
    
    int main(void) {
    	char deck[52][3];
    	int s, v, c = 0;
    
    /* create the deck */
    	for (s = 0; s < 4; s++) {
    		for (v = 0; v < 13; v++) {
    			sprintf(deck[c], "%c%c", suits[s], values[v]);
    			printf("%s\n", deck[c]);
    			c++;
    		}
    	}
    
    	return 0;
    }
    Last edited by MK27; 04-14-2012 at 04:02 PM.
    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

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Reading up on the Fisher–Yates shuffle in Wikipedia I see that the algorithm I gave above is subtly incorrect, and in a fairly interesting way too. You shouldn't select the random element from the entire range every time, but only from what you might call the "unshuffled" range, which decreases by 1 each iteration. See the implementation in the Wikipedia article under "The modern algorithm" for the correct algorithm.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    28
    having the two arrays isn't necessary, but that's what he suggested. I'm still lost with the whole "const" before declaring the arrays, and also wondering why you added "c++" on line 17?

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    28
    also is it supposed to be "sprintf" and not "printf" or is that a mistake in line 15?

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    28
    is there a way you could write that in code? I am looking at the wikipedia page and it seriously looks like jibberish to me and I don't understand it at all

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jamdonley View Post
    is there a way you could write that in code?
    It wouldn't really do you any good for someone here to just do all the work for you. Oh sure, you'd get a good grade, but you'd still be a lousy programmer with no problem solving skills.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    28
    I'm doing this as my major. Trust me my grade is the last thing I'm worried about if I plan on doing this for a living. I need to be able to actually see and understand the code in order to do it.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    By that same logic you'd better learn mechanical engineering by watching NASCAR events.

    Soma

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    28
    All I'm asking is to help understand what this means in code

    To shuffle an array a of n elements (indices 0..n-1):
    for i from n − 1 downto 1 do
    j ← random integer with 0 ≤ ji
    exchange a[j] and a[i]

    I don't understand 0 ≤ ji and what it means and how exactly you do exchangea[j] and a[i]. I understand what it's doing by reading the page, but I don't know what to do with it. I understand where you guys are coming from, but how exactly would I understand how to do it if I don't have any code whatsoever to follow

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    don't understand 0 ≤ j ≤ i and what it means
    No math background? It means j is between 0 and i inclusive.
    how exactly you do exchangea[j] and a[i]
    Exchange the values held in those indices (j and i)?

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    28
    Yes i understand what it means, I'm saying in terms of c code. how do you exchange them? and why do you need 0 ≤ j ≤ i, what is its purpose

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jamdonley View Post
    having the two arrays isn't necessary, but that's what he suggested. I'm still lost with the whole "const" before declaring the arrays
    "const" just indicates a value can't be changed, eg:

    Code:
    const int X = 666;
    X = 0;  // compiler will not like this.
    and also wondering why you added "c++" on line 17?
    It's the equivalent of:

    Code:
    c = c + 1;
    and is performed after the rest of the operation (aka, post-increment).

    Quote Originally Posted by jamdonley View Post
    also is it supposed to be "sprintf" and not "printf" or is that a mistake in line 15?
    No mistake. If you look up sprintf(), you'll get it. Hopefully...

    Quote Originally Posted by jamdonley View Post
    is there a way you could write that in code? I am looking at the wikipedia page and it seriously looks like jibberish to me and I don't understand it at all
    That's actually why I didn't post a link to wikipedia, lol. I have to run, but here's a "fisher-yates.c" example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void FYshuffle (int *ray, int len) {
    	int i, tmp, x;
    	for (i=len-1; i>1; i--) {
    		x = rand()%i; 
    		if (x==i) continue;
    	// now swap
    		tmp = ray[i];
    		ray[i] = ray[x];
    		ray[x] = tmp;
    	}
    }
    
    int main(void) {
    	int ray[10] = {0,1,2,3,4,5,6,7,8,9}, i;
    
    	srand(time(0));
    	FYshuffle(ray,10);
    	for (i=0;i<10;i++) printf("%d\n",ray[i]);
    
    	return 0;
    }
    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

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The whole purpose is just to write down an algorithm in a concise way without assuming what language the reader cares about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with deal cards program.
    By Izzy123 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2011, 03:11 AM
  2. Replies: 5
    Last Post: 09-18-2008, 02:57 PM
  3. Shuffle and Deal Cards
    By killmequick in forum C# Programming
    Replies: 4
    Last Post: 03-21-2008, 01:30 AM
  4. Shuffle Cards and Deal
    By killmequick in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2008, 10:53 PM
  5. Card shuffle and deal want to put on Stack
    By sugie in forum C++ Programming
    Replies: 4
    Last Post: 12-12-2005, 08:40 PM