/* 
   Hand.h - Class for a hand of cards.  Since a hand of cards IS_A list of
            cards, this is derived from base class List.  Since List is a 
            class template, Hand must also be a class template.
*/


#ifndef HAND_H
#define HAND_H

#include "List.h"


template<class T>
class Hand : public List<T>  {

public:
   
    Hand()  {}                                             
       
    void display();                                      // Display the hand.
    void display(int n);                                 // Display n items in the hand.
                         
    Hand<T>& reset();                                    // Reset the hand to zero.  
    T operator[](int n);                                 // Subscript operator.
};


/* Function definitions for Hand.h */


// Overload the subscript operator for easy access to the cards in a hand.  This is
//  useful because every card game has different rules for assigning point values to
//  the cards.  Usually the point values aren't trivial (in Blackjack, the Ace is
//  1 or 11 depending on the player's total.)

template<class T>
T Hand<T>::operator[](int n)
{
    int count = 1;               
    ListIterator<T> i(*this);
    while ( count != n )  {
        i.advance();
        count ++;
    }
    return i.current();
}


template<class T>
void Hand<T>::display() 
{
    for (ListIterator<T> i(*this); i.more(); i.advance() ) 
    {
       cout << "                       " ;
       i.current()->display_card(); 
    }
    cout << endl;
}


/* Display n cards from the hand. */

template<class T>
void Hand<T>::display(int n) 
{

    if ( n > num_elts )
    {
       cout << " Exceeded subscript range.  Displaying all cards. " << endl;
       n = num_elts;
    }

    int count = 0;
    ListIterator<T> i(*this);
    while ( count != n )  
    {
       cout << "                       " ;
       i.current()->display_card();
       i.advance();
       count ++;
    }
    
    for (int i = 0; i < num_elts-n ; i++) 
    {
       cout << "                        " ;
       cout << "HIDDEN CARD" << endl;
    }
}


/* Reset the hand, so the Player can play another round. */

template<class T>
Hand<T>& Hand<T>::reset() 
{
   for (ListIterator<T> i(*this); i.more(); i.advance()) 
        this->remove(i.current());
}

#endif
