// File: ex4-5.cpp
#include <iostream>
#include <cstdlib> // needed for rand() function
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 Card
{
private:
int value;
int suit;
public:
Card(int=0);
int get_value() const { return value;} // accessor function
int get_suit() const { return suit;} // accessor function
void print() const;
};
Card:Card(int x)
{
x = abs(x)%52; // make sure x is between 0 and 51
value = x % 13;
suit = x / 13;
}
void Card::print() const
{
cout << (ValueName[value]) << " of "
<< (SuitName[suit]) << endl;
}
class Deck
{
private:
Card** d;
int size;
int next_Card;
public:
Deck(int s = 52);
~Deck(void);
void shuffle(void);
void deal(int=5);
void print(void);
};
Deck::Deck(int s)
{
size = s;
d = new Card*[size];
for (int i = 0; i < size; i++) d[i] = new Card(i);
next_Card = 0;
}
Deck::~Deck(void)
{
for (int i = 0; i < size; i++) delete d[i];
delete [] d;
cout << "The Deck is gone" << endl;
}
void Deck::shuffle(void)
{
int i, k;
Card* temp;
cout << "I am shuffling the Deck\n";
for (i = 0; i < size; i++)
{
k = rand() % size;
temp = d[i];
d[i] = d[k];
d[k] = temp;
}
return;
}
void Deck::print(void)
{
for (int i = 0; i < size; i++)
d[i]->print(); // same as (*d[i]).print()
return;
}
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[next_Card++]->print();
return;
}
int main (void)
{
Deck poker;
poker.shuffle();
poker.print();
poker.deal();
poker.deal(3);
return 0;
}