Thread: Need help with a card dealing program using classes

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    Need help with a card dealing program using classes

    I am working on a program that will deal cards using classes. KI have somewhat of a start but am confused on a couple things. Here is what I'm supposed to do:
    Construct a class definition that can be used to represent a deck of cards. Each card deck is defined by an integer array of 52 cards and an integer indicating the last card position for undealt cards. The services provided by the class should be the ability to deal a single card from the set of undealt cards, and the ability to shuffle the deck by setting the last card position back to 51.

    The main program will deal and display 2 hands of 5 cards, shuffle the deck and then deal and display 2 more hands of 5 cards.

    The card representation is like this
    suit 1-4 1=clubs, 2 = diamonds, 3 = hearts, 4=spades
    value 1 = ace 2-10=value of card, 11 = jack, 12 = queen ,13 = king

    I have attempted some of it so far but I don't really know if i'm suppoed to generate 52 random numbers and put the numbers into the array or? Also I don't really know how to deal the cards. I am just overall really confused. Here is what I have so far

    Code:
    #include <iostream>
    #include<ctime>
    using namespace std;
    
    
    class Cards
    {
    private:
    	int deck[52];
    	int card;
    
    public:
    	Cards(int[], int); //Constructor
    	void deal(int, int);
    	void shuffle(int, int);
    };
    
    Cards::Cards(int deck[], int card)
    {
      srand(time(NULL));
      int suitRand = rand() % 4 + 1;
      int valRand = rand() % 13 +1;
    }
    
    void Cards::deal(int deck, int card) 
    {
    All help/advice is appreciated.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It doesn't really matter how you make a shuffled deck, but all the cards need to be in the deck only once. You have to also figure out what to do if someone wants to reshuffle the cards.

    I think the instructions are rather clear about dealing -- you have a number that is the place of the top of the deck, and as cards are dealt, the top changes.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    But based on the directions, what ints should be stored in the array? It doesn't seem like 1-52 would make sense, since 1-52 has nothing to do with anything so far(does it?). I guess to get started here, I need help on what numbers need to be stored in the 52 locations of my deck[] array, and how I would automatically get the numbers into the array.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by nick753 View Post
    But based on the directions, what ints should be stored in the array? It doesn't seem like 1-52 would make sense, since 1-52 has nothing to do with anything so far(does it?). I guess to get started here, I need help on what numbers need to be stored in the 52 locations of my deck[] array, and how I would automatically get the numbers into the array.

    Can you not have an array of a user-defined type?
    So
    Code:
    Struct cards {
    int suit;
    int card;
    };
    
    array[52] cards;

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Darren, do you not mean

    cards array[52];

    It's nice if you make sure that everything you post on the forum compiles, especially if you are new to the language.

    nick, again, I think the instructions are clear. You can do something like what darren suggests, or if you think that isn't good enough, use the hundreds place for a suit number. For example, if spades is 4, then all the ranks fit in 400. 413 is the king of spades and so on.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by whiteflags View Post
    Darren, do you not mean

    cards array[52];

    It's nice if you make sure that everything you post on the forum compiles, especially if you are new to the language.

    I sure did.....Friday afternoon over here! It's been a long week

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Here is my updated code. I changed the functions to not receive any values, and made the deal function return an int. I have been working on my deal function for a while and came up with something that seems to work except one thing, when suit = 0, it will mess everything up. How can I make sure that suit will never equal 0? Here is my code, another question I have, what in the heck should go in the constructor in this program? I'm sort of confused on that. Thank you guys for helping me out and as always I appreciate any help on any part of this program. BTW the_card is the card I'm dealing.

    Code:
    #include <iostream>
    #include<ctime>
    using namespace std;
    
    
    class Cards
    {
    private:
    	int deck[52];
    	int card;
    
    public:
    	Cards(); //Constructor
    	int deal();
    	void shuffle();
    };
    
    Cards::Cards() //constructor
    {
      
    }
    
    int Cards::deal() 
    {
    	int i;
    	int the_card;
    	srand(time(NULL));
       
    	
    
    	for (i = 0; i < 52; i++)
    	{
    	int suit = rand() % (4+1)*100;
    	int value = rand() % 13 + 1;
    	deck[i] = (suit) + (value);
    	}
    	
    	the_card = deck[51];
    
    	return the_card;
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    suit = (1 + rand() % (4 - 1)) * 100;
    value = 1 + rand() % (13 - 1);
    temp = suit + value;
    This seems to be what you were missing. Do the math and you will see it finds a random number in the range. One important thing that you were missing was that the number generated could already be in the deck, so only add it to the deck if it is not there; i.e. check all the cards.

    Also I could be wrong but I do not think you were supposed to shuffle every time you deal a card.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    hmm, to check if two values are the same I would think it's something like

    Code:
    if(temp!= temp)
    deck[i]= temp;
    But I know that's wrong. I did a little searching on google and didn't find much.

    And one more thing, what should go in the constructor? So far it doesn't seem like it's even necessary??

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whiteflags View Post
    Code:
    suit = (1 + rand() % (4 - 1)) * 100;
    value = 1 + rand() % (13 - 1);
    That can't be right. % 3 means only three different suits.
    You'd want the mod of max - min + 1.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I think I did it right. The reason I did it this way was to prevent the suit from being zero, but 4 will occasionally be the suit and stuff. The method is also repeated here: Eternally Confuzzled - Using rand(). If you suggest something like rand()%4+1 then you would occassionally get 5 so that's also wrong.

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Any advice on what to put in my constructor? And how to check and see if 2 suits are identical?

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Okay, I think I got this working pretty much like it's supposed to. The one thing that is sort of stumping me, however, is the shuffle function. To shuffle, I was thinking of just allowing all of the cards to be picked from again. Any advice on a good way of doing this? I was thinking about creating another for loop to just re-populate the array with 52 cards, but that just does not seem right for some reason. Also, after I do shuffle the cards, would I just do something like
    Code:
    a.shuffle();
    for(int i =1; i < 6; i++) // using same for loop as first time I dealt first hand ??????
    	{
    
    	the_card = a.deal();
    	cout << the_card << endl;
    
    	}
    	
         
    	for(int j = 1; j < 6; j++) //using same for loop as the first time I dealt a second                hand???
    	{
    		the_card = a.deal();
    		cout << setw(15) <<  the_card << endl;
    	}

    Here is my whole program, I am hoping someone can look it over and see if I did things like the directions specified, etc. Thanks

    Code:
    #include <iostream>
    #include<iomanip>
    #include<ctime>
    using namespace std;
    
    
    class Cards
    {
    private:
    	int deck[52];
        int card ;
    
    public:
    	Cards(); //Constructor
    	int deal();
    	void shuffle();
    };
    
    Cards::Cards() //constructor
    {
    
    	card = 51;
    	srand(time(NULL));
      
    }
    
    int Cards::deal() 
    {
    int i;
    int deck[52];
    int the_card;
    
    int suit, value;
    
    int randcard = 1 + rand()% 52;
    
    
    for (i = 0; i < 52; i++)
    {
    deck[i]=i;
    }
    
    suit = deck[randcard] % (4+1)  * 100;
    value = deck[randcard] % 13 + 1;
    
    deck[card] = deck[randcard];
    deck[card] =  deck[card-1];
    
    the_card = suit+value;
    
    
    return the_card;
    }
    
    void shuffle ()
    {
    	
    
    }
    
    int main()
    {
    	int the_card;
    	Cards a;
    	
    	for(int i =1; i < 6; i++)
    	{
    
    	the_card = a.deal();
    	cout << the_card << endl;
    
    	}
    	
         
    	for(int j = 1; j < 6; j++)
    	{
    		the_card = a.deal();
    		cout << setw(15) <<  the_card << endl;
    	}
    	cin.get();
    
    	return 0;
    }

  14. #14
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'm a bit late to this thread but you don't have to use a structure and could just use the raw numbers. Suits are composed of a set number of cards so you could translate the number into whatever output you want.

    Figuring out how to shuffle the deck by passing through the deck exactly one time and yet ensuring every card is shuffled is a very good exercise. In fact I've used it as an interview question b/c it allows me to see someone work through a problem step by step which is invaluable when it comes to analyzing how different people approach problems.

    I'd be interested to see your first attempt at your own shuffle algorithm here in this thread. This will definitely get you thinking and I'm curious as to what you will come up with.

  15. #15
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    No matter what I try NOTHING seems to work when it comes to trying to making my program not deal the same card twice! I am hoping someone can point me to the right direction with this! The first way I was going about it was the above way, that isn't working. Now I tried to come up with a new way but this way isn't working either. What I am trying to do is put the last card dealt at the end of the array, then decrement the pointer by one every time I deal so that the cards at the end of the array are not deal again(before I shuffle which is just going to reset the pointer back to position 51). Please please please take a look at this and try to tell me what I'm doing wrong this time.

    Code:
    #include <iostream>
    #include<iomanip>
    #include<ctime>
    using namespace std;
    
    
    class Cards
    {
    private:
    	int deck[52];
            int *card ;
    
    public:
    	Cards(); //Constructor
    	int deal();
    	void shuffle();
    };
    
    Cards::Cards() //constructor
    {
        
    	card = &deck[51];
    	srand(time(NULL));
      
    }
    
    int Cards::deal() 
    {
       int i;
       int deck[52];
       int the_card;
    
       int suit, value;
    
       int randcard = rand()% 52;
     
    
      for (i = 0; i < 52; i++)
        {
          deck[i]=i;
         }
     
        suit = deck[randcard] % (4+1)  * 100;
        value = deck[randcard] % 13 + 1;
    
        *card = deck[randcard];
        *card --;
    
        the_card = suit+value;
    
    
         return the_card;
    }
    
    void shuffle ()
      {
    	
    
      }
    
    int main()
    {
    	int the_card;
    	Cards a;
    	
    	for(int i =1; i < 6; i++)
    	{
    
    	the_card = a.deal();
    	cout << the_card << endl;
    
    	}
    	
         
    	for(int j = 1; j < 6; j++)
    	{
    		the_card = a.deal();
    		cout << setw(15) <<  the_card << endl;
    	}
    	cin.get();
    
    	return 0;
    }
    Last edited by nick753; 11-24-2010 at 04:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  2. Bingo Card Program. Any input?
    By dukebdx12 in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2008, 03:00 PM
  3. Replies: 2
    Last Post: 11-07-2003, 12:21 AM
  4. Card Dealing Problem
    By curlious in forum C++ Programming
    Replies: 16
    Last Post: 08-19-2003, 06:47 PM
  5. card shuffling dealing question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2002, 08:37 PM