Thread: Shuffle and Deal Cards

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

    Shuffle and Deal Cards

    I need to make a program that shuffles a deck of cards and simply deals them into 4 hands
    as soon as the program starts it will list something like this:
    D9 H5 S10 C4 DJ C10 DA H2 D8 H10 D7 S9 HQ
    and 3 more rows of 13

    i have got this working correctly but is there anyway i could do it with a double array without using the constant characters. here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>
    
    void shuffle( int wdeck[][13]);
    void deal( const int wdeck[][13], const char *wface[],
    	 const char *wsuit[]);
    void pair( int a[]);
    	
    		  
    int main()
    {
    	const char *suit[4] = {"H", "D", "C", "S"};
    
    	const char *face[13] = {"A", "2", "3", "3", "5",
    		"6", "7", "8", "9", "10", "J", "Q", "K"};
    
    	int deck[4][13] ={0};
    
    	srand( time(0));
    
    	shuffle(deck);
    	deal(deck, face, suit);
    
    
    	return 0;
    
    }
    
    void shuffle(int wdeck[][13])
    {
    	int row;
    	int column;
    	int card;
    
    	for(card = 1; card <= 52; card++) {
    		do {
    			row = rand() % 4;
    			column = rand() % 13;
    		}while(wdeck [row] [column] !=0);
    
    		wdeck [row] [column] = card;
    	}
    }
    
    void deal(const int wdeck[][13], const char *wface[], 
    		  const char *wsuit[])
    {
    	int card;
    	int row;
    	int column;
    
    
    	for ( card = 1; card <= 52; card++ ) {
    		
    		for ( row = 0; row <= 3; row++ ){
    
    			for ( column = 0; column <=12; column++ ){
    
    				if ( wdeck[row][column] == card ) {
    
    					printf("%5s%-8s%\n", wface[column], wsuit[row],
    					card % 52 == 0 );
    					
    					
    				}		
    			}
    		}
    	}_getch();
    }

  2. #2
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    This is C++.

    Please ask a moderator to move it to the C++ forum, or delete it and repost it there.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Actually - this is C

    On the C board I have just now answered question - how to allocated 2D dynamic array base on user input
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    where have you done this array

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shuffle Cards and Deal
    By killmequick in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2008, 10:53 PM
  2. Deck Shuffle
    By pjharris in forum C++ Programming
    Replies: 51
    Last Post: 01-06-2006, 04:59 PM
  3. 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
  4. Creating a blackjack game
    By JoshR in forum Game Programming
    Replies: 4
    Last Post: 04-29-2005, 11:27 PM
  5. I need some help with dealing cards.
    By collegegal in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 10:13 PM