Thread: Poke program, class... :(

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

    Poke program, class... :(

    Please help guys... I need to complete this class to make it work witht his program, and I feel bad already, but I am really clueless. CLasses are hard for me if you could help me out, I'd appreciate it very much.

    Code:
    
    
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    //#include "card.h"
    
    
     class card
    	{
       public:
       	card();
                card(int num, char suit);
                set_num(input from main);
                set_suite(input from main);
                get_num();
                get_suite();
                ~card();
      private:
                int number;
                char suite;
       };
    
      card::card()
    	{
       number=0;
       suite=0;
       }
    
    card::card(int num, char suit)
    	{
    	number=num;
    	suite=suit;
       }
    
    card::~card()
    	{
    	}
    
    card::get_num()
    	{
       }
    
    card:: get_suite()
    	{
       }
    
    card::set_num(input from main)
    	{
       }
    
    card::set_suite( input from main)
    	{
       }
    
    
    
    
    
    // Declare and initialize global variables
    // 'HIGH' - the value of the highest card in the deck
    // 'LOW' - the value of the lowest card in the deck
    // 'HAND_SIZE' - the number of cards in your hand
    const int HIGH = 14, LOW = 2;
    const int HAND_SIZE = 5;
    
    bool good_card( int num, char suit, int index );
    void sort_cards( card _hand[] );
    void cards_switcheroo( card _hand[], int num );
    bool straight_flush( card _hand[] );
    bool four_of_a_kind( card _hand[] );
    bool full_house( card _hand[] );
    bool flush( card _hand[] );
    bool straight( card _hand[] );
    bool three_of_a_kind( card _hand[] );
    bool two_pair( card _hand[] );
    bool pair( card _hand[] );
    
    int main()
    {
    	card hand[HAND_SIZE];
       int num = 0;
       int suit_num = 0;
       char suit;
       int discard_counter = 0;
       int card_num = 0;
    
       for ( int i = 0; i < HAND_SIZE; i++ )
       {
          num = rand() % ( HIGH - LOW + 1 ) + LOW;
          word_num = rand() % ( 4 - 1 + 1 ) + 1;
          if ( word_num == 1 )
          	word = 's';
          if ( word_num == 2 )
          	word = 'h';
          if ( word_num == 3 )
          	word = 'c';
          if ( word_num == 4 )
          	word = 'd';
          if ( word_num > 4 || word_num < 1 )
          	i--;
    
          else if ( good_card( num, word, i )
          	hand[i].set_card( num, word );
    
          else
          	i--;
       }
    
       sort_cards( hand );
    
       if (straight_flush( hand ))
       {
       	cout << "You intially have a straight flush." << endl;
       }
    
       if (four_of_a_kind( hand ))
       {
       	cout << "You initially have four of a kind." << endl;
       }
    
       if (full_house( hand ))
       {
         	cout << "You initially have a full house." << endl;
       }
    
       if (flush( hand ))
       {
       	cout << "You initially have a flush." << endl;
       }
    
       if (straight( hand ))
       {
       	cout << "You initially have a straight." << endl;
       }
    
       if (three_of_a_kind( hand ))
       {
       	cout << "You initially have a kind." << endl;
       }
    
       if (two_pair( hand ))
       {
       	cout << "You initially have a two pair." << endl;
       }
    
       if (pair( hand ))
       {
       	cout << "You initially have a pair." << endl;
       }
    
       cout << "Your hand:";
       for ( int i = 0; i < HAND_SIZE; i++ )
       	cout << "[" << i+1 << "]  " << hand[i].get_num() << hand[i].get_suit() << endl;
       do
       {
       	if ( discard_counter == 0 )
       		cout << "Which card(s) would you like to discard ( < 0 to quit)?" << endl;
    
       	cin >> card_num;
    
          if ( card_num > 0 && card_num < 6 )
          {
          	card_num -= 1;
          	card_switcheroo( card _hand[], card_num );
          }
          if ( card_num == 0 || card_num > 5 )
          	cout << "Invalid card choice! Try again" << endl;
    
          discard_counter++;
       }while ( card_num >= 0 );
    
       return 0;
    }
    
    bool good_card( int num, string suit, int index )
    {
       for ( int i = 0; i < index; i++ )
       {
       	if ( hand[i].get_suit() == suit )
          {
          	if( hand[i].get_num() == num )
             {
             	return false;
             }
          }
       }
    
       return true;
    }
    
    void sort_cards( card _hand[] )
    {
    
    	for ( int i = 0; i < (HAND_SIZE - 1); i++ )
       {
       	for ( int j = (i + 1); j < HAND_SIZE; j++ )
          {
          	if ( _hand[j].get_num() < _hand[i].get_num() )
             {
             	int temp_card_type = _hand[i].get_num();
                _hand[i].set_num( _hand[j].get_num() );
                _hand[j].set_num( temp_card_type );
    
                string temp_suit_type = _hand[i].get_suit();
                _hand[i].set_suit( _hand[j].get_suit() );
                _hand[j].set_suit( temp_suit_type );
             }
          }
       }
    }
    
    void cards_switcheroo( card _hand[], int num )
    {
    	
    }
    
    bool straight_flush( card _hand[] )
    {
    	int same_suit_count = 1;
       int order_count = 0;
    
    
       for ( int i = 0; i < ( HAND_SIZE - 1 ); i++ )
       {
       	if ( _hand[i].get_suit() == _hand[(i+1)].get_suit() )
          	same_suit_count++;
       }
    
       if ( same_suit_count != 5 )
       	return false;
    
       for ( int i = 0; i < (HAND_SIZE - 1); i++ )
       {
       	if ( _hand[i].get_num() == ( _hand[i+1].get_num() - 1 ) )
          {
          	order_count++;
          }
       }
    
       if ( _hand[0].get_num() == 2 &&
            _hand[1].get_num() == 3 &&
            _hand[2].get_num() == 4 &&
            _hand[3].get_num() == 5 &&
            _hand[4].get_num() == 14)
       	{
          	return true;
          }
    
       if ( order_count == 5 )
       {
       	return true;
       }
    
       return false;
    }
    
    bool four_of_a_kind( card _hand[] )
    {
       int card_count = 0;
    
       for ( int i = 2; i < 15; i++ )
    	{
       	for ( int j = 0; j < HAND_SIZE; j++ )
       	{
          	if ( _hand[j].get_num() == i )
             	card_count++;
    
             if (card_count == 4)
             	return true;
          }
          card_count = 0;
       }
    
       return false;
    }
    
    bool full_house( card _hand[] )
    {
    	int first_num = 0;
       bool possible_full_house = false;
    	int card_count = 0;
       for ( int i = 2; i <= 14; i++ )
    	{
       	for ( int j = 0; j < HAND_SIZE; j++ )
       	{
          	if ( _hand[j].get_num() == i )
             	card_count++;
    
             if ( card_count == 3 )
          	{
             	first_num = i;
             	possible_full_house = true;
                card_count = 0;
                break;
             }
       	}
       }
               card_count=0;
       if ( possible_full_house )
       {
       	for ( int i = 2; i <= 14; i++ )
       	{
       		for ( int j = 0; j < HAND_SIZE; j++ )
          	{
          		if ( _hand[j].get_num() == i && i != first_num )
             	{
             		card_count++;
    				}
    
                if ( card_count == 2 )
                	return true;
          	}
             card_count = 0;
          }
       }
    
       return false;
    }
    
    bool flush( card _hand[] )
    {
    	int same_suit_count = 1;
    
    
       for ( int i = 1; i < HAND_SIZE; i++ )
       {
       	if ( _hand[0].get_suit() == _hand[i].get_suit() )
          	same_suit_count++;
    
          if ( same_suit_count == 5 )
       		return true;
       }
    
      	return false;
    }
    
    bool straight( card _hand[] )
    {
       int order_count = 0;
    	for ( int i = 0; i < (HAND_SIZE - 1); i++ )
       {
       	if ( _hand[i+1].get_num() - _hand[i].get_num() == 1 )
          {
          	order_count++;
          }
       }
    
       if ( _hand[0].get_num() == 2 &&
            _hand[1].get_num() == 3 &&
            _hand[2].get_num() == 4 &&
            _hand[3].get_num() == 5 &&
            _hand[4].get_num() == 14)
       	{
          	return true;
          }
    
       if ( order_count == 5 )
       {
       	return true;
       }
    
       return false;
    }
    
    bool three_of_a_kind( card _hand[] )
    {
    	int card_count = 0;
       for ( int i = 2; i <= 14; i++ )
    	{
       	for ( int j = 0; j < HAND_SIZE; j++ )
       	{
          	if ( _hand[j].get_num() == i )
             	card_count++;
       	}
    
          if ( card_count == 3 )
          	 return true;
    
          card_count = 0;
       }
    
       return false;
    }
    
    bool two_pair( card _hand[] )
    {
       bool one_pair = false;
    
    	for ( int j = 0; j < HAND_SIZE; j++ )
       {
          if (one_pair)
          {
          	if ( _hand[j].get_num() == _hand[j+1].get_num() )
             	return true;
          }
    
          if ( _hand[j].get_num() == _hand[j+1].get_num() )
          {
          	one_pair = true;
          }
       }
    
       return false;
    }
    
    bool pair( card _hand[] )
    {
    	int card_count = 0;
       for ( int i = 2; i <= 14; i++ )
    	{
       	for ( int j = 0; j < HAND_SIZE; j++ )
       	{
          	if ( _hand[j].get_num() == i )
             	card_count++;
       	}
    
          if ( card_count == 2 )
          	 return true;
    
       	card_count = 0;
    	}
    
       return false;
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    What specific problem(s) are you having? Like what compile errors do you get? I can see a few right now:
    Code:
    class card
    	{
       public:
       	card();
                card(int num, char suit);
                set_num(input from main);
                set_suite(input from main);
                get_num();
                get_suite();
                ~card();
      private:
                int number;
                char suite;
       };
    Those functions need a return type (except for the constructors/deconstructor)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    16
    Yaeh that's the thing. I dont know really how to write a class... just trying hard. But the assignment is due tomorrow and my buddy who was gonna clue me in on what I missed isn't available... if you could help me out, I'd appreciate it so much... just with writing it I guess. He said it wouldn't be too hard, i hope not.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Sorry I'm a bit rusty since Cprog has been down I haven't been programming much. Anyways, I would say you should make a hand class and use a structure for your card, like this:
    Code:
    struct card
    {
    int number;
    char suit;
    };
    
    class hand
    {
    public:
    hand(); //initialize cards array with random values
    //...maybe a function to print out what your hand is?
    void GetHand();
    
    ~hand();
    
    private:
    card cards[HAND_SIZE]; 
    
    void SortCards();
    bool straight_flush();
    //Could have these functions here and call them from GetHand()
    
    };
    
    void hand::GetHand()
    {
    SortCards();
    //...
    if (straight_flush())
     std::cout<<"You have a straight flush!";
    //etc
    }
    This way your class would take care of everything, rather than having a ton of function calls in main. Just some ideas, hopefully you can come up with more
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. C program for Class --- DUE TODAY!!! AHHHH
    By Fr0st2k in forum C Programming
    Replies: 6
    Last Post: 09-22-2004, 03:10 PM