import java.util.*;

public class Deck
{
	private ArrayList m_cardsLeft;
	
	public Deck()
	{
		createShuffledDeck();
	}
	
	public void shuffle()
	{
		createShuffledDeck();
	}
	
	public int cardsLeft()
	{
		return m_cardsLeft.size();
	}
	
	public Card nextCard()
	{
		Random generator = new Random();
		int index = generator.nextInt(cardsLeft());
		//System.out.println(index + " " + cardsLeft());
		return (Card)m_cardsLeft.remove(index);
	}
	
	private void createShuffledDeck()
	{
		m_cardsLeft = new ArrayList();
		
		for (int i = 0; i < 13; i++)
		{
				add(i, "Hearts");
				add(i, "Spades");
				add(i, "Clubs");
				add(i, "Diamonds");
		}
	}
	 
	private void add(int rank, String suit)
	{
		String rankStr = stringifyRank(rank);
		
				
		Card card = new Card(rankStr, suit);
		m_cardsLeft.add(card);
	}
	
	private String stringifyRank(int rank)
	{
		if (rank == 1)
			return "A";
			
		if (rank == 11)
			return "J";
				
		if (rank == 12)
			return "Q";
				
		if (rank == 0)
			return "K";	
			
		return (rank + "");			
	}
	
	public static void main(String[] args)
	{
		Deck deck = new Deck();
		while(true)
		{
			if(deck.cardsLeft() == 0)
			{
				deck.shuffle();
				Dialog.message("shuffle");
			}
				
			
			Card card = deck.nextCard();
			Dialog.message("your card is a " + card.getRank() + " of " + card.getSuit());	
		}
	}
}
/**
 *	We are continuing to build our blackjack program from the ground up.  Yesterday,
 *	we designed a Card.  Today we will put those Cards into a Deck.
 *	Answer these questions on paper:
 *
 *	1.  Compile and run the code.  (This does a little more than yesterday).  
 *	    Do the cards always come out in the same order?
 *	2.  Multiple choice.  Which of the following methods has code to handle the 
 *		A, K, Q, and J?
 *			a. stringifyRank
 *			b. createShuffledDeck
 *			c. nextCard
 *			d. cardsLeft
 *	3.  Multiple choice.  Which of the following methods uses a for loop to create cards?
 *			a. stringifyRank
 *			b. createShuffledDeck
 *			c. nextCard
 *			d. cardsLeft
 *	4.  Multiple choice.  Which of the following methods randomly returns the next card?
 *			a. stringifyRank
 *			b. createShuffledDeck
 *			c. nextCard
 *			d. cardsLeft
 *	5.  Do you think this deck of cards can only be used for Blackjack, or can 
 *		they be used for other games (give examples)?  
 *
 */