I am trying to develop an UNO card game, and now I reach to a point where I need a data structure that can hold the whole deck, and then I can take pieces of it and distribute it among several places (The players, the discard pile, and the draw pile). The code would be something like this:
public class UnoGame
{
public UnoGame()
{
SomeDataStructure<Card> unoDeck = generateUnoCards();
List<Cards> player1 = unoDeck.get(7);
List<Cards> player2 = unoDeck.get(7);
List<Cards> discardPile = unoDeck.get(1);
List<Cards> drawPile = unoDeck.getRest();
}
private static SomeDataStructure<Card> generateUnoCards()
{
SomeDataStructure<Card> cards = new SomeDataStructure<Card>(108);
// Here we can create the proper cards and add them to the data structre
Collections.shuffle(cards);
return cards;
}
}
So, it there any built-in data structure in Java that fits my requirements? Or should I implement my own data structure?
It’s not really clear why you need anything special. I would model it as per the real game: a stack for all the cards (using
LinkedList<T>as the stack implementation), which you then take cards off to get a hand, and which is also used to fetch cards later. I would model each player with a separate class though… even if initially it just contains aList<Card>or something similar.At every point, if you think about what would happen in the physical game, it should be reasonably easy.