Thread: poker game without suits

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    poker game without suits

    Hi I really need help with this program. I'm separating it into 3 functions:
    read_cards
    anaylze_cards
    and then print_results

    but I'm stuck on read_cards
    I'm basically doing a color blind poker game where the suits are ignoreed. There are two players and each player has 5 cards. The players compare their hands according to rankings. But I'm stuck on how to have the user input their cards in a
    2x5 array. ANY help is really apperciated. I have my anaylze functions pretty much done but it needs to input like this:
    02020 JJQQA
    1st ply| 2nd plyr

    Code:
    #include <stdbool.h>   /* C99 only */
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_PLAYERS 2
    #define HANDS 2
    #define CARDS 5
    
    /* external variables */
    int hand[NUM_PLAYERS][5];
    
    
    bool straight, flush, four, three;
    int pairs;   /* can be 0, 1, or 2 */
    
    /* prototypes */
    void read_cards(void);
    void analyze_hand(void);
    void print_result(void);
    
    /**********************************************************
     * main: Calls read_cards, analyze_hand, and print_result *
     *       repeatedly.                                      *
     **********************************************************/
    int main(void)
    {
      for (;;) {
        read_cards();
      
      }
    }
    
    /**********************************************************
     * read_cards: Reads the cards into the external variable *
     *             hand; checks for bad cards and duplicate   *
     *             cards.                                     *
     **********************************************************/
    void read_cards(void)
    {
      char ch, hands_ch, cards_ch;
      int i, hands, cards;
      bool bad_card, duplicate_card;
      int cards_read = 0;
    
    
        printf("Enter a card: ");
    
        cards_ch = getchar();
        switch (cards_ch) {
        case '0':             cards = 10; break; 
          case '2':           cards = 0; break;
          case '3':           cards = 1; break;
          case '4':           cards = 2; break;
          case '5':           cards = 3; break;
          case '6':           cards = 4; break;
          case '7':           cards = 5; break;
          case '8':           cards = 6; break;
          case '9':           cards = 7; break;
          case 't': case 'T': cards = 8; break;
          case 'j': case 'J': cards = 9; break;
          case 'q': case 'Q': cards = 10; break;
          case 'k': case 'K': cards = 11; break;
          case 'a': case 'A': cards = 12; break;
          default:            bad_card = true;
        }
    
        hands_ch = getchar();
        
    
        while ((ch = getchar()) != '\n')
          if (ch != ' ') bad_card = true;
    
        if (bad_card) {
          printf("Bad card; ignored.\n");
          continue;
        }
    
        duplicate_card = false;
        for (i = 0; i < cards_read; i++)
          if (hand[i][HANDS] == hands && hand[i][CARDS] == cards) {
            printf("Duplicate card; ignored.\n");
            duplicate_card = true;
            break;
          }
    
        if (!duplicate_card) {
          hand[cards_read][HANDS] = hands;
          hand[cards_read][CARDS] = cards;
          cards_read++;
        }
      }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( player = 0; player < PLAYERS; player++ )
        readcards( player );
    Something like that. Combine with something like this:
    Code:
    void readcards( int player )
    {
        if( player > -1 && player < PLAYERS )
        {
            ...read each card into hands[ player ][ card ]
        }
    }
    Alternately, you could make the function return a value if the read was successful or not.


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

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    So do I replace my orignial for statment with the one you listed above?

    I've been working on this for about 4 hours already, just trying to get the right output so I can have a goodnight

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    So lets take a look at this, first off you have your constant definitions however fail to use them when you define your array:
    Code:
    int hand[NUM_PLAYERS][5]; // This could be hand[NUM_PLAYERS][CARDS]
    You also define a bunch of global variables including your array; although this works so your functions can use them I would take a look at restructuring this as it is bad practice.

    As for your function prototypes, to ease gaming logic you might want to think about passing a value to your read_cards function to represent the current player, thus your function prototype would be:
    Code:
    void read_cards(int);
    For your gaming logic in your main function, just think about how you want to do this. I am assuming you would like the game to perform input cards for each player and then run your analysis and print functions for each hand. To me that sounds like a nested loop:
    Code:
    int main(void)
    {
         for(int currentHand=0; currentHand < HANDS; currentHand++){
              for(int currentPlayer=0; currentPlayer < NUM_PLAYERS; currentPlayer++){
                   read_cards(currentPlayer);
              }
              analyze_hand();
              print_result();
         }
         return(0); //<-- Do not forget this
    }
    Now your read cards function only needs to worry about the current player. So when you access your array you just have to worry about inputting the number of cards for that player:
    Code:
    void read_cards(int player)
    {
        for(int currentCard=0; currentCard < CARDS; currentCard++)
        {
              ...ask for card and do your conversions....
              ...store the card in hand[player][currentCard]....
         }
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    1
    nivoca... could u email me the working code please .?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    @ricky12155
    Stop hijacking threads with "gimmetehcodez" requests.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poker Game need help...
    By boyracer88 in forum Game Programming
    Replies: 1
    Last Post: 11-25-2005, 01:32 PM
  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