Thread: filling a deck of cards

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    34

    filling a deck of cards

    ok so i want to write a function that fills a deck of cards and then print out the face values and their number or symbol:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    char faces[]={"0023456789TJQKA"};
    
    typedef enum
    {
    	eHearts=3,
    	eDiamonds,
    	eClubs,
    	eSpades
    }Cardsuit;
    
    typedef struct
    {
    	char faceValue;
    	Cardsuit suitValue;
    }Card;
    
    void filldeck(Card *);
    
    main()
    {	
    	char i;
    	Card Deck[52];
    	Card *deckptr;
    	deckptr=Deck;
    
    	filldeck(deckptr);
    
    	for(i=0;i<52;i++)
    	{
    		printf("%c--%c\n",faces[deckptr->faceValue],deckptr->suitValue);
    		
    	}
    
    }
    
    void filldeck(Card *deckptr)
    {
    	char i;
    	char j;
    	for(i=3;i<7;i++)
    	{
    		for(j=2;j<15;j++)
    		{
    			deckptr->suitValue=i;
    			deckptr->faceValue=j;
    		}
    		deckptr++;
    	}
    }
    this is what comes out:
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾
    A--¢¾

    Is filldeck() wrong or is it my loop that prints out? Thanks for the help

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    yeah so i dont know why but the cents 3/4 symbol is a heart in the output. thanks.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    printf("%c--%c\n",faces[deckptr->faceValue],deckptr->suitValue);
    Why are you doing this?
    Code:
    printf("%c--%c\n", faces[Deck[x].faceValue], suit[Deck[x].suitValue] );
    You don't need a pointer at all for your function. Pass it the name of the array. Furthermore, you never increment the pointer in your loop, so even if it was right, it would only ever print the same card every time.

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

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    yah i would like to just pass filldeck an array, but its mandatory that i pass it the pointer so... whatever. Thanks quzah

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    i got it working, but now i have another question:

    allright so i need a function to shuffle a deck of cards with its only parameter being deckptr:

    I was thinking i could find two random numbers ( rand()%52 ) then switch their places in the array( in a loop ) but i got confused trying to write it. Would there be an easier or a more efficient way? thanks all.

  6. #6
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    I didn't test this, but feel free to use it if it works.
    Code:
    shuffle(Card *deckptr){
        Card aux[52];
        int x, y, m = 52 / 2;
    
        for (x = 0, y = 0; x < 52; x += 2, y++){
            aux[x] = deckptr[y];
            aux[x + 1] = deckptr[m + y + 1];
        }
    
        for (x = 0; x < 52; x++)
            deckptr[x] = aux[x];
    }

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    this is what i came up with and it seems to work fine:
    Code:
    void shuffleDeck(card *deckptr)
    {
    	card temp, *crdptr;
    	int i,num;
    
    	for(i=0;i<53;i++)
    	{
    		num=rand()%52;
    		crdptr = deckptr + num;
    
    		temp = *crdptr;
    		*crdptr = *deckptr;
    		*deckptr = temp;
    
    		deckptr + 1;
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 03:22 PM
  2. Weird, cards not outputting correctly
    By Shamino in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2007, 03:26 PM
  3. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM
  4. Deck Shuffle
    By pjharris in forum C++ Programming
    Replies: 51
    Last Post: 01-06-2006, 04:59 PM
  5. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM