Thread: pointers, arrays, and functions oh my (newbie Q)

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    pointers, arrays, and functions oh my (newbie Q)

    I have this program i modified that shuffles strings representing a deck of 52 cards. I've made it so it only deals 5 cards... similar to poker. Basically, im trying to create a function that will determine whether the hand contains a pair, two pair, three/four of a kind, straight, and flush. As you can see in my incomplete function "handTester" i attempt to use an if structure to determine the first thing on my list: a pair. I am totally confused on what to put in the if statement's condition. Please help me !!

    Code:
    #include <iostream>
    
    using std::cout;
    
    #include <iomanip>
    using std::ios;
    
    using std::setw;
    using std::setiosflags;
    
    #include <cstdlib>
    #include <ctime>
    
    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;
    
       for ( int 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[] )
    
    {
    		  void handTester ( const int [][ 13 ], const char *[], 
    		  const char *[] );
    
       for ( int card = 1; card <= 5; card++ )
    
          for ( int row = 0; row <= 3; row++ )
    
             for ( int column = 0; column <= 12; column++ )
    
                if ( wDeck[ row ][ column ] == card )
                   cout << setw( 5 ) << setiosflags( ios::right )
                        << wFace[ column ] << " of "
                        << setw( 8 ) << setiosflags( ios::left )
                        << wSuit[ row ] 
                        << ( card % 2 == 0 ? '\n' : '\t' );
    				cout << "\n";
    
    			handTester ( wDeck, wFace, wSuit );
    			
    	void handTester ( const int tDeck[][ 13 ], const char *tFace[],
               const char *tSuit[] )
    	{
    		
    		if (  )
    		cout << "Pair";
    	}
    
    }
    PS i had no idea on what to declare in my created function. If you have any tips for me, i would greatly appreciate it.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    Explain

    I'm a bit shaky on how the "shuffle" and "deal" functions work regarding shuffling your deck and dealing five cards. If you could explain in plain English what's happening there, I might be able to help.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    11
    okay i will ost the psudocode of what is supposed to happen:

    Initialize the suit array
    Initialize the face array
    Initialize the deck array

    For each of the 52 cards
    Choose slot of deck randomly


    While slot of deck has been previously chosen
    Choose slot of deck randomly

    Place card number in chosen slot of deck

    For each of the 52 cards
    For each slot of deck array
    If slot contains desired card number
    Print the face and suit of the card


    BTW, i am aware that this is a very inefficient algorithm but at the time it is the most simple for me :-D

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Consider this
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    #include <iomanip>
    using std::ios;
    using std::setw;
    using std::setiosflags;
    
    #include <cstdlib>
    #include <ctime>
    
    struct card {
        int	face;
        int	suit;
    };
    
    const int   suit_len = 4;
    const int   face_len = 13;
    const int   deck_len = suit_len * face_len;
    const int   hand_len = 5;
    
    void initialise( card deck[] );
    void showcards( card cards[], int num_cards,
                    const char *suit_names[], const char *face_names[] );
    void shuffle( card deck[] );
    void deal( card deck[], card hand[] );
    void handTester ( card hand[],
                      const char *suit_names[], const char *face_names[] );
    
    int main()
    {
        const char *suit[suit_len] =  
          { "Hearts", "Diamonds", "Clubs", "Spades" };
        const char *face[face_len] = 
          { "Ace", "Deuce", "Three", "Four",
            "Five", "Six", "Seven", "Eight",
            "Nine", "Ten", "Jack", "Queen", "King" };
    
        card deck[deck_len];
        card hand[hand_len];
    
        srand( time( 0 ) );
    
        initialise( deck );
        showcards( deck, 10, suit, face );
        shuffle( deck );
        showcards( deck, 10, suit, face );
        deal( deck, hand );
        showcards( hand, 5, suit, face );
        handTester( hand, suit, face );
    
        return 0;
    }
    
    // puts cards into order in a deck
    void initialise ( card deck[] ) {
        int index = 0;
        for ( int suit = 0 ; suit < suit_len ; suit++ ) {
            for ( int face = 0 ; face < face_len ; face++ ) {
                deck[index].face = face;
                deck[index].suit = suit;
                index++;
            }
        }
    }
    
    // just a debug function to show what's going on
    void showcards ( card cards[], int num_cards,
                     const char *suit_names[], const char *face_names[] ) {
        for ( int i = 0 ; i < num_cards ; i++ ) {
            cout << "card " << i << " is ";
            cout << suit_names[cards[i].suit] << ",";
            cout << face_names[cards[i].face] << endl;
        }
        cout << endl;
    }
    
    // takes pairs of cards, and swaps them over
    void shuffle( card deck[] ) {
        for ( int i = 0 ; i < deck_len ; i++ ) {
            int pos1 = rand() % deck_len;
            int pos2 = rand() % deck_len;
            if ( pos1 != pos2 ) {
                // swap them over (one small shuffle)
                card    temp = deck[pos1];
                deck[pos1] = deck[pos2];
                deck[pos2] = temp;
            }
        }
    }
    
    // simple deal from the start of the deck
    // to deal more hands, you would need to remember the last card dealt
    void deal( card deck[], card hand[] ) {
        for ( int i = 0 ; i < hand_len ; i++ ) {
            hand[i] = deck[i];
        }
    }
    
    // sort cards into face order
    int sortface ( const void *a, const void *b ) {
        const card *pa = (const card *)a;
        const card *pb = (const card *)b;
        if ( pa->face < pb->face ) return -1;
        if ( pa->face > pb->face ) return 1;
        return 0;
    }
    // sort cards into suit order
    int sortsuit ( const void *a, const void *b ) {
        const card *pa = (const card *)a;
        const card *pb = (const card *)b;
        if ( pa->suit < pb->suit ) return -1;
        if ( pa->suit > pb->suit ) return 1;
        return 0;
    }
    
    // I suggest you write a separate function for each possible rule
    // like
    // bool handHasPair ( card hand[] );
    // bool handHasTwoPair ( card hand[] );
    // that way, you can focus on each problem in turn, and bring them all
    // together in handTester
    
    void handTester ( card hand[],
                      const char *suit_names[], const char *face_names[] )
    {
        // sorting into face order will make pairs adjacent to one another
        qsort( hand, hand_len, sizeof(card), sortface );
        showcards( hand, 5, suit_names, face_names );
        for ( int i = 0 ; i < hand_len-1 ; i++ ) {
            if ( hand[i].face == hand[i+1].face ) {
                cout << "A pair" << endl;
            }
        }
    }

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    11
    wow i didn't expect you to rewrite the entire code But really it was meant to be that way because it was supposed to be simplistic code taken from the book and modified not necceraly efficient. Anyway thank you, i just realized after seeing the handTester function you made what i should do now.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    BIIIIIIG PROBLEM!

    Code:
    #include <iostream>
    
    using std::cout;
    
    #include <iomanip>
    using std::ios;
    
    using std::setw;
    using std::setiosflags;
    
    #include <cstdlib>
    #include <ctime>
    
    void shuffle( int [][ 13 ] );
    void deal( const int [][ 13 ], const char *[], const char *[] );
    void handTester ( int, int p[]);
    
    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;
    
       for ( int 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 p[ 4 ] = { 0 };
    		  int i = 0;
    		  int card, row, column;
    
    	for ( card = 1; card <= 5; card++ )
    
    		for ( row = 0; row <= 3; row++ )
    
    			for ( column = 0; column <= 12; column++ )
    
    				if ( wDeck[ row ][ column ] == card ){
    				
                   cout << setw( 5 ) << setiosflags( ios::right )
                        << wFace[ column ] << " of "
                        << setw( 8 ) << setiosflags( ios::left )
                        << wSuit[ row ] 
                        << ( card % 2 == 0 ? '\n' : '\t' )
    					<< "\n";
    				 p[ i ] = column;
    				 i++;
    				}
    
    	handTester ( i, p );
    }			
    
    void handTester ( int x, int q[ 4 ] )
    	{
    	int y;
    		
    		for ( x = 0; x <= 4; x++ )
    			for ( y = x + 1; y <= 4; y++)
    				if ( q[ x ] == q [ y ] )
    					cout << "\nPair found\n";
    	}
    When i run it i get a weird error message... i will attempt to post it.

  7. #7
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    wuts the entire name of ur visual c++ compiler?
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie problems with pointers arrays and functions.
    By MegaManZZ in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2008, 05:33 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM