### How does this work?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260`` ``````// Blackjack.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include using namespace std; class Card { public: enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}; enum suit {CLUBS, DIAMONDS, HEARTS, SPADES}; // overloading << operator so can send Card object to standard output friend ostream& operator<<(ostream& os, const Card& aCard); Card(rank r = ACE, suit s = SPADES, bool ifu = true); // returns the value of a card, 1 - 11 int GetValue() const; // flips a card; if face up, becomes face down and vice versa void Flip(); private: rank m_Rank; suit m_Suit; bool m_IsFaceUp; }; Card::Card(rank r, suit s, bool ifu): m_Rank(r), m_Suit(s), m_IsFaceUp(ifu) {} int Card::GetValue() const { // if a card is face down, its value is 0 int value = 0; if (m_IsFaceUp) { // value is number showing on card value = m_Rank; // value is 10 for face cards if (value > 10) value = 10; } return value; } void Card::Flip() { m_IsFaceUp = !(m_IsFaceUp); } class Hand { public: Hand(); virtual ~Hand(); //adds a card to the hand void Add(Card* pCard); // clears hand of all cards void Clear(); // gets hand total value, inteillgiently treats aces as 1 or 11 int GetTotal() const; protected: vector m_Cards; }; Hand::Hand() { m_Cards.reserve(7); } Hand::~Hand() // don't use the keyword virtual outside of class definition { Clear(); } void Hand::Add(Card* pCard) { m_Cards.push_back(pCard); } void Hand::Clear() { // iterate through vector, freeing all memory on the heap vector::iterator iter = m_Cards.begin(); for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) { delete *iter; *iter = 0; } // clear vector of pointers m_Cards.clear(); } int Hand::GetTotal() const { // if no cards in hand, return 0 if (m_Cards.empty()) return 0; // if a first card has a value of 0, then card is face down; return 0 if (m_Cards[0]->GetValue() == 0) return 0; // add up card values, treat each ace as 1 int total = 0; vector::const_iterator iter; for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) total += (*iter)->GetValue(); // determine if hand contains an ace bool containsAce = false; for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) if ((*iter)->GetValue() == Card::ACE) containsAce = true; // if hand contains ace and total is low enough, treat ace as 11 if (containsAce && total <= 11) // add only 10 since we've already added 1 for the ace total += 10; return total; } class GenericPlayer : public Hand { friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer); public: GenericPlayer(const string& name = ""); virtual ~GenericPlayer(); // indicated whether or not generic player wants to keep hitting virtual bool IsHitting() const = 0; // returns whether generic player has busted - has a total greater than 21 bool IsBusted() const; // announces that the generic player busts void Bust() const; protected: string m_Name; }; GenericPlayer::GenericPlayer(const string& name): m_Name(name) {} GenericPlayer::~GenericPlayer() {} bool GenericPlayer::IsBusted() const { return (GetTotal() > 21); } void GenericPlayer::Bust() const { cout << m_Name << " busts.\n"; } class Player : public GenericPlayer { public: Player(const string& name = ""); virtual ~Player(); // returns whether or not the player wants another hit virtual bool IsHitting() const; // announces that the player wins void Win() const; // announces that the player loses void Lose() const; // announces that the player pushes void Push() const; }; Player::Player(const string& name): GenericPlayer(name) {} Player::~Player() {} bool Player::IsHitting() const { cout << m_Name << ", do you want a hit? (Y/N): "; char response; cin >> response; return (response == 'y' || response == 'Y'); } void Player::Win() const { cout << m_Name << " wins.\n"; } void Player::Lose() const { cout << m_Name << " loses.\n"; } void Player::Push() const { cout << m_Name << " pushes.\n"; } class House : public GenericPlayer { public: House(const string& name = "House"); virtual ~House(); // indicates wheter house is hitting - will always hit on 16 or less virtual bool IsHitting() const; // flips over the first card void FlipFirstCard(); }; House::House(const string& name): GenericPlayer(name) {} House::~House() {} bool House::IsHitting() const { return (GetTotal() <= 16); } void House::FlipFirstCard() { if (!(m_Cards.empty())) m_Cards[0]->Flip(); else cout << "No card to flip!\n"; } int _tmain(int argc, _TCHAR* argv[]) { return 0; }``````

 ``12345`` `````` // add up card values, treat each ace as 1 int total = 0; vector::const_iterator iter; for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) total += (*iter)->GetValue();``````

The second code block above, how does it work?
It is an iterator of type Card* and from the looks of it, it is doing exactly what any iterator will do. It is going through your vector called m_Cards and from the start of the vector, it is adding up the value of each card.

I bet line 5 is the one confusing you. I am stumped on that one to, I have no idea why you can't just do `iter->GetValue() `
Same. Could you possibly explain how GetValue works? :).
Bump.
What's to understand?
 ``123456789101112`` ``````int Card::GetValue() const { // if a card is face down, its value is 0 int value = 0; if (m_IsFaceUp) { // value is number showing on card value = m_Rank; // value is 10 for face cards if (value > 10) value = 10; } return value; }``````

The code is distinguishing between cards that are face up or not. If a card is not face up (dealer's hole card), then it is not included in the computed value. The value is set to the rank of the card, unless it's a face card, then it is set to 10 since all face cards have a value of 10. Then the computed value is returned.

The book I have doesn't explain things very well for a beginner. I just needed the explanation that was provided above.

Thanks.
 ``12345`` `````` //add up card values, treat each Ace as 1 int total = 0; vector::const_iterator iter; for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) total += (*iter)->GetValue(); ``````

What does `vector<Card*>`. What does Card* do?
It's the vector type. It's declaring an iterator for a vector of Card*s
Could I name it to whatever I wanted to?
If this book is intended for beginners, it's a terrible book. I suggest you brush up on your knowledge before digging any deeper into this particular program.
use youtube and two books and this sites tutorials to structure your learning and you will learn at optimum speed
What two books do you recommend?
c++ for dummies isn't as easy as it sounds but it is ideal, the learning curve with books is irritating for me, i think i have a hard time interpreting what they mean, just with this book and another and a video you have more perspective on each concept, like having three different explanations on the same thing,

I found this way my brain had more data making interpreting an abstract idea into reality
Topic archived. No new replies allowed.