I've been trying to make a class to represent a deck of cards. However, I wanted to create it in a way which it could be any kind of cards, it doesn't need to know which kind it just has be able to store them, shuffle and draw them one at a time be them uno cards, regular playing cards, or trading cards. For this, I've been trying something I've heard of but have not used -- Generics.
However, I've had no luck at all trying to get it to work. It won't instantiate, populate cards, or return the correct type when drawing the card. I've tried mixing and matching and I just simply can't get it to work.
Old Code that was buggy was truncated to save space, look at previous edits to see. Summary: I used Cardable instead of T and lacked to express generics in general.
So how would this work, I'm completely new to generics. I've been looking around everywhere and I keep hearing about Type Erasure and that the class literal should be a parameter and yadda yadda... But then how does ArrayList do it? Why is it that you can just type ArrayList<String>()
and it will just work without needing something ridiculous like ArrayList<String>(String.GetClass())
?
Thanks for your time.
Edit: Cardable is a class in which any card that can be put into the deck will extend.
Edit2: Perception's suggestion has thus fixed my code, but I am not sure how I could call to populate the deck. Right now I have it to accept an array, but it would be nice to have it internal, and I'm not entirely sure I grasp the entire factory method.
public class Deck<T extends Cardable>
{
private ArrayList<T> cardsInDeck;
public Deck()
{
cardsInDeck = new ArrayList<T>();
}
public void populate( T[] newCards )
{
cardsInDeck.clear();
for( T card : newCards )
{
cardsInDeck.add( card );
}
shuffle();
}
public T drawCard()
{
T card = null;
try
{
card = cardsInDeck.get( 0 );
}
catch( IndexOutOfBoundsException e )
{
System.out.println( "Ran out of Cards" );
e.printStackTrace();
}
cardsInDeck.remove( 0 );
return card;
}
public void shuffle()
{
ArrayList<T> newDeck = new ArrayList<T>();
Random rand = new Random();
while( !cardsInDeck.isEmpty() )
{
int index = rand.nextInt( cardsInDeck.size() );
newDeck.add( cardsInDeck.get( index ) );
cardsInDeck.remove( index );
}
cardsInDeck = newDeck;
}
public int getSize()
{
return cardsInDeck.size();
}
}