Sign in to follow this  
m0ng00se

load a vector

Recommended Posts

m0ng00se    104
Hi Decided to write a poker game to add to my existing chatbot game. Just started writing it and wondered how to load a vector up with cards so I can shuffle them. I use vectors all the time but I usually push the contents in from a script. The issue is how do I load up my own type into a vector. Do I need to dump it into an array somehow first then cycle the array? Here is the start of my class. All I really want to know is how to get my card types into a vector.

class pack{

public:

//	enum suit {club, diamond, heart, spade};
//	enum denomination {ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king};

	enum card {ACE_H, TWO_H, THREE_H, FOUR_H, FIVE_H, SIX_H, SEVEN_H, EIGHT_H, NINE_H, TEN_H, JACK_H, QUEEN_H, KING_H,
				ACE_D, TWO_D, THREE_D, FOUR_D, FIVE_D, SIX_D, SEVEN_D, EIGHT_D, NINE_D, TEN_D, JACK_D, QUEEN_D, KING_D,				
				ACE_C, TWO_C, THREE_C, FOUR_C, FIVE_C, SIX_C, SEVEN_C, EIGHT_C, NINE_C, TEN_C, JACK_C, QUEEN_C, KING_C,
				ACE_S, TWO_S, THREE_S, FOUR_S, FIVE_S, SIX_S, SEVEN_S, EIGHT_S, NINE_S, TEN_S, JACK_S, QUEEN_S, KING_S };
	
 ~pack();


private:
};

#define ACE_H		"ACE OF HEARTS"
#define TWO_H		"TWO OF HEARTS"
.... etc
.....etc 



thanx for any advice m0ng00se

Share this post


Link to post
Share on other sites
have you thought about putting the card into a structure (or even its own class)?

struct card
{
int num; //1 - 13 (ace to king)
int type; //1 - 4(clubs to diamonds)
bool joker; // not required i guess
card(int _num, int _type) { num = _num; type = _type; }
};

then make ur vector templated: vector<card>

Share this post


Link to post
Share on other sites
m0ng00se    104
Um, as I posted I remembered that enum types have consecutive integer values so I suddenly just this second wondered if I can do something like:

for (card = 0, card < 52, card++) {
myVector.push_back(card);
}

????

I'll go try it.

m0ng00se

Share this post


Link to post
Share on other sites
m0ng00se    104
Hi Dave

yes this is actually my second attempt. My first attempt had a base class of "card" rather than "pack".

Ideally I need to inherit card from pack or pack from card. I can't really decide but as you rightly point out they're completely different object types with completely different properties... though still related.

I guess card should be inherited from pack?

m0ng00se

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by m0ng00se
Hi Dave

yes this is actually my second attempt. My first attempt had a base class of "card" rather than "pack".

Ideally I need to inherit card from pack or pack from card. I can't really decide but as you rightly point out they're completely different object types with completely different properties... though still related.

I guess card should be inherited from pack?

m0ng00se


Nononono. Inheritance means "is a kind of". A card is not a kind of a pack. A pack is not a kind of a card. A pack contains cards. You model "contains" by, well, containing things.


class Pack {
std::vector<Card> cards;
// etc. as needed
};

Share this post


Link to post
Share on other sites
A pack is made up of 52 cards, it itself isn't a card - neither are any of the cards a pack. So no inheritence should exist, however there is a relationship between them: as represented as the bold writting.

If you think about it, the only card that you're concerned about is the one at the top of the deck, so a stack might be a better storage type.
A Pack class could include a stack (or vector) of cards objects, but then again you don't really need a seperate class to define a pack.

Share this post


Link to post
Share on other sites
m0ng00se    104
Yes that is true. I kept thinking a card is a member of pack (in real world terms) so my card functions should be members of a pack class. Sort of back to when I was at uni and our tutor kept telling us to always try and model the "real world" objects with our C++ classes. When you put it like that it doesn't really make much sense though to include the card stuff in a pack class.

Yes I was also thinking a stack is all I need. It's not really a biggy either way though. Shuffling a vector, or popping cards off a stack is only a few lines of code.

The hard part is going to be evaluating every hand dealt for all the various poker winning combinations. I almost need a "hand" type as a superset of "card" because it is the complete hand dealt that I will be evaluating in a loop.

Thanx for the feedback.

m0ng00se

Share this post


Link to post
Share on other sites
m0ng00se    104
PS I thought I might add that it will be fully animated. I already have the .png files for each card, hence the reason I decided to make each card a seperate item rather than use a simple struct of four "suits" and 13 values that I then have to match to the .png I'm loading. Which makes no sense really because I still have to do that match anyway to evaluate the "hand."

Still either way will give the same result in the end.

m0ng00se

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this