Thread: Need help with program for class

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    1

    Question Need help with program for class

    Hello everyone. I’m in desperate need for some help with my problem. First a little background info. The first part of the assignment was to create a program that would take a deck of cards shuffle them, deal a hand, and determine if there was a two pair, three pair, flush.... ect. I was able to knock that program out pretty easily. But the second part of the program is killing me. The second part of the program requires me to have the program a second hand, determine what both hands have and then state which hand would win. I have no clue on how to do this and every time I email the teacher for help, she just confuses me more. If any of you coders out there could tell me how to do this, it would be greatly appreciated. And if you could explain in detail, it would help since I’m still new and learning.

    Here is the code for the first part:
    Code:
    // Fig. 5.12 
    // Card shuffling dealing program 
    #include <iostream>
    #include <conio.h>
    
    using std::cout;
    using std::ios;
    using std::endl;
    
    #include <iomanip>
    
    using std::setw;
    using std::setiosflags;
    
    #include <cstdlib>
    #include <ctime>
    
    void shuffle( int [][ 13 ] );
    void deal( const int [][ 13 ], const char *[], const char *[], int [][ 2 ] );
    void pair( const int [][ 13 ], const int [][ 2 ], const char *[] );
    void threeOfKind( const int [][ 13 ], const int [][ 2 ], const char *[] );
    void fourOfKind( const int [][ 13 ], const int [][ 2 ], const char *[] );
    void flushHand( const int [][ 13 ], const int [][ 2 ], const char *[] );
    void straightHand( const int [][ 13 ], const int [][ 2 ], 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 }, hand[ 5 ][ 2 ] = { 0 };
    
    srand( time( 0 ) );
    
    shuffle( deck );
    deal( deck, face, suit, hand );
    pair( deck, hand, face );
    threeOfKind( deck, hand, face );
    fourOfKind( deck, hand, face );
    flushHand( deck, hand, suit );
    straightHand( deck, hand, suit, face );
    
    
    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 wHand[][ 2 ] )
    {
    int r = 0;
    cout << "Your five card hand is:\n \n";
    
    for ( int card = 1; card < 6; ++card )
    for ( int row = 0; row <= 3; ++row )
    for ( int column = 0; column <= 12; ++column )
    if ( wDeck[ row ][ column ] == card ) {
    wHand[ r ][ 0 ] = row;
    wHand[ r ][ 1 ] = column;
    cout << setw( 5 ) << setiosflags( ios::left )
    << wFace[ column ] << " of " 
    << setw( 8 ) << setiosflags( ios::left )
    << wSuit[ row ] 
    << ( card % 1 == 0 ? '\n' : '\t' );
    ++r;
    }
    }
    
    void pair( const int wDeck[][ 13 ], const int wHand[][ 2 ], 
    const char *wFace[] )
    {
    int counter[ 13 ] = { 0 };
    for ( int r = 0; r < 5; ++r )
    ++counter[ wHand[ r ][ 1 ] ];
    cout << '\n';
    for ( int p = 0; p < 13; ++p )
    if ( counter[ p ] == 2 )
    cout << "Nice job, you have a pair of " << wFace[ p ] << "s.\n";
    }
    
    void threeOfKind( const int wDeck[][ 13 ], const int wHand[][ 2 ],
    const char *wFace[] )
    {
    int counter[ 13 ] = { 0 };
    for ( int r = 0; r < 5; ++r )
    ++counter[ wHand[ r ][ 1 ] ];
    for ( int t = 0; t < 13; t++ )
    if ( counter[ t ] == 3 )
    cout << "Sweet! You have three " << wFace[ t ] << "s.\n";
    }
    
    void fourOfKind( const int wDeck[][ 13 ], const int wHand[][ 2 ],
    const char *wFace[] )
    {
    int counter[ 13 ] = { 0 };
    for ( int r = 0; r < 5; ++r )
    ++counter[ wHand[ r ][ 1 ] ];
    for ( int k = 0; k < 13; ++k )
    if ( counter[ k ] == 4 )
    cout << "You Rule!!! there is four " << wFace[ k ] << "s.\n";
    }
    
    void flushHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],
    const char *wSuit[] )
    {
    int count[ 4 ] = { 0 };
    for ( int r = 0; r < 5; ++r )
    ++count[ wHand[ r ][ 0 ] ];
    for ( int f = 0; f < 4; ++f )
    if ( count[ f ] == 5 ) cout << "AWESOME!!! You have a flush of " << wSuit[ f ] << "s.\n";
    }
    
    void straightHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],
    const char *wSuit[], const char *wFace[] )
    {
    int x[ 5 ] = { 0 }, y;
    for ( int r = 0; r < 5; ++r )
    x[ r ] = wHand[ r ][ 1 ];
    for ( int b = 1; b < 5; ++b )
    for ( int c = 0; c < 4; ++c )
    if ( x[ c ] > x[ c + 1 ] ) {
    y = x[ c ];
    x[ c ] = x[ c + 1 ];
    x[ c + 1 ] = y;
    }
    if ( x[ 4 ] - 1 == x[ 3 ] && x[ 3 ] - 1 == x[ 2 ] 
    && x[ 2 ] - 1 == x[ 1 ] && x[ 1 ] - 1 == x[ 0 ] ) {
    cout << "I bow down to you! You have a straight of\n";
    for ( int d = 0; d < 5; ++d )
    cout << wFace[ wHand[ d ][ 1 ] ] << " of " << wSuit[ wHand[ d ][ 0 ] ]
    << '\n';
    }
    }
    THANK YOU SO MUCH
    Morgan

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    dragunsflame
    Guest

    Unhappy

    Do I really gotta read all of that? If you are gonna post an entire program, could you comment it a little? It'd make everything a lot easier.

    I just scanned over it, but why cant you make two "hand" arrays, deal into one hand, then deal into the other hand, check both of them, and decide who has the better hand?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You need some way to rate a hand. There are many ways to do it. One is to use an enum:
    Code:
    enum score{zilch, pair};
    
    score player1 = pair;
    score player2 = zilch;
    
    if(player1 < player2)
      cout << "player2 wins" << endl;
    else if(player2 < player1)
      cout << "player1 wins" << endl;
    else
      cout << "draw" << endl;
    Another way would be to use a struct/class to represent a player. The player has a hand of 5 cards. The type of hand he has is assigned a value.

    Code:
    struct Player{
    {
       cards hand[5];
       int value;
       int assignValueToHand();
    };
    
    int Player::assignValueToHand()
    {
       if(isZilch(hand))
         value = 0;
       else if(isPair(hand))
         value = 1;
    }
    where isZilch() and isPair() have bool return

    You can even be more creative and indicate that a pair of Aces beats a pair of Kings, and a royal flush 10 high looses to a royal flush jack high, and royal flush 10 high in hearts looses to a royal flush 10 in spades, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM