import java.util.Random;

/**
 * An encapsulation of a (52-card) deck of "playing cards"
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class Deck
{
    private Card[]      cards;
    private int         top;

    private static final int deckSize = 52;


    /**
     * Default Constructor
     *
     * Note: When a Deck is constructed it is not shuffled
     */
    public Deck()
    {
	int      index, suit, value;

	cards = new Card[deckSize];
	top   = 0;

	index = 0;
	for (suit=Card.CLUBS; suit <= Card.SPADES; suit++)
	{
	    for (value=2; value <= Card.ACE; value++)
	    {
		cards[index] = new Card(suit, value);
		index++;
	    }
	}
    }


    /**
     * Determine if this Deck has any cards that have
     * not yet been dealt
     *
     * @return true if another Card can be dealt, false otherwise
     */
    public boolean hasMoreCards()
    {
	boolean       more;

	more = false;
	if (top < cards.length) more = true;

	return more;
    }


    /**
     * Get the next Card in this Deck (if one is available)
     *
     * @return  The next Card (or null if no Card is available)
     */
    public Card nextCard()
    {
	Card    cardToReturn;

	cardToReturn = null;
	if (hasMoreCards()) 
	{
	    cardToReturn = cards[top];
	    top++;
	}

	return cardToReturn;
    }



    /**
     * Shuffle this Deck of cards 
     * (using the Fisher-Yates algorithm)
     */
    public void shuffle()
    {
	Card        temp;
	int         i, j;
	Random      rand;


	top  = 0;
	rand = new Random();

	for (i=0; i < cards.length; i++)
	{
	    // Generate a pseuod-random number in [i, cards.length)
	    j = rand.nextInt(cards.length - i) + i;

	    // Swap cards i and j
	    temp = cards[j];
	    cards[j] = cards[i];
	    cards[i] = temp;
	}
    }

}
