Thread: function help

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    2

    function help

    I have been trying and trying to write a function that will determine if the hand dealt has a pair, 3 of a kind etc...I have to write several more and was just looking for help getting started with the simplest one..Any help would be greatly appreciated. Thanks. my code is below. Thanks for any help offered.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle( int [][ 13 ] );
    void deal( const int [][ 13 ], const char *[], const char *[] );
    
    int main()
    {
       const char *suit[ 4 ] =  
          { "Hearts", "Diamonds", "Clubs", "Spades" };
       const char *face[ 13 ] = 
          { "Ace", "Deuce", "Three", "Four",
            "Five", "Six", "Seven", "Eight",
            "Nine", "Ten", "Jack", "Queen", "King" };
       int deck[ 4 ][ 13 ] = { 0 };
    
       srand( time( 0 ) );
    
       shuffle( deck );
       deal( deck, face, suit );
    
       return 0;
    }
    
    void shuffle( int wDeck[][ 13 ] )
    {
       int row, column, card;
    
       for ( card = 1; card <= 52; card++ ) {
          do {
             row = rand() % 4;
             column = rand() % 13;
          } while( wDeck[ row ][ column ] != 0 );
    
          wDeck[ row ][ column ] = card;
       }
    }
    
    void deal( const int wDeck[][ 13 ], const char *wFace[],
               const char *wSuit[] )
    {
       int card, row, column;
    
       for ( card = 1; card <= 52; card++ )
    
          for ( row = 0; row <= 3; row++ )
    
             for ( column = 0; column <= 12; column++ )
    
                if ( wDeck[ row ][ column ] == card )
                   printf( "%5s of %-8s%c",
    			            wFace[ column ], wSuit[ row ],
    						card % 2 == 0 ? '\n' : '\t' );

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's some code that shows how to match "X of a kind". Have a read of the function CountSameFace() and see if you understand how it's implemented. The principle behind it is simple:
    - Create a table big enough to hold one entry for every "face".
    - Set the table entries to 0
    - Now loop through the hand, and using the face of each card, index the table and increment the entry.
    - Once all cards are done, the table entry with the highest value is the winner. In my code, I keep tabs on the best table entry whilst looping through the hand, that way I don't need to review the whole table at the end.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define CARDS_PER_HAND  5
    
    enum { DEALER, PLAYER_1, PLAYER_2, MAX_PLAYERS };
    
    typedef enum { HEARTS, DIAMONDS, CLUBS, SPADES, MAX_SUITS } Suits_t;
    
    typedef enum { F_DUMMY, ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, 
                   EIGHT, NINE, TEN, JACK, QUEEN, KING, MAX_FACES } Faces_t;
                   
    const char *Suits_str[MAX_SUITS] = { "Hearts", "Diamonds", "Clubs", "Spades" };
    const char *Faces_str[MAX_FACES] =
    {
      /*
       * The first entry ("Dummy") is not used, it's there to ensure 
       * that an ACE has the index of 1 in the array.
       * This makes things easier when getting data from the user,
       * as they can enter 7, and an index of 7 on this array
       * gets you "Seven".  You may not want to use this method.
       */
      "Dummy",  "Ace",  "Deuce",  "Three",  "Four",   "Five", "Six",    "Seven",
      "Eight",  "Nine", "Ten",    "Jack",   "Queen",  "King"
    };
    
    typedef struct Card
    {
      Faces_t Face;
      Suits_t Suit;
    } Card_t;
    
    typedef struct Hand
    {
      Card_t Cards[CARDS_PER_HAND];
    } Hand_t;
    
    typedef enum {False, True} Bool_t;
    
    size_t CountSameFace (Hand_t *Hand, Faces_t *Face)
    {
      /* Returns: The max occurences of a face within a hand.
       * The winning face is placed in the variable pointed
       * to by parameter Face
       */
      static unsigned char Table[MAX_FACES];
      unsigned char *Best = Table;
      int i;
      memset (Table, 0, sizeof (Table));
      
      for (i = 0; i < CARDS_PER_HAND; i++)
      {
        Faces_t Face;
        Face = Hand->Cards[i].Face;
        Table[Face]++;
        if (Table[Face] > *Best) Best = &Table[Face];
      }
      *Face = Best - Table;
      return *Best;
    }
    
    #define TWO_OF_A_KIND(H,C)   (CountSameFace((H), (C))   == 2)
    #define THREE_OF_A_KIND(H,C) (CountSameFace((H), (C))   == 3)
    #define FOUR_OF_A_KIND(H,C)  (CountSameFace((H), (C))   == 4)
    
    int main(void)
    {
      Hand_t  Hands[MAX_PLAYERS];
      Faces_t WinningFace;
      size_t  MatchingFaces;
    
      /* 
       * Populate a hand manually.  
       * This wouldn't normally be done like this.
       */
       
      Hands[PLAYER_1].Cards[0].Face = ACE;
      Hands[PLAYER_1].Cards[0].Suit = HEARTS;
      Hands[PLAYER_1].Cards[1].Face = KING;
      Hands[PLAYER_1].Cards[1].Suit = DIAMONDS;
      Hands[PLAYER_1].Cards[2].Face = SEVEN;
      Hands[PLAYER_1].Cards[2].Suit = CLUBS;
      Hands[PLAYER_1].Cards[3].Face = ACE;
      Hands[PLAYER_1].Cards[3].Suit = DIAMONDS;
      Hands[PLAYER_1].Cards[4].Face = ACE;
      Hands[PLAYER_1].Cards[4].Suit = SPADES;
    
      /* Now look for an "X of a kind" winner */
      if ((MatchingFaces = CountSameFace (&Hands[PLAYER_1], &WinningFace)) > 1)
        printf ("You have %d of a kind, for face %s\n", MatchingFaces, Faces_str[WinningFace]);
      else puts ("Looser");
      
      /* An example using the macros: */
      if (THREE_OF_A_KIND(&Hands[PLAYER_1], &WinningFace))
      {
        printf ("(via the macro)-> Player %d has three of a kind for face %s\n", 
                PLAYER_1, Faces_str[WinningFace]);
      }
        
      return(0);
    }
    
    /*
    
    My output
    
    You have 3 of a kind, for face Ace
    (via the macro)-> Player 1 has three of a kind for face Ace
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM