/*
	For: Poker
	
	Created by Travis Roe
*/
import java.util.*;

public class Deck{

	private ArrayList<Card> cards;
	private Random rand;
	
	public int numSwapsPerShuffle = 100;
	
	public Deck(){
		//initialize the ArrayList holding the cards in the deck
		cards = new ArrayList<Card>();
		//shuffling requires some randomness, so an instance of Random is created
		rand = new Random();
		//initialize the cards and store them in the deck
		initCards();
		//shuffles the deck at the start
		shuffle();
	}
	
	/*
		Fills the ArrayList cards with every value a card can be
	*/
	private void initCards(){
		//if there are any cards remaining in the old deck, clear them out to be replaced
		if(cards.size() > 0) cards.clear();
		//for each suit that a card can be
		for(char suit : Card.SUITS.toCharArray()){
			//for each value that a card can be
			for(char value : Card.VALUES.toCharArray()){
				//add that card to the array
				cards.add(new Card(value, suit));
			}
		}
	}
	
	/*
		When this method is finished, the ArrayList cards is holding a randomly shuffled
		array of cards.
	*/
	private void shuffle(){
		/*
			Repeatedly choose two indexes in the array and swap their contents.
			Note: these two indexes can be the same, effectively
					not swapping the values.
		*/
		for(int x = 0; x < numSwapsPerShuffle; x++){
			int indexA = rand.nextInt(cards.size());
			int indexB = rand.nextInt(cards.size());
			Collections.swap(cards, indexA, indexB);
		}
	}	
	/*
		Returns a card at the top of the deck
	*/
	public Card deal(){
		/*
			Internally, an ArrayList is an array.
			As such, instead of having to shift all the indexes in the array over by
			one by removing index 0, we're removing the last index in the array, so
			no shifting has to occur internally
		*/
		return cards.remove(cards.size() - 1);
	}
	
	/*
		Returns
			true	if there are cards left in the deck
			false	if all the cards have been dealt
	*/
	public boolean hasCardsLeft(){
		return (cards.size() > 0);
	}
	
	public String toString(){
		return "Deck: " + cards.toString();
	}
}