Thread: having problems with my card program

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    16

    Exclamation having problems with my card program

    i have a program and it works great... the only thing is that i need to add a best hand function... its a poker problem... i need to make a section that stores the best hand and if the hand is beaten it gets replaced... can anyone help me??? the program is at http://www.math.concordia.ab.ca/~mar...2/code/poker.c

    thanks

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I don't know exactly what you want to store, I just noticed that this thread remained unanswered.
    What is the type of information you want to store?
    You want this information to be saved or not?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You'd probably get more replies if you posted the code here. Most people don't want to have to click a link to read your code.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here it is, for reference:
    Code:
    /*********************************************************
     * From C PROGRAMMING: A MODERN APPROACH, by K. N. King  *
     * Copyright (c) 1996 W. W. Norton & Company, Inc.       *
     * All rights reserved.                                  *
     * This program may be freely distributed for class use, *
     * provided that this copyright notice is retained.      *
     *********************************************************/
    
    /* poker.c (Chapter 10, page 199) */
    /* Classifies a poker hand */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_RANKS 13
    #define NUM_SUITS 4
    #define NUM_CARDS 5
    #define TRUE 1
    #define FALSE 0
    
    typedef int Bool;
    
    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 */
    
    void read_cards(void);
    void analyze_hand(void);
    void print_result(void);
    
    /********************************************************** 
     * main: Calls read_cards, analyze_hand, and print_result * 
     *       repeatedly.                                      * 
     **********************************************************/
    main()
    {
      for (;;) {          /* infinite loop */
        read_cards();
        analyze_hand();
        print_result();
      }
    }
    
    /********************************************************** 
     * read_cards: Reads the cards into the external          * 
     *             variables num_in_rank and num_in_suit;     * 
     *             checks for bad cards and duplicate cards.  * 
     **********************************************************/
    
    void read_cards(void)
    {
      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 a three-of-a-kind; determines the * 
     *               number of pairs; stores the results into * 
     *               the external variables straight, flush,  * 
     *               four, three, and pairs.                  * 
     **********************************************************/
    void analyze_hand(void)
    {
      int num_consec = 0;
      int rank, 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 */
      rank = 0;
      while (num_in_rank[rank] == 0) rank++;
      for (; rank < NUM_RANKS && num_in_rank[rank]; rank++)
        num_consec++;
      if (num_consec == NUM_CARDS) {
        straight = TRUE;
        return;
      }
    
      /* 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: Notifies the user of the result, using   * 
     *               the external variables straight, flush,  * 
     *               four, three, and pairs.                  * 
     **********************************************************/
    void print_result(void)
    {
      if (straight && flush) printf("Straight flush\n\n");
      else if (four)         printf("Four of a kind\n\n");
      else if (three &&
               pairs == 1)   printf("Full house\n\n");
      else if (flush)        printf("Flush\n\n");
      else if (straight)     printf("Straight\n\n");
      else if (three)        printf("Three of a kind\n\n");
      else if (pairs == 2)   printf("Two pairs\n\n");
      else if (pairs == 1)   printf("Pair\n\n");
      else                   printf("High card\n\n");
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This code is pretty old. It uses the implicit int rule:
    Code:
    main() {
    This should be
    Code:
    int main() {
    for more recent compilers. Other than that (and maybe Bool) it's good code.

    i have a program and it works great... the only thing is that i need to add a best hand function...
    Have you tried this yet? Or are you hoping we'll write it?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] Program : How to validate credit card number
    By kingofdcp in forum C Programming
    Replies: 13
    Last Post: 10-31-2009, 12:58 AM
  2. Blackjack program, having problems with the ace
    By yigster in forum C Programming
    Replies: 6
    Last Post: 05-07-2009, 06:41 AM
  3. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  4. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  5. Sign up: The Card Game Contest
    By ygfperson in forum Contests Board
    Replies: 40
    Last Post: 09-24-2002, 05:43 PM