Thread: C programming, Poker game...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    10

    C programming, Poker game...

    I'm having trouble with a poker game. I was able to print out the correct hand (straight, three of a kind, etc.) for one player after they input their own cards.... but now I need to add a second player and i need their hands to compare and figure out who the winner is. And i just dont know where to start. There are no suits when you are playing the game so please ignore any part of the code that has to do with suits.

    Also, if there is a way to input the players' cards into an array for each player and work the program like that i'd greatly appreciate it if anyone could help me out with that as well.




    Code:
    /* Classifies a poker hand */
    
    #include <stdbool.h>   
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_RANKS 13
    #define NUM_SUITS 4
    #define NUM_CARDS 5
    
    /* external variables */
    int num_in_rank[NUM_RANKS];
    int num_in_suit[NUM_SUITS];
    bool straight, flush, four, three;
    int pairs;   /* can be 0, 1, or 2 */
    
    /* prototypes */
    
    /**********************************************************
     * main                             *
     **********************************************************/
    int main(void)
    {
    
    
    
    /**********************************************************
     * read_cards: Reads the cards into the external          *
     *             variables num_in_rank and num_in_suit;     *
     *             checks for bad cards and duplicate cards.  *
     **********************************************************/
    
      bool card_exists[NUM_RANKS][NUM_SUITS];
      char ch, rank_ch, suit_ch;
      int rank, suit;
      bool bad_card;
      int cards_read = 0;
    
      for (rank = 0; rank < NUM_RANKS; rank++) {
        num_in_rank[rank] = 0;
        for (suit = 0; suit < NUM_SUITS; suit++)
          card_exists[rank][suit] = false;
      }
    
      for (suit = 0; suit < NUM_SUITS; suit++)
        num_in_suit[suit] = 0;
    
      while (cards_read < NUM_CARDS) {
        bad_card = false;
    
        printf("Enter a card: ");
    
        rank_ch = getchar();
        switch (rank_ch) {
          case '0':           exit(EXIT_SUCCESS);
          case '2':           rank = 0; break;
          case '3':           rank = 1; break;
          case '4':           rank = 2; break;
          case '5':           rank = 3; break;
          case '6':           rank = 4; break;
          case '7':           rank = 5; break;
          case '8':           rank = 6; break;
          case '9':           rank = 7; break;
          case 't': case 'T': rank = 8; break;
          case 'j': case 'J': rank = 9; break;
          case 'q': case 'Q': rank = 10; break;
          case 'k': case 'K': rank = 11; break;
          case 'a': case 'A': rank = 12; break;
          default:            bad_card = true;
        }
    
        /*suit_ch = getchar();
        switch (suit_ch) {
          case 'c': case 'C': suit = 0; break;
          case 'd': case 'D': suit = 1; break;
          case 'h': case 'H': suit = 2; break;
          case 's': case 'S': suit = 3; break;
          default:            bad_card = true;
        }*/
    
        while ((ch = getchar()) != '\n')
          if (ch != ' ') bad_card = true;
    
        if (bad_card)
          printf("Bad card; ignored.\n");
        /*else if (card_exists[rank][suit])
          printf("Duplicate card; ignored.\n");*/
        else {
          num_in_rank[rank]++;
          num_in_suit[suit]++;
          card_exists[rank][suit] = true;
          cards_read++;
        }
      }
      
    /**********************************************************
    
    
    
    /**********************************************************
    
    /**********************************************************
     * analyze_hand: Determines whether the hand contains a   *
     *               straight, a flush, four-of-a-kind,       *
     *               and/or three-of-a-kind; determines the   *
     *               number of pairs; stores the results into *
     *               the external variables straight, flush,  *
     *               four, three, and pairs.                  *
     **********************************************************/
    {
      int num_consec = 0;
    
      bool straights = false;
      flush = false;
      four = false;
      three = false;
      pairs = 0;
    
      /* check for straight */
      rank = 0;
      while (num_in_rank[rank] == 0) rank++;
      for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
        num_consec++;
      if (num_consec == NUM_CARDS) {
        straight = true;
      }
    
      /* check for 4-of-a-kind, 3-of-a-kind, and pairs */
      for (rank = 0; rank < NUM_RANKS; rank++) {
        if (num_in_rank[rank] == 4) four = true;
        if (num_in_rank[rank] == 3) three = true;
        if (num_in_rank[rank] == 2) pairs++;
      }
    }
    
    /**********************************************************
     * print_result                  *
     **********************************************************/
    
      if (four)         printf("Four of a kind");
      else if (three &&
               pairs == 1)   printf("Full house");
      else if (flush)        printf("Flush");
      else if (straight)     printf("Straight");
      else if (three)        printf("Three of a kind");
      else if (pairs == 2)   printf("Two pairs");
      else if (pairs == 1)   printf("Pair");
      else                   printf("High card");
    
    getch();
      printf("\n\n");
      
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by JSlam View Post
    Also, if there is a way to input the players' cards into an array for each player and work the program like that i'd greatly appreciate it if anyone could help me out with that as well.
    Do you know how to put values into an array? (Say yes, you are doing it in your code already.) Ok, so make an array for each player's hand. Or make a P dimension by C array, and store all the hands:
    Code:
    type hands[ PLAYERS ][ CARDS ];
    I would probably make a function that just assigned a value to each hand. Read: List of poker hands - Wikipedia, the free encyclopedia

    High Card = Face value
    One Pair = Highest Card Value + 1
    Two Pair = One Pair + 1
    ...
    Straight Flush = Four Of A Kind + 1

    Make a function that takes a hand as an argument and returns the value of that hand. Compare the hand value of each player, whoever is higher has won.


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

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Quote Originally Posted by quzah View Post
    Do you know how to put values into an array? (Say yes, you are doing it in your code already.) Ok, so make an array for each player's hand. Or make a P dimension by C array, and store all the hands:
    Code:
    type hands[ PLAYERS ][ CARDS ];
    I would probably make a function that just assigned a value to each hand. Read: List of poker hands - Wikipedia, the free encyclopedia

    High Card = Face value
    One Pair = Highest Card Value + 1
    Two Pair = One Pair + 1
    ...
    Straight Flush = Four Of A Kind + 1

    Make a function that takes a hand as an argument and returns the value of that hand. Compare the hand value of each player, whoever is higher has won.


    Quzah.
    Thank you for your reply quzah. I finally got both players in a [2][5] array.... now all i need to do is get an output for the type of hands each player has and compare the hands and see who the winner is.... could you please elaborate on how i would go about making a function for that? Also, here's the edited code, thanks again....

    Code:
    /* Classifies a poker hand */
    
    #include <stdbool.h>   
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_RANKS 13
    #define NUM_SUITS 4
    #define NUM_CARDS 5
    
    /* external variables */
    int num_in_rank[NUM_RANKS];
    int num_in_suit[NUM_SUITS];
    bool straight, flush, four, three;
    int pairs;   /* can be 0, 1, or 2 */
    
    /**********************************************************
     * main                             *
     **********************************************************/
    int main(void)
    {
    
    
    
    /**********************************************************
     * read_cards: Reads the cards into the external          *
     *             variables num_in_rank and num_in_suit;     *
     *             checks for bad cards and duplicate cards.  *
     **********************************************************/
    
      bool card_exists[NUM_RANKS][NUM_SUITS];
      char ch, rank_ch, suit_ch;
      int rank, suit;
      bool bad_card;
      int cards_read = 0;
    
      for (rank = 0; rank < NUM_RANKS; rank++) {
        num_in_rank[rank] = 0;
        for (suit = 0; suit < NUM_SUITS; suit++)
          card_exists[rank][suit] = false;
      }
    
      char inHand[2][5];
      int card;
    
      while (cards_read < NUM_CARDS) {
        bad_card = false;
    
    printf("Enter player1 and player2's cards, respectively\n"); 
    printf("(Example: '23456(hit tab here)45678') : \n");
        
    for(card=0;card<5;card++)             //player1
        scanf("%c", &inHand[0][card]);    
        
    for(card=0;card<5;card++)             //player2
        scanf("\t%c", &inHand[1][card]);
    
    for(card=0;card<5;card++)
    {
        switch (inHand[0][card]) {
          case '0':           exit(EXIT_SUCCESS);
          case '2':           rank = 0; break;
          case '3':           rank = 1; break;
          case '4':           rank = 2; break;
          case '5':           rank = 3; break;
          case '6':           rank = 4; break;
          case '7':           rank = 5; break;
          case '8':           rank = 6; break;
          case '9':           rank = 7; break;
          case 't': case 'T': rank = 8; break;
          case 'j': case 'J': rank = 9; break;
          case 'q': case 'Q': rank = 10; break;
          case 'k': case 'K': rank = 11; break;
          case 'a': case 'A': rank = 12; break;
          default:            bad_card = true;
        }
        
        switch (inHand[1][card]) {
          case '0':           exit(EXIT_SUCCESS);
          case '2':           rank = 0; break;
          case '3':           rank = 1; break;
          case '4':           rank = 2; break;
          case '5':           rank = 3; break;
          case '6':           rank = 4; break;
          case '7':           rank = 5; break;
          case '8':           rank = 6; break;
          case '9':           rank = 7; break;
          case 't': case 'T': rank = 8; break;
          case 'j': case 'J': rank = 9; break;
          case 'q': case 'Q': rank = 10; break;
          case 'k': case 'K': rank = 11; break;
          case 'a': case 'A': rank = 12; break;
          default:            bad_card = true;
        }
        
    
          
          
    
        if (bad_card)
          printf("Bad card; ignored.\n");
          
        else {
          num_in_rank[rank]++;
          num_in_suit[suit]++;
          card_exists[rank][suit] = true;
          cards_read++;
        }
      }
    }
      
    //**********************************************************
    //******************************************************************************
    //Printout Cards
    //******************************************************************************
                   
          for(card=0; card<5; card++)     
          { 
                   printf("%c", inHand[0][card]);
          }  
          
          printf("\t");
          
          for(card=0; card<5; card++)     
          { 
                   printf("%c", inHand[1][card]);
          }        
    
    
    //**********************************************************
    
    //**********************************************************
     // analyze_hand:                *
    // **********************************************************/
    {
      int num_consec = 0;
    
      bool straights = false;
      flush = false;
      four = false;
      three = false;
      pairs = 0;
    
      /* check for straight */
      rank = 0;
      while (num_in_rank[rank] == 0) rank++;
      for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
        num_consec++;
      if (num_consec == NUM_CARDS) {
        straight = true;
      }
    
      /* check for 4-of-a-kind, 3-of-a-kind, and pairs */
      for (rank = 0; rank < NUM_RANKS; rank++) {
        if (num_in_rank[rank] == 4) four = true;
        if (num_in_rank[rank] == 3) three = true;
        if (num_in_rank[rank] == 2) pairs++;
      }
    }
    
    /**********************************************************
     * print_result                  *
     **********************************************************/
    
      if (four)         printf("\tFour of a kind");
      else if (three &&
               pairs == 1)   printf("\tFull house");
      else if (flush)        printf("Flush");
      else if (straight)     printf("\tStraight");
      else if (three)        printf("\tThree of a kind");
      else if (pairs == 2)   printf("\tTwo pairs");
      else if (pairs == 1)   printf("\tPair");
      else                   printf("\tHigh card");
    
    getch();
      printf("\n\n");
      
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure:
    Code:
    int handvalue( type hand[5] )
    {
        if hand is a straight flush
            return highest value
        if hand is four of a kind
            return one less than what a straight flush returns
        ... repeat for rest of hands
        return highest card from hand
    }
    ...
    
    for( x = 0; x < PLAYERS; x++ )
        hval[ x ] = handvalue( inHand[ x ] );
    
    winner = highestvaluein( hval );
    That's the general idea. You need to write the parts that compare the passed hand with what each hand type would be.


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

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Thanks for that info.... but right now I'm getting incorrect hand values (straight, pairs, etc.) after i added the second player.... could you please run this code and steer me in the right direction again? lol thanks.

    Code:
    /* Classifies a poker hand */
    
    #include <stdbool.h>   
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_RANKS 13
    #define NUM_SUITS 4
    #define NUM_CARDS 5
    
    /* external variables */
    int num_in_rank[NUM_RANKS];
    int num_in_suit[NUM_SUITS];
    bool straight, flush, four, three;
    bool straight1, flush1, four1, three1;
    int pairs;   /* can be 0, 1, or 2 */
    int pairs1;
    
    /**********************************************************
     * main                             *
     **********************************************************/
    int main(void)
    {
    
    
    
    /**********************************************************
     * read_cards: Reads the cards into the external          *
     *             variables num_in_rank and num_in_suit;     *
     *             checks for bad cards and duplicate cards.  *
     **********************************************************/
    
      bool card_exists[NUM_RANKS][NUM_SUITS];
      char ch, rank_ch, suit_ch;
      int rank, rank1, suit;
      bool bad_card;
      int cards_read = 0;
    
      for (rank = 0; rank < NUM_RANKS; rank++) {
        num_in_rank[rank] = 0;
        for (suit = 0; suit < NUM_SUITS; suit++)
          card_exists[rank][suit] = false;
      }
    
      char inHand[2][5];
      int card;
    
      while (cards_read < NUM_CARDS) {
        bad_card = false;
    
    printf("Enter player1 and player2's cards, respectively\n"); 
    printf("(Example: '23456(hit tab here)45678') : \n");
        
    for(card=0;card<5;card++)             //player1
        scanf("%c", &inHand[0][card]);    
        
    for(card=0;card<5;card++)             //player2
        scanf("\t%c", &inHand[1][card]);
    
    for(card=0;card<5;card++)
    {
        switch (inHand[0][card]) {
          case '0':           exit(EXIT_SUCCESS);
          case '2':           rank = 0; break;
          case '3':           rank = 1; break;
          case '4':           rank = 2; break;
          case '5':           rank = 3; break;
          case '6':           rank = 4; break;
          case '7':           rank = 5; break;
          case '8':           rank = 6; break;
          case '9':           rank = 7; break;
          case 't': case 'T': rank = 8; break;
          case 'j': case 'J': rank = 9; break;
          case 'q': case 'Q': rank = 10; break;
          case 'k': case 'K': rank = 11; break;
          case 'a': case 'A': rank = 12; break;
          default:            bad_card = true;
        }
        
        switch (inHand[1][card]) {
          case '0':           exit(EXIT_SUCCESS);
          case '2':           rank1 = 0; break;
          case '3':           rank1 = 1; break;
          case '4':           rank1 = 2; break;
          case '5':           rank1 = 3; break;
          case '6':           rank1 = 4; break;
          case '7':           rank1 = 5; break;
          case '8':           rank1 = 6; break;
          case '9':           rank1 = 7; break;
          case 't': case 'T': rank1 = 8; break;
          case 'j': case 'J': rank1 = 9; break;
          case 'q': case 'Q': rank1 = 10; break;
          case 'k': case 'K': rank1 = 11; break;
          case 'a': case 'A': rank1 = 12; break;
          default:            bad_card = true;
        }
        
    
          
          
    
        if (bad_card)
          printf("Bad card; ignored.\n");
          
        else {
          num_in_rank[rank]++;
          num_in_rank[rank1]++;
          num_in_suit[suit]++;
          card_exists[rank][suit] = true;
          card_exists[rank1][suit] = true;
          cards_read++;
        }
      }
    }
      
    //**********************************************************
    //******************************************************************************
    //Printout Cards
    //******************************************************************************
                   
          for(card=0; card<5; card++)     
          { 
                   printf("%c", inHand[0][card]);
          }  
          
          printf("\t");
          
          for(card=0; card<5; card++)     
          { 
                   printf("%c", inHand[1][card]);
          }        
    
    
    //**********************************************************
    
    //**********************************************************
     // analyze_hand:   player 1             *
    // **********************************************************/
    {
      int num_consec = 0;
    
      bool straights = false;
      flush = false;
      four = false;
      three = false;
      pairs = 0;
    
      /* check for straight */
      rank = 0;
      while (num_in_rank[rank] == 0) rank++;
      for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
        num_consec++;
      if (num_consec == NUM_CARDS) {
        straight = true;
      }
    
      /* check for 4-of-a-kind, 3-of-a-kind, and pairs */
      for (rank = 0; rank < NUM_RANKS; rank++) {
        if (num_in_rank[rank] == 4) four = true;
        if (num_in_rank[rank] == 3) three = true;
        if (num_in_rank[rank] == 2) pairs++;
      }
    }
    
    //**********************************************************
     // analyze_hand:   player 2            *
    // **********************************************************/
    {
      int num_consec = 0;
    
      bool straights1 = false;
      bool flush1 = false;
      bool four1 = false;
      bool three1 = false;
      int pairs1 = 0;
    
      /* check for straight */
      rank1 = 0;
      while (num_in_rank[rank1] == 0) rank1++;
      for (; rank1 < NUM_RANKS && num_in_rank[rank1] > 0; rank1++)
        num_consec++;
      if (num_consec == NUM_CARDS) {
        straight1 = true;
      }
    
      /* check for 4-of-a-kind, 3-of-a-kind, and pairs */
      for (rank1 = 0; rank1 < NUM_RANKS; rank1++) {
        if (num_in_rank[rank1] == 4) four1 = true;
        if (num_in_rank[rank1] == 3) three1 = true;
        if (num_in_rank[rank1] == 2) pairs1++;
      }
    }
    
    /**********************************************************
     * print_result 1                 *
     **********************************************************/
    
      if (four)         printf("\tFour of a kind");
      else if (three &&
               pairs == 1)   printf("\tFull house");
      else if (flush)        printf("Flush");
      else if (straight)     printf("\tStraight");
      else if (three)        printf("\tThree of a kind");
      else if (pairs == 2)   printf("\tTwo pairs");
      else if (pairs == 1)   printf("\tPair");
      else                   printf("\tHigh card");
      
      /**********************************************************
     * print_result     2             *
     **********************************************************/
    
      if (four1)         printf("\tFour of a kind");
      else if (three1 &&
               pairs1 == 1)   printf("\tFull house");
      else if (flush1)        printf("Flush");
      else if (straight1)     printf("\tStraight");
      else if (three1)        printf("\tThree of a kind");
      else if (pairs1 == 2)   printf("\tTwo pairs");
      else if (pairs1 == 1)   printf("\tPair");
      else                   printf("\tHigh card");
    
    getch();
      printf("\n\n");
      
    }

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Hmm... a very similar thread was posted here. Are you guys in the same class?

    Either way your best bet is to clean up that code a little bit and break it into some functions. Once you get the logic working for one player you will be able to have it work for any number of players. The above post should give you some ideas on where you should be heading.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  2. Poker Game
    By egomaster69 in forum C Programming
    Replies: 18
    Last Post: 01-03-2005, 06:24 PM
  3. poker game
    By b00l34n in forum Game Programming
    Replies: 14
    Last Post: 01-03-2005, 12:21 PM
  4. Poker Game
    By Smiley**123 in forum Game Programming
    Replies: 6
    Last Post: 12-14-2003, 09:48 AM
  5. poker game help
    By datainjector in forum C Programming
    Replies: 5
    Last Post: 09-06-2002, 07:31 AM

Tags for this Thread