/* Deck.h - Class for a deck of cards.  The default size for the deck is
            52 cards. */


#ifndef DECK_H
#define DECK_H

#include "Card.h"
#include "Random.h"


class Deck  {
public:

    Deck(int n = 52);                                    // Create a deck of cards
    ~Deck();

    Deck&   initialize();                               
    Deck&   shuffle();				 

    Card*   deal();                                      // pointer to next card
    Card*   deal_random();
    
    int     Cards_in_Use()  { return cards_in_use; }
    void    display_deck();			          

private:

    Card**  ptr_to_card;                                 // Array of card pointers.
    int     num_cards;				         // Total # of cards in deck.
    int     cards_in_use;			         // # of cards dealt.
    int     value;                                       
    static  Random rand;                                 // only 1 instance of Random!

};


/* Functions for Deck class */

Random Deck::rand;

Deck::Deck(int n = 52) 
{	
    num_cards    = n;			
    cards_in_use = 0;				           // 0 cards have been dealt.
    ptr_to_card  = new Card*[num_cards];
    initialize();
    shuffle();
}


Deck::~Deck()
{
    for (int i=0; i < num_cards; i++)   delete ptr_to_card[i];
    delete [] ptr_to_card;
}  


/* Initialize a standard deck in sequence.  The suit ranges from 0 to 3;
   the Rank ranges from 1 to 13.  "Unfair decks" have not been implemented
   in any logical way, but the following code will make sure that at least
   the program will not crash for strange deck sizes.
*/

Deck& Deck::initialize() 
{
    Card  temp;
    
    if (num_cards == 52)
    {
       for (int i = 0 ; i <= 3; i++) 
          for (int j = 1; j <= 13; j++) 
          {
            temp.int_to_Suit(i);		               // Change temp's suit & rank,
            temp.int_to_Rank(j); 		               //   based on i & j.
            ptr_to_card[i*13+j-1] = new Card( temp );
          }
    }
    else
    {
       for (int i = 0; i < num_cards; i++)  
          ptr_to_card[i] = new Card( temp );
    } 
    return *this;  
} 


/* Shuffle the deck:  Pick a random number called "value".  Swap the first
   card in the deck with the card that is located in ptr_to_card[value-1] .  Repeat 
   this process for the second card in the deck, and so on.
   (Note we must swap the VALUE of the cards, and not the ADDRESS of the cards!)
*/

Deck& Deck::shuffle() 
{
    for (int i = 0; i < num_cards; i++)
    {
        value = rand.get_random(num_cards) + 1;
        Card temp = *ptr_to_card[i];
        *ptr_to_card[i] = *ptr_to_card[value-1];   
        *ptr_to_card[value-1] = temp;
    }
    return *this;
}


/* Return a pointer to the next card in the deck.  If all cards have already been 
   dealt, then reshuffle the deck and set cards_in_use to zero. */

Card* Deck::deal()
{
    if ( cards_in_use == num_cards )
    {
       shuffle();     
       cards_in_use = 0;
       cout << endl << "* The deck has been reshuffled! *" << endl << endl;
    } 
    
    cards_in_use++;
    return ptr_to_card[cards_in_use-1];
}


/* Return a pointer to a random card in the deck.  In a Las Vegas casino,
   this is equivalent to using several decks at a time to discourage 
   "card counting."  Basically, I am dealing a card from an infinite
   number of well-shuffled decks. */
   
Card* Deck::deal_random() 
{
    value = rand.get_random(num_cards) + 1;
    cards_in_use++;
    return ptr_to_card[value-1];
}

void Deck::display_deck()
{
    for (int i = 0; i < num_cards; i++) ptr_to_card[i]->display_card();
}

#endif
