/**
 * @author Yoshihiro Fujio
 * CS239 section 2
 * PA2
 * V1 date 2007/03/11
**/
/**********************************************************************
 * Deck.java
 *
 * Represents a Deck of cards.
 ********************************************************************/

public class Deck
{
	private Card[] cards;
	private int numCards = 52;	// numbers of cards left in deck
	
	 /***************************************************************
    * Creates a Deck of cards that contains 52 cards. 
    ***************************************************************/
   public Deck()
   {
		Card allCard;
		int ii = 0;
		
		cards = new Card[52];
		
		for (Suit suit: Suit.values())
		{
			for (Rank rank: Rank.values())
			{
				allCard = new Card(rank, suit);
				cards[ii] = allCard;
				ii++;
			}
		}
   } // end Deck
	
	 /***************************************************************
    * shuffles a deck 
    ***************************************************************/
   public void shuffle()
   {
		int firstArray;
		int secondArray;
		Card Array;
		
		for ( int ii = 0; ii < 50; ii++) // shuffle 50 times
		{
			firstArray = (int)(Math.random() * 52); // decide first array
			secondArray = (int)(Math.random() * 52); // decide second array
			
			Array = cards[firstArray]; // store value of first array to Array
			cards[firstArray] = cards[secondArray]; // store value of second array to first array
			cards[secondArray] = Array; // store value of array to second array
		}
	} // end shuffle
		
	 /***************************************************************
    * deal cards to players 
    ***************************************************************/
   public Card deal()
   {
	
		Card top; // the card on the top of deck
		
		top = cards[numCards - 1]; // set top equals to the array of numCards
		numCards--; // minus the numCards, so it does not draw same one
		
		return top;		
   } // end deal
	
   /*********************************************************
    * The string of cards
    * @return The String of list of cards
    ********************************************************/
   public String toString ()
   {
		String doCards = "";
		
		for (int ii = 0; ii < 52; ii++)
		{
			doCards += cards[ii] + "\n";
		}	
		
		return doCards;
   } // end toString					
} // end class
	