Okidoky.
You can download the source files from my site:
http://home.tiscali.nl/~valmont/cpp/.../blackjack.zip
Just unzip it in a folder and then double clock on "Main.cpp".
Then compile it and then things will go smooth.
It is exactly the same code as posted above. Ready to run, just see what happens.
Here is a drawing of what is going on:
Let me explain what is going on here.
Your class
CardGames is a "plug-in machine".
And your class
BlackJack is module that can be plugged into your CardGames machine.
If you derrive more classes from the CardGames machine, then you are basically creating more modules for your CardGames machine. For example a class Poker. You name it.
Just implement the
pure virtual methods (wich you see in the base class) in your derrived classes.
About pure virtual functions:
A pure virtual function is just like a ordinary virtual function. But instead of code, you add
= 0 ; after the declaration.
So this time, you are not only able to override it in your derrived classed, but you MUST implement them in your derrived classes. A pure virtual function is a
order:
Quote:
I am a PURE virtual function so you MUST and WILL implement me in classes derrived from me.
If you don't, the compiler will make your life a misery!
|
[u]About the vector from the <vector> header[u]
A vector is a sort of an array, but is very flexible and easy to handle. It is really simple but the simplicity is a bit obscured by another Standard Template Library item: the "pair". More on that coming up. But understand that learning the ways of vector is very important. It is much easier then using arrays and all. And occasionally even more efficient in terms of run-time-speed management. I fyou need help on this, then go ahead.
[u]About the "pair" from the <utility> header[u]
First of all this. Although the structure "pair" is defined in the include file <utility>, we don't need to add that header because we are using a "vector" (wich was defined in <vector>). In this very case, the <utility> header is automatically included.
Ok, so here is what it does:
A card is distinguished by its value and its suit. Since this is such a unity, I use a "pair" structure to implement the unity:
pair<int, int>(diamonds, 12)
But where do I store a "pair"? Well, I decided to store in a "vector".
That's why you see this strange code:
vector<pair<int, int> > Deck; Wich is the Deck that will hold all the pairs. And then:
Deck.push_back(pair<int, int>(suit, value)) ;
"push_back()" is a method from "vector". It pushed the next value in the vector. We "push_back" 52 times, so Deck has 52 pairs of "suit-value" items.
To view what is in it we do:
Code:
for(vector<pair<int, int> >::iterator i = Deck.begin(); i != Deck.end(); ++i)
{
cout<< i->first <<" "<< i->second<<endl;
}
An "iterator" is a thingy that belongs to the vector (int this case). I load it with a Deck:
Code:
for(vector<pair<int, int> >::iterator i = Deck.begin(); }
And now we can use the iterator (wich we called "i") to traverse the vector.
This seems a bit weird:
Code:
cout<< i->first <<" "<< i->second<<endl;
But this is the way to access the "first value of a pair (suits) and the second value (value of the card).
[u]About main()[u]
I bet you didn't see such simple code in a long time. The main function is often called a "driver". Nevermind what that exactly is, but realize that if you make a library or libraries (and you are, since our classes are libraries in a sense), you want client coders to be able to code dynamically yet easy.
I think I achieved the easy part. More is not needed since that wasn't the assignment I believe.
[u]About You[u]
- Never ever touch the main(). Don't change a single thing!!!
Yes, you may remove (I forgot).
Code:
#include "Player.h"
#include "card.h"
Then forget main(). It doesn't exist anymore.
- I implemented your shuffle and your basic Deck setup.
Try to implement the rest.
- NO. You may NOT touch class "CardGames". Illegal too. Bzzt

.
- NO, You may NOT touch class "Card". We defined card, we have Deck. So leave it.
- NO. You may NOT touch the class "Player" until you have a fully functional and correct black jack game. The game only needs to know how much players there are, and I provided that implementation as well. You
could let player "1" (or "0" if you will) always be the "computer dealer". Up to you. But that has NOTHING to do with the "Player" class. So :th: to class "Player" for now.
- Yep, you only need black jack to implement the gamerules so it can play black jack.
a) Dealing cards is a rule.
b) Shuffling is a rule.
c) Checking for wagers.
c) Observing wich player number is should receive cards and all.
You name it.
I gotta work until late tomorrow. But I'll be back home eventually I hope. Then I'll be able to help you further.
Good luck!