Thread: Playing with the STL.

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Playing with the STL.

    So i finished my Blackjack game project (http://cboard.cprogramming.com/showthread.php?t=91714). It works fine, but as many of you pointed out in my first thread, it's not very well written, and the source code is also a bit too long for my liking.

    So i sat down and started thinking about what i wanted to include in a Blackjack 2.0. I've now made a prototype of how my class definition for the Deck class might look, considering what you said in my previous thread.

    This is how it looks, in all it's glory:

    Code:
    #ifndef DECK_H
    #define DECK_H
    
    class Deck {
    
    	public:
    		Deck();
    		void Shuffle();
    		void GetCard(std::vector &Card);
    		
    	private:
    		struct Card {
    			enum Rank { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, 
    					    EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
    					  
    			enum Suit { SPADES, CLUBS, HEARTS, DIAMONDS };
    		};
    		
    		const unsigned int DeckSize;
    		std::vector<Card> Deck;
    };
    
    #endif
    As you can see, i've decided to use a struct for my Cards, rather than an additional class, and then just integrate it into my Deck class. My plan is to save all the cards in a vector of structs, so i can use the STL functions for shuffling, rather than having to write my own again.

    I'm not very familiar with the STL, so i have a couple of questions to begin with:

    1. The above code is from a header file, should i define the Card struct as i have done, or do it in the .cpp file? (Is that even possible? Can structs have prototypes like functions?)

    2. Can i declare my vector the way i did, or do i have to typedef the struct first?

    3. Is there a better way of doing it, than using a struct for the Cards, and a vector for the deck?

    Thanks in advance for your help

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    1. Defining a struct in a header is fine.

    2. Yes. You don't have to typedef a struct in C++

    3. Looks fine to me.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I believe that you are declaring the enum within the struct, but you do not have an instance of the enum variable that you can store the constant in. You can create the enum variable by just doing Rank "varname"; and Suite "varname" to make it so it only allows integer values that fall within the enum to be assigned to it. You could just add it to the very end of your enum declaration.
    Code:
       enum Suit { SPADES, CLUBS, HEARTS, DIAMONDS } varSuit;

    1. You want to put declarations in a header file and definitions(implementation) in a cpp file. A struct is a data type. It is like a class in that you can create functions to act on the data with the only difference that the default state is public not private. If you are going to have functions to act on the data in the struct, you might as well make it a class. If you are using just as data, then you can treat it just like a variable declaration and put it in the header file. What you have is just the declaration, you have not actually created an instance of the struct. You have created a vector that can hold them.

    2. Your vector is fine. As long as you declare your struct first, you can then use it to make a vector. Typedef is used to create an alternate name usually to shorten a lengthy statement and make it easier to create objects and pointers to that object. You do not have to use a typedef.

    3. If there is a better way, it really depends on what you are trying to do and what your criteria are for the program. Are you making a single player project, or a networked multiplayer with hundreds of clients. If you are trying to learn object oriented design then I would recommend making the card struct a separate class instead of a struct. The reason being that with black jack you are going to have a minimum of two hands, one for you and one for the dealer. You will have to pass the cards to each hand as you deal them. So each hand will probably have a vector<Cards> to store them. The way you have it now, only the Deck class will be able to use the Card struct data type as you have made it private.
    Last edited by manofsteel972; 07-18-2007 at 09:24 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM