Thread: Help with a Poker problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    Help with a Poker problem

    I've been working on a problem that simulates a poker hand. The program shuffles a standard deck of 52 cards and then deals a 5-card poker hand. Then it evaluates what kind of hand you have. Here's what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define NUM_FACES 13
    #define NUM_SUITS 4
    #define NUM_CARDS 5
    #define TRUE 1
    #define FALSE 0
    
    typedef int bool;
    
    bool straight, flush, four, three;
    int pairs;   /* can be 0, 1, or 2 */
    
    void shuffle( int wDeck[][ 13 ] );
    void deal( int wDeck[][ 13 ], char *wFace[], char *wSuit[] );
    void analyze_hand(int [], int []);
    void print_result(void);
    
    int getSuit( int Card );
    int getFace( int Card );
    
    int main()
    {
    	char *Suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
    	char *Face[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
    		"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
    
    	int deck[ 4 ][ 13 ] = { 0 };
    	int num_in_Face[NUM_FACES];
        int num_in_Suit[NUM_SUITS];
    
    	srand( time( 0 ) );
    
        shuffle( deck );
    	deal( deck,  Face, Suit );
    	analyze_hand(num_in_Face, num_in_Suit);
    
    	return 0;
    }
    
    void shuffle( int wDeck[][ 13 ] )
    {
    	int Suit;
    	int Face;
    	int card;
    
    	for ( card = 1; card <= 5; card++ )
    	{
    		do
    		{
    			Suit = rand() % 4; Face = rand() % 13;
    		} while( wDeck[ Suit ][ Face ] != 0 );
    
    		wDeck[ Suit ][ Face ] = card;
    	}
    }
    
    
    void deal( int wDeck[][ 13 ], char *wFace[], char *wSuit[] )
    {
    	int card;
    	int Suit;
    	int Face;
    
    	for ( card = 1; card <= 5; card++ )
    	{
    		for ( Suit = 0; Suit <= 3; Suit++ )
    		{
    			for ( Face = 0; Face <= 12; Face++ )
    			{
    				if ( wDeck[ Suit ][ Face ] == card )
    				{
    					printf( "%5s of %-8s%c", wFace[ Face ], wSuit[ Suit ], card % 2 == 0 ? '\n' : '\t' );
    				}
    			}
    		}
    	}
    }
    
    void analyze_hand(int num_in_Face[], int num_in_Suit[])
    {
      int num_consec = 0;
      int Face, Suit;
    
      straight = FALSE;
      flush = FALSE;
      four = FALSE;
      three = FALSE;
      pairs = 0;
    
      /* check for flush */
      for (Suit = 0; Suit < NUM_SUITS; Suit++) 
      {
    	  if (num_in_Suit[Suit] == NUM_CARDS) {
    		flush = TRUE;
    	  }
      }
    
      /* check for straight */
      Face = 0;
      while (num_in_Face[Face] == 0) Face++;
      {
    	for (; Face < NUM_FACES && num_in_Face[Face]; Face++)
    		  num_consec++;
    	{
    		if (num_consec == NUM_CARDS) {
    			straight = TRUE;
    			return;
    		}
    	}
      }
    
      /* check for 4-of-a-kind, 3-of-a-kind, and pairs */
      for (Face = 0; Face < NUM_FACES; Face++) {
    	  if (num_in_Face[Face] == 4) {
    		four = TRUE;
    	  }
    	  if (num_in_Face[Face] == 3) {
    		three = TRUE;
    	  }
    	  if (num_in_Face[Face] == 2) {
    		pairs++;
    	  }
      }
      if (straight && flush) {
    		printf("You have a Straight Flush.\n\n");
    	}
    	else if (four) {
    		printf("You have Four of a Kind.\n\n");
    	}
    	else if (three && pairs == 1) {
    		printf("You have a Full House.\n\n");
    	}
    	else if (flush) {
    		printf("You have a Flush.\n\n");
    	}
    	else if (straight) {
    		printf("You have a Straight.\n\n");
    	}
    	else if (three) {
    		printf("You have Three of a Kind.\n\n");
    	}
    	else if (pairs == 2) {
    		printf("You have two pairs.\n\n");
    	}
    	else if (pairs == 1) {
    		printf("You have one pair.\n\n");
    	}
    	else {
    		printf("You have a runt.\n\n");
    	}
    }
    When I ran the program, it compiled and executed perfectly except for one detail. Every single hand I play, it says "You have a runt" even when I do indeed have a poker hand. Here's one example:

    Seven of Hearts Jack of Spades
    Three of Clubs Seven of Clubs
    Five of Diamonds You have a runt.

    Press any key to continue
    It should say "You have one pair" instead. Any suggestions?
    Last edited by MetalSmasher86; 12-12-2005 at 01:44 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Perhaps put values in the Num_in_Face array so the checks your doing with it are actually valid. Unless I don't see it, which is possible, cause I'm tired. I don't see you changing the values in Num_in_Face or Num_in_Pair, yet in your analyze_hand function, you're dependant on their values. You don't even initialize them.
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM