Here it is. I think you'll like it

.
It is complete and working. You can bet some mone on it

.
blackjacvc6final.zip - Game ends when player has no more money.
- Card deck is recreated and shuffled if holds less then 33% of the cards. But game continues if player still has money.
- Class "card.h" defines a pair<string, unsigned> aCard. Since a card is known for its two-unit "suit" and "value".
- But it is the class Card that actually IS a single card and therefore is the complete definition of a card:
it has a pair-unit and some methods (member functions). These things makes a card a card.
Think of a human. It doesn't have only arms and legs. But also some behaviour (give cup of tea), and properties (sleeping, working). - The card defines also an overloaded operator "<<".
Such an operator is naturally used or output to screen (on our example), but by overloading it, it will show a neat output like:
Heart Ace
instead of
3 14 - The deck: in class Deck.h naturally.
The core of creating a deck is placing all the individual "cards" (as defined in card.h) in a vector. Here is the core of the deck definition:
vector<Card> theDeck;
theDeck.push_back(Card(pair<string,unsigned>(suit, 2+i%13) ));
Besides creating a deck of cards, it also defines some minimal but highly usefull methods as well. Obviously because a "deck of cards" has a behaviour as well.
Lets analyze what happens in class Deck.h:
First of all,
Deck is NOT a derrived class from class
Card. Why is that? Well easy:
a deck IS NOT A KIND OF a card.
Therefore: a deck IS NOT a card. So NO derrivation from card it is.
In the Deck class, I could have (and should have) created a Deck in its constructor. But instead I made an initialisation method called "CreateDeck". I did this so you can create new decks on-the-fly in the game (in BJ we fetch a new deck if deck has less than 33% of cards). But you may ask yourself if this is really OO programming

! Nevermind that for now though.
The method
Deck::Show(ostream &os) belongs in the
deck class indeed since the deck is responsible on how it shows itself to the public. Just like you or me are in real life.
So now you know why
Shuffle() and
RemoveTopCard are in the
Deck class too

.
Quote:
In Card class
Card(pair<string, unsigned> crd) : aCard(crd) {}
: aCard(crd) Could you explain this?
|
I was just on my way to do that

.
Lets analyze
theDeck.push_back(Card(pair<string,unsigned>(suit, 2+i%13) )) for a moment again.
Usually when you put something in a
vector you do:
vector myVec<int>;
myVec.push_back(5);
Easy huh? :rock:
But our deck doesn't store integers. It stores cards. And our card was defined in the class
Card.
So I could have done easely:
Code:
Card c:
theDeck.push_back(c);
But before I do that, I need to set the card with a card -suit and value pair:
Code:
Card c(pair<string,unsigned>(suit, 2+i%13));
theDeck.push_back(c);
But I don't need card objects, so I use a trick wich does the same thing in one line. In that line, first the constructor of
card is called (loads the card with the values passed on) and then I load my Deck vector with that particular card:
Code:
vector<Card> theDeck;
theDeck.push_back(Card(pair<string,unsigned>(suit, 2+i%13) ));
I can do this because my
Card constructor accetpts a pair value. This is a typical occasion where a class (like our
Card class) doesn't have a default constructor defined by us, since it is of no use. Instead we have that other constructor:
Code:
Card( pair<string, unsigned> crd );
Observe how this is just a SIMPLE constructor wich accepts a single value:
Code:
//EXAMPLE, NOT IN OUR ORIGINAL CODE
//observe how simple this constructor is actually by comparing
//a fictional case:
WeirdCardClass(int crd) { }
Card( pair<string, unsigned> crd ) { } Quote:
In Deck class
theDeck.erase, .begin etc. are all these the methods in Vector?
|
Yes.
The vector, wich is part o the STL (Standard Template Library) offers lot of such method. You only have to know them and know a bit how to use them. The vector itself will manage the annoying things like memory allocation, memory freeing etc..
That's why "we" love the STL so much. In fact, if you think of using the good old array again, try to use vector instead.
The vector has template arguments. So you could dump just about anything in a vector. Even vectors in vectors. That's a nice alternative to creating multidimensional arrays. But without fuss.
And don't even
think an array executes faster. In complex situations the vector methods
may be even faster. You won't lose, that you can count on it in serious applications.
Here is another example of the power of STL (wich we did use in our code):
Code:
void Shuffle()
{
static bool seeded;
if (!seeded)
{
srand(time(0));
seeded = true;
}
random_shuffle(theDeck.begin(), theDeck.end());
} I am talking about
random_shufle.
This requires the <algorithm> header to be included.
Okidoky. It is VERY early here, and I just created the proggy from scratch for you. You deserve it. Now I need a cappucino, and then to work. I will occasionally lurk for this site at work as I do usually, so if you have questions, ask them anytime.
Be nice and tell me what grade you got for this assignment

.
Notice that we don't use
all the methods I created in Deck.h and card.h. One rule is:
1) Make your classes minimal.
But the second rule is:
2) Make your classes functional.
So that's why I added a few common occuring methods like
accessor methods. These end with the
const keyword: to make sure we can't modify our content. That is especially important when we return a reference.
- Val -