// the Card and Deck
example with nested classes
#include
<iostream>
#include
<cstdlib>
using namespace std;
const char*
ValueName[13] =
{"two","three","four","five","six",
"seven","eight","nine","ten","jack","queen","king","ace"};
const char*
SuitName[4] =
{"clubs","diamonds","hearts","spades"};
class Deck
{
public:
// Why is this public?
class Card
{
private:
int value;
int suit;
public:
void assign(int);
int get_value() const;
int get_suit() const;
void print() const;
};
private:
Card d[52];
int nextCard;
public:
void creakDeck();
void shuffle();
void deal(int=5);
void print() const;
};
int
Deck::Card::get_value() const
{
return
value;
}
int
Deck::Card::get_suit() const
{
return
suit;
}
void
Deck::Card::assign(int x)
{
value = x
% 13;
suit = x
/ 13;
}
void
Deck::Card::print() const
{
cout
<< (ValueName[get_value()]) << " of "
<< (SuitName[get_suit()]) << endl;
}
void
Deck::creakDeck()
{
for (int
i = 0; i < 52; i++) d[i].assign(i);
nextCard
= 0;
}
void Deck::shuffle()
{
int i, k;
Card temp;
cout
<< "\nI am shuffling the Deck\n";
// swap
the ith card with random card
for (i =
0; i < 52; i++)
{
k = rand() % 52;
temp = d[i];
d[i] = d[k];
d[k] = temp;
}
}
void Deck::print()
const
{
cout
<< "\nHere's the Deck:\n";
for (int
i = 0; i < 52; i++) d[i].print();
}
void Deck::deal(int
no_of_Cards)
{
cout
<<"\nOk, I will deal you
"<<no_of_Cards<<" Cards:\n";
for (int
i=0;i<no_of_Cards; i++) d[nextCard++].print();
}
int main ()
{
//
Card
C;
// Why won't this work?
Deck::Card C;
C.assign(17);
C.print();
Deck
poker;
poker.creakDeck();
poker.print();
poker.shuffle();
poker.print();
poker.deal();
poker.deal(3);
}