Thread: Card Dealing program help

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    6

    Card Dealing program-please help again!

    I have a problem due in school. I have the shuffle and deal all set. I need some help on how to determine if you have a pair, two pairs, flush, etc. I should use pointers and arrays. Any help would be appreciated. I think I can attach what I have so far (this is my first time). Thanks again!
    Last edited by Randoon; 11-21-2002 at 06:58 PM.

  2. #2
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    you just hit the jackpot ...ahh where is my POT

    Code:
    /*Poker game: written by Datainjector*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define NUM_SUITS   4
    #define NUM_FACES   13
    #define HAND_SIZE   5
    #define NUM_HAND 	2
    #define POINT_SIZE	2
    #define NAMESIZE    20
    
    void shuffle( int deck[][ NUM_FACES ] );
    void deal( int deck[][ NUM_FACES ], int hand[][ HAND_SIZE ], int hand_size );
    void showhand( const int hand[][ HAND_SIZE ], int hand_size,
                    const char *suits[], const char *faces[], char name[] );
    void smartComp( int hand[][ HAND_SIZE ], int wDeck[][ NUM_FACES ] );
    void smartPlay( int hand[][ HAND_SIZE ], int wDeck[][ NUM_FACES ] );
    void printCard( const int hand[][ HAND_SIZE ], int hand_size,
    				const char *suits[], const char *faces[], char name[] );
    void getPair( int hand[][ HAND_SIZE ], int hand_size, const char *suits[], int point[] );
    void getPairFace( int hand[][ HAND_SIZE ], int hand_size, const char *face[], int point[] );
    int getSuit( int Card );
    int getFace( int Card );
    void winner( int point[] );
    
    int main()
    {
        const char *suit[ NUM_SUITS ] =
          { "Hearts", "Diamonds", "Clubs", "Spades" };
        const char *face[ NUM_FACES ] =
          { "Ace", "Deuce", "Three", "Four",
            "Five", "Six", "Seven", "Eight",
            "Nine", "Ten", "Jack", "Queen", "King" };
        int deck[ NUM_SUITS ][ NUM_FACES ] = { { 0 } };
        int hand[ NUM_HAND ][ HAND_SIZE ];
    	int point[ POINT_SIZE ] = { 0 };
        char name[NAMESIZE];
        int i;
        srand( time( NULL ) );
    	/*-----------------9/21/02 2:58:M-------------------
    	 * Well alil show of .Tried to make some
    	 * Card.Yeah i know Some card !!!
    	 * --------------------------------------------------*/
    
    
        for ( i =1; i <= 80; i++ )
            printf("_");
        printf("\n\n\n");
    	printf( "\t\t--------PKœR--------\n" );
    
         printf("\t\t©---------ª               ©---------ª\n"
    			"\t\t|       |               |       |\n"
    			"\t\t|         |               |         |\n"
    			"\t\t|        |               |        |\n"
    			"\t\t|         |               |         |\n"
    			"\t\t|       |    Written    |       |\n"
    			"\t\tÀ---------Ù      By       À---------Ù\n");
    	 printf("\t\t            Datanjector             \n");
    	 printf("\t\t©---------ª               ©---------ª\n"
    			"\t\t|       |               |       |\n"
    			"\t\t|         |               |         |\n"
    			"\t\t|        |               |        |\n"
    			"\t\t|         |               |         |\n"
    			"\t\t|       |               |       |\n"
    			"\t\tÀ---------Ù               À---------Ù\n");
        printf("\n\n");
    
        printf( "Type in you name below.\n" );
        for ( i =1; i <= 80; i++ )
            printf("_");
    
        scanf("%s", name );
        printf( "Press <return> to continue with the game....." );
    	getchar();
       	system( "cls" );
        shuffle( deck );
    	deal( deck, hand, HAND_SIZE );
    	printf( "   These symbols %c %c shows that the cards arnt visible to you\n",'±','°' );
    	puts("\n");
    	showhand( hand, HAND_SIZE, suit, face, name );
    	smartComp( hand, deck );
    	smartPlay( hand, deck );
    	system( "cls" );
    	printf( "   These symbols %c %c shows that the cards arnt visible to you\n",'±','°' );
    	puts( "\n" );
    	printCard( hand, HAND_SIZE, suit, face,name );
        getPair( hand, HAND_SIZE, suit, point );
    	getPairFace( hand, HAND_SIZE, face, point );
    	puts( " " );
    	winner ( point );
    
        system( "pause" );
        return 0;
    }
    
    void shuffle( int wDeck[][ NUM_FACES ] )
     {
        int row, column, card;
    
        for(card = 1; card <= NUM_FACES*NUM_SUITS; card++) {
            do {
                row = rand( ) % NUM_SUITS;
                column = rand( ) % NUM_FACES;
            } while(wDeck[ row ][ column ] != 0);
    
            wDeck[ row ][ column ] = card;
        }
    }
    
    void deal( int wDeck[][ NUM_FACES ], int hand[][ HAND_SIZE ], int hand_size )
    {
        int card, row, column, hand_num;
    
    for (hand_num = 0; hand_num < 2; hand_num++ )
    {
        for(card = 0; card < hand_size; card++) {
            do {
                row = rand( ) % NUM_SUITS;
                column = rand( ) % NUM_FACES;
            } while(wDeck[ row ][ column ] == 0);
    		hand[ hand_num ][ card ] = wDeck[ row ][ column ];
    
    		wDeck[ row ][ column ] = 0; /* this card has been dealt */
        }
    }
    }
    
    void showhand( const int hand[][ HAND_SIZE ], int hand_size,
                    const char *suits[], const char *faces[] , char name[])
    {
        int card, suit, face, hand_num;
    
    for ( hand_num = 0; hand_num < 2; hand_num++ )
    {
    	if (hand_num == 0 )
    		printf( "Dealer's hand:\n" );
    	else
    		printf( "%s's hand:\n", name );
    
        for( card = 0 ; card < hand_size; card++ )
    	 {
            suit = getSuit( hand[ hand_num ][ card ] );
            face = getFace( hand[ hand_num ][ card ] );
       if ( hand_num == 1 )
       	printf( "%5s of %-8s%c",
                faces[ face ], suits[ suit ], '\n'  );
    
       else
       	printf( "%5s of %-8s%c",
    			"±±±±±±","°°°°°°°", '\n'  );
    
    	}
    		printf( "\n" );
    }
    }
    /* This Makes the computer Act Smarter */
    /*After some thinking i cam up with this algortithm of doing it */
    void smartComp( int hand[][ HAND_SIZE ], int wDeck[][ NUM_FACES ] )
    {
    	int cnt_main, cnt_sub, zero = 0, suit, face, hold;
    	int sa[ 5 ];
    	int cardCapture[ 5 ] = { 0 };
    
    	for ( cnt_main = 0; cnt_main < 5; cnt_main++ )
    			sa[cnt_main] = getSuit( hand[zero][cnt_main] );
    
    	for ( cnt_main = 0; cnt_main < 5; cnt_main++ )/* A nice trick */
    		++cardCapture[ sa[ cnt_main ] ];      /* Tell you how many times the same card is in the hand */
    
    	for ( cnt_main = 0; cnt_main < 5; cnt_main++ )
    	{
    		suit = rand( ) % NUM_SUITS;
    		face = rand( ) % NUM_FACES;
    		if ( cardCapture[ sa [ cnt_main ] ] > 1 )
    			;
    		else
    		{
    			if ( wDeck[ suit ][ face ] == 0 )
    				;
    			else
    			{
    				hold = hand[ zero ][ cnt_main ];
    			    hand[ zero ][ cnt_main ] = wDeck[ suit ][ face ];
    			    wDeck[ suit ][ face ] = hold;
    			}
    	   	}
    	}
    }
    /*This helps the player choose the card he wants to swap */
    void smartPlay( int hand[][ HAND_SIZE ], int wDeck[][ NUM_FACES ] )
    {
    	int cnt_main=0, userInput, hold, one = 1;
    	int suit, face;
    
    	printf( "You arn't allowed to swap more than two cards .Remember --> (To End Press -1).\n" );
    	printf( "-----------------------------------------------------------------------------\n" );
    
    	printf( "\nEnter the card number to change.(Card No. {%d} ): ", cnt_main+1 );
    	scanf( "%d", &userInput );
    	while ( ( ( ++cnt_main != 2 ) && (userInput != -1 ) ) )
    	{
    	 	suit = rand( ) % NUM_SUITS;
    		face = rand( ) % NUM_FACES;
    
    		if ( wDeck[ suit ][ face ] == 0 )
    		   ;
    		else
    		{
    			hold = hand[ one ][ userInput - 1 ];
    			hand[ one ][ userInput - 1 ] = wDeck[ suit ][ face ];
    			wDeck[ suit ][ face ] = hold;
    		}
    
    		printf( "Enter the card number to change.(Card No. {%d} ): ", cnt_main+1 );
    		scanf( "%d", &userInput );
    
    	}
    }
    /*Prints the Cards out this time with the dealers cards up */
    void printCard( const int hand[][ HAND_SIZE ], int hand_size,
                    const char *suits[], const char *faces[], char name[] )
    {
        int card, suit, face, hand_num;
    
    for ( hand_num = 0; hand_num < 2; hand_num++ )
    {
    	if (hand_num == 0 )
    		printf( "Dealer's hand:\n" );
    	else
    		printf( "%s's hand:\n", name );
    
        for( card = 0 ; card < hand_size; card++ )
    	 {
            suit = getSuit( hand[ hand_num ][ card ] );
            face = getFace( hand[ hand_num ][ card ] );
       if ( hand_num == 1 )
       	printf( "%5s of %-8s%c",
                faces[ face ], suits[ suit ], '\n'  );
    
       else
       	printf( "%5s of %-8s%c",
    			faces[ face ], suits[ suit ], '\n'  );
    
    	}
    		printf( "\n" );
    }
    }
    
    
    
    void getPair( int hand[][ HAND_SIZE ], int hand_size,
    			 const char *suits[], int point[] )
     {
        int cnt_main, hand_num;
        int s1;
       	int store[ 2 ][ 5 ] = { 0 };
    
    
    for ( hand_num = 0; hand_num < 2; hand_num++ )
    {
        for( cnt_main = 0; cnt_main < hand_size; cnt_main++ )
    	 {
                s1 = getSuit( hand[hand_num][cnt_main] );
    			++store[ hand_num ][ s1 ];
    
    			if ( store[ hand_num ][ s1 ] == 2 )
    			{
    				printf( "\nYou have a pair of %s", suits[s1] );
    				++point[ hand_num ];
    			}
    			else if ( store[ hand_num ][ s1 ] == 4 )
    			{
    				printf( "\nAnd another pair of %s", suits[s1] );
    			   	point[ hand_num ] += 2;
    			}
    			else if ( store[ hand_num ][ s1 ] == 5)
    			{
    				printf( "\nCongatulation you got a flush" );
    				point[ hand_num ] += 5;
    			}
    
    
    
    	 }
       				printf ("\n");
    
    
    }
    
    }
    void getPairFace ( int hand[][HAND_SIZE], int hand_size,
    					const char *face[], int point[] )
    {
    	int cnt_main, hand_num;
    	int s1;
    	int store[2][13] = {0};
    
    for ( hand_num =0; hand_num < 2; hand_num++ )
    {
    
    	for ( cnt_main = 0; cnt_main < hand_size; cnt_main++ )
    	{
    			s1 = getFace ( hand[hand_num][cnt_main] );
    			++store[hand_num][s1];
    
    				if ( store[hand_num][s1] == 3 )
    				{
    					printf ("You have three %s\n", face[s1]);
    					point[hand_num] += 3;
    				}
    				else if ( store[hand_num][s1] == 4 )
    				{
    					printf ("You have four %s\n", face[s1]);
    					point[hand_num] += 4;
    				}
    				else if ( store[hand_num][s1] == 5 )
    				{
    					printf( "You have a Straight\n" );
    					point[hand_num] += 10;
    				}
    
    		    }
    
    	}
    }
    
    
    int getSuit( int Card ) {
        return ( Card-1 )/NUM_FACES;
    }
    int getFace( int Card ) {
        return ( Card-1 )%NUM_FACES;
    }
    
    void winner ( int point[] )
    {
    	if ( point[0] > point[1] )
    		printf("Dealer wins!!\n");
    	else if ( point[0] < point[1] )
    		printf("Player wins!!\n");
    	else
    		printf ("Its a Draw!!\n");
    }
    /*-----------------9/21/02 3:00:M-------------------
     * Die!!I said die..Yeah...right jesus is gonna save
     * ya sorry ass from me eh?  KAZDAX
     *--------------------------------------------------*/
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with this last project! Credit Card Program!
    By ClearSights in forum C++ Programming
    Replies: 18
    Last Post: 10-27-2008, 10:08 AM
  2. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 04:26 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM