Thread: Card Shuffle Help

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    43

    Card Shuffle Help

    i need to shuffle an array of cards and simply print them in a simple order but i dont know where to go from here, please help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    int main()
    {
    	char suit[4][0]={"Hearts", "Diamonds", "Spades", "Clubs"};
    	char deck[51] {"H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "HJ", "HQ", "HK", "HA", 
                   "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "DJ", "DQ", "DK", "DA", 
    	       "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "SJ", "SQ", "SK", "SA", 
    	       "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "CJ", "CQ" "CK", "CA"};
    	int card;
    	
    	
    _getch();
    return 0;
    }
    Last edited by Salem; 04-02-2008 at 09:17 AM. Reason: Folded!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    FYI.

    Most C compilers ignore white space, which means as a courtesy to us all, you can split long lines and make it multiple short lines.

    And the last time I counted, there were 52 cards in a deck.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    To shuffle the deck you can swap positions in the array something like:
    Code:
    #include <stdlib.h>
    #include<ctime.h>
    
    int main()
    {
        srand(time(NULL)); //Seed randomisation
    
        int deck[52];
        int i, temp, random;
        // Init deck
        for(i=0; i<52; i++)
             deck[i] = i;
        //Shuffle deck
        for(i=0; i<52; i++)
        {
             random = rand() % 52;
             temp = deck[ i ];
             deck[ i ] = deck[ random ];
             deck[ random ] = temp;
        }
    }
    Also you can use an integer value to hold the card. card value % 13 will get the rank and card value / 13 the suit. You could then get the relevant text using functions something like:
    Code:
    const char* GetSuit(Uint8 card)
    
    {
    
        static const char *suit[] = {"Clubs", "Diamonds", "Hearts", "Spades"};
    
        return suit[card/13];    
    
    }
    
    
    
    const char* GetRank(Uint8 card)
    
    {
    
        static const char *rank[] = {"Ace", "Two", "Three", "Four", "Five", 
    
            "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    
        return rank[card%13];  
    
    }

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    i have this but it doesnt seem to work i have to terminate the program and it comes up with lots of errors.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main()
    {
        srand(time(NULL)); //Seed randomisation
    
        int deck[52];
        int i, temp, random;
        // Init deck
        for(i=0; i<52; i++)
             deck[i] = i;
        //Shuffle deck
        for(i=0; i<52; i++)
        {
             random = rand() % 52;
             temp = deck[ i ];
             deck[ i ] = deck[ random ];
             deck[ random ] = temp;
    		 printf("%s", deck[i]);
    		
        }
    return 0;
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't print an integer using %s.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    well heres the problem now, i can get it to display random numbers 1 through 52 but they should not repeat and also must be like a deck of cards with Jacks queens kings and aces

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your shuffle doesn't repeat numbers. You need to print outside the shuffle loop.

    And everyone else is going to tell you what mike_g told you (about getting names), so I'm not even going to type it -- just look up and pretend I said it again, but louder this time.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    from here how could i make it so each card takes a suit?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>
    
    
    int main()
    {
        srand(time(NULL)); //Seed randomisation
    
    	char suit[4][0]= {"Hearts", "Spades", "Clubs", "Diamonds"}
    	char rank[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    	    int deck[52];
    		
        int i, temp, random;
        // Init deck
        for(i=0; i<52; i++)
             deck[i] = i;
        //Shuffle deck
        for(i=0; i<52; i++)
        {
             random = rand() % 52;
             temp = deck[i];
             deck[i] = deck[ random ];
             deck[ random ] = temp;		 
    		
        }
    	printf("%d", deck[i]);
    _getche();
    return 0;
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by killmequick View Post
    from here how could i make it so each card takes a suit?
    And everyone else is going to tell you what mike_g told you (about getting names), so I'm not even going to type it -- just look up and pretend I said it again, but even louder this time.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by killmequick View Post
    Code:
    char suit[4][0]= {"Hearts", "Spades", "Clubs", "Diamonds"}
    char rank[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    I don't expect either of those array declarations to work. The first declares an array of zero length which is not legal. The second declares an array of characters of unknown length instead of a 2D array of chars, or an array of pointers to chars.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    so how would i write an array that works

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    i am not supposed to use constant characters with this program

  13. #13
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Why not? contsant char would be more suitable since you wont want to be editing the content. Never the less, all you would have to do is copy the code from the functions I posted and remove the 'const' part. IE:
    Code:
    int main()
    {
        char *suit[] = {"Clubs", "Diamonds", "Hearts", "Spades"};
    }

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    The simplest way is to generate a randon number between 1 and 51, then split the array into
    two at that point and then make a new array, with the botton half of the split above the top
    half, then copy the result back to the original array. Repeat as necessary. That is how a real shuffle is done.
    I guess you could mix this with 'fancy shuffles' where you split the pack in to to halfs and then merge them
    into the order (for halfs 'a'' and 'b') a[0], b[0], a[1]], b[1], a[2], b[2], etc....



    Or perhaps quicker and simpler, select a card at a random position and put it into a new array
    in first position, then select another and put it the next position. If the card has already been selected use the first unselected card after it in the array. That is probably the fastest way. But is gets less effiecient towards the end.
    Last edited by esbo; 04-02-2008 at 06:32 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I disagree - the algorithm mike suggested is the Knuth shuffle, which is a good shuffle because every card has an equal probability to be swapped with every other card. It's difficult to implement correctly, but I doubt it's any less efficient than what you've suggested. And in all honesty, I'd rather use a documented algorithm for shuffling. I don't think my feelings are that exceptional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  3. Vector out of range program crash.
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 01-18-2008, 05:37 PM
  4. Segmentation Fault - aaaaaaaah!
    By yogibear in forum C Programming
    Replies: 6
    Last Post: 10-01-2007, 03:21 AM
  5. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM