import java.util.Random;

/**
 * A basic class to handle dealing and shuffling cards from one or more decks of cards
 * 
 * @author dave
 */
public class CardDealer{
	private static final int NUM_CARDS = 52;  // the number of cards in the deck
	
	private int[] cards;  // all of the cards to be dealt
	private int position;  // our current position in our stack of cards

	public static void main(String[] args){
		CardDealer dealer = new CardDealer(1);
		
		for( int i = 0; i < 100; i++ ){
			System.out.println(dealer.next());
		}
	}
	
	/**
	 * Creates a new card dealer with numDecks decks in the deck
	 * 
	 * @param numDecks the number of card decks to use
	 */
	public CardDealer(int numDecks){
		cards = new int[numDecks*NUM_CARDS];
		
		for( int i = 0; i < numDecks; i++ ){
			for( int j = 0; j < NUM_CARDS; j++ ){
				cards[i*NUM_CARDS + j] = j;
			}
		}
		
		shuffle();
	}
	
	/** 
	 * Returns true if there are cards left in the deck, false otherwise
	 */
	public boolean hasNext(){
		return position < cards.length;
	}
	
	/**
	 * Returns the next card if there are cards left in the deck
	 * 
	 * @pre assumes there are cards left in the deck (i.e. hasNext() is true)
	 */
	public int next(){
		position++;
		return cards[position-1];
	}
	
	/** 
	 * Randomly shuffles the deck and resets the card to be dealt as the first card
	 */
	public void shuffle(){
		Random rand = new Random();
		
		for( int i = 0; i < cards.length; i++ ){
			int nextPosition = rand.nextInt(cards.length);
			
			// swap this card with the card at next position
			int temp = cards[i];
			cards[i] = cards[nextPosition];
			cards[nextPosition] = temp;
		}
		
		position = 0;
	}
}