Need some guidance on a poker program

This is a discussion on Need some guidance on a poker program within the C Programming forums, part of the General Programming Boards category; Okay, here's my situation. I'm not in a programming class right now, but I'm trying to learn more on my ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    15

    Need some guidance on a poker program

    Okay, here's my situation. I'm not in a programming class right now, but I'm trying to learn more on my own. I'm going through some of the more complex stuff from "C: How to Program", 5th ed., Deitel & Deitel, which is a scholastic textbook, meaning I don't have solutions provided. I don't want someone to write code for me here, I just need a push in the right direction to know where to go.
    There's a program in the book that it keeps coming back to and building on; it starts as a card shuffling program. It uses a double subscripted array of 4 columns and 13 rows to represent the 4 suits and 13 values in each suit (or maybe it's 4 rows and 13 columns, I don't have the code in front of me but I don't guess it really matters which). The first iteration of the program "shuffles" the cards by going through and randomly seeding the numbers 1 through 52 to each value in the array, to represent each of the 52 cards in a deck...1 is the top of the deck, 52 is the bottom. The next "deals" 5 cards by using nested for loops to go through and find the numbers 1 through five. The next iteration shuffles the cards by going through the array and swapping values randomly.
    So here's the problem I'm running into. The book wants me to deal two hands (dealer and player) and compare them. It wants me to then build on that and allow me to discard cards like you would in real game of poker and to automatically have the "dealer" do it. The problem I'm having is that I don't get how to "store" the hands...like, I know on the initial deal, if I did it like a real deal in poker, my cards would be array items 1, 3, 5, 7, and 9, and the dealer's would be 2, 4, 6, 8, and 10, and those values would change according to discards and whatnot. Like if I discarded 3 cards, then my cards might be array items 1, 5, 11, 12, and 13. I'm just not sure how to have the program keep track of those. (By the way, at this point in the book, I'm not using structures, which I know would make everything much easier, but I'm trying to stay with the book). Should I use a single subscripted array to keep track of which cards I have in each hand and use loops to compare the arrays? If I could figure out a good way to keep track of each hand, comparing them and all that would be easy, I've got a pretty good idea of how to do that. I'm sure I'm just missing something obvious, but I'm stuck.
    Also, is there a more efficient way to "deal" the cards than using nested for loops? It seems horribly inneficient to me to potentially run through the brunt of a 52 item array for each card (5 passes for a 5 card hand...I do have it optimized to stop searching once it finds the card, but still).
    I hope that all makes sense.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't worry about efficiency, just yet. Follow the authors program along, (they are the experts and chose it for a reason), and learn.

    Second, make your forum posts concise, and to the point. C is a concise language, and this forum mimics that.

    I ran out of patience, long before you ran out of words. Without code, or specific timing data, or profile info of any kind, I really don't know what your problem is, let alone the solution.

    You believe the author's program is inefficient. But you must know that their program isn't meant to be efficient. It's meant to be a learning tool for it's readers.

    So no, I don't believe you have a problem. Enjoy your book's program. Learn from it. Then write your own that does just what you want.
    Last edited by Adak; 10-04-2009 at 05:50 PM.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    15
    Wow. Thank you for being not only unhelpful, but also rude.
    I have an array, call it int Deck[4][13], where the first subscript represents the 4 suits, and the second reprensents the card values from Ace through King within those suits. Given that the numbers 1 through 52 are seeded randomly through this "deck" to represent their order after being shuffled (1 being the top of the deck, 52 the bottom), what approach should I use to represent different hands so I can compare different players' hands? I'm just not sure where to begin on that part of it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So as you said you know five numbers. So store five numbers.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    15
    Okay...so lets say I store the 5 numbers I need in an array, how do I go about using those? Use if statements inside for loops to compary to array deck? I'm just a bit confused since the value of the card is represented by its location in Deck (the subscripts, that is), not the number that represents its location in a physical deck of cards.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you don't want to keep using loops, then do the loops once and store the subscripts (as in my_hand_face to store the faces and my_hand_suit to store the suits).

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    15
    Oh, of course. That makes a lot of sense. Now I feel a bit silly.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    15
    Thank you, by the way.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm being honest, and that is *very* helpful. Posts like your first one, don't get many replies, let alone helpful one's. If you need your advice sugar-coated, go have a donut.

    Because of the way I think about a poker hand, I would recommend, a struct for each hand.

    Tip:
    If you look at the lower end of the ascii character chart, there are card suit symbols there. Those can be used to print up a representation of a card, even though you're still in console (text) mode.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    15
    Yeah, I realize that structs would be easiest, but the book isn't using them at this point, and I'm trying to stick with the book.

  11. #11
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    wish i had the older Deital Deital files on line ..wait i might have it on here..thou dont expect it to be amything fancy ..it was code writting way back when i was much younger ..

    Code:
    /***********************************************************
      *
      *   Program:    Poker Game
      *   Created:    09/12/02 03:38:AM
      *   Author:     Karan Sandhu
      *   Requested:
      *   Comments:   A small and simple poker game.
      *
      ************************************************************/
    
    
    
    #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
    
    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[] );
    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};
    
        srand( time( NULL ) );
    
    	printf( "--------PKœR--------\n" );
    
         printf("---------               ---------\n"
    			"|       |               |       |\n"
    			"|         |               |         |\n"
    			"|        |               |        |\n"
    			"|         |               |         |\n"
    			"|       |   Presented   |       |\n"
    			"---------      By       ---------\n");
    	 printf("            Datanjector             \n");
    	 printf("---------               ---------\n"
    			"|       |               |       |\n"
    			"|         |               |         |\n"
    			"|        |               |        |\n"
    			"|         |               |         |\n"
    			"|       |               |       |\n"
    			"---------               ---------\n");
        printf ("Press <return> to continue with the game.....");
    	getchar();
       	system("cls");
        shuffle( deck );
    	deal( deck, hand, HAND_SIZE );
    	showhand( hand, HAND_SIZE, suit, face );
        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[] )
    {
        int card, suit, face, hand_num;
    
    for ( hand_num =0; hand_num < 2; hand_num++ )
    {
        for(card = 0 ; card < hand_size ; card++) {
            suit = getSuit( hand[hand_num][card] );
            face = getFace( hand[hand_num][card] );
    
    	 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 ("You have a pair of %s\n", suits[s1]);
    				++point[hand_num];
    			}
    			else if ( store[hand_num][s1] == 4 )
    			{
    				printf( "And another pairs of %s\n", suits[s1] );
    			   	point[hand_num] += 2;
    			}
    			else if ( store[hand_num][s1] == 5)
    			{
    				printf( "Congatulation 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("\nHand 1 wins!!\n");
    	else if ( point[0] < point[1] )
    		printf("\nHand 2 wins!!\n");
    	else
    		printf ("\nIts a Draw!!\n");
    }

    still i dont belive i am a very good programmer as most people in here are ..

    i cant dedicate y time because i spend most of the time struggling with addiction ..but hey .... check my posts ..i was following the book you are .so you might find some help from my older posts..

    I agree with the author above ..who mentions about first learning the basics and then think of things like opitmizatin ..else you might waste time ..its well known fact ..learn the foundation ..trhe better the foundation belove me ..when you dont have to relear the foundation anything that builds on it would be solid ..
    Last edited by datainjector; 10-07-2009 at 12:22 AM.
    "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. Why doesn't this poker C program work?
    By hermancarson in forum C Programming
    Replies: 5
    Last Post: 04-09-2008, 10:58 AM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21