How would you represent a deck of cards?

Started by
21 comments, last by vinb 19 years, 6 months ago
I'm experimenting with making a blackjack game and I'm not sure how I want to represent the deck of cards in a class. I was thinking either an array of 52 Card objects which have a suit and a value property. I'm sure a lot of you have done a card game before and I was just wondering what more experienced people have done in the past. Thanks.
Advertisement
As a std::deque<Card>, obviously. [smile]

And you might be happy to hear that C++ does have a shuffling function, called std::random_shuffle in the <algorithm> header.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I actually looked at this just a few weeks ago. =)

The most natural approach would obviously be to use a std::deque of cards.

A card would then have a value and a suit.

You might not want to limit your "deck" class to 52 cards, but rather an arbitrary number of cards for blackjack where you can play with for instance 4 normal "decks" packed into one.

You then fill a deck using the rules of the current game.

Atleast that was the approach I planned on using, lemme know if it works out =)

Edit: doh, Fruny beat me to it =)
I figured if I asked I could avoid making a call to wheel.reinvent(). Are there any other useful things like this that might pertain to card games? I wanted to try a few of them before getting into graphics and stuff.
Quote:Original post by vinb
I figured if I asked I could avoid making a call to wheel.reinvent(). Are there any other useful things like this that might pertain to card games? I wanted to try a few of them before getting into graphics and stuff.

A wise choice. I'd suggest you implement a text version first, just to try your implementation out. =)
Quote:Original post by DEVLiN
You might not want to limit your "deck" class to 52 cards, but rather an arbitrary number of cards for blackjack where you can play with for instance 4 normal "decks" packed into one.

You then fill a deck using the rules of the current game.


Interesting. I was actually thinking of using an array of Deck objects to handle that. That way the user could set a difficulty level of choosing how many decks to play with. But for games that don't use a standard deck size, that approach would work in both situations and I wouldn't have to mess with it. I'll post the code when I'm done. Oh, that reminds me, is this the proper area of the site to post code? You know, just to share. Or is there another place to do that? I tried it a couple of times and I'm not sure how useful it was to anyone.
im me, I will give you some code, my in is " the c programmer
Thanks, but I don't have IM. Could you post it here?
Quote:Original post by Fruny
As a std::deque<Card>, obviously. [smile]


Booooo... bad joke. (So why can't I stop grinning?) [smile]
Joke? That is what you call self-commenting code.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.

This topic is closed to new replies.

Advertisement