I am making a program to play a game of UNO. In the UNO deck, some cards are repeated, and therefore I cannot just make a list of integers; I have to use objects. I plan on using a LinkedList for the deck, but I am aware that shuffles on a LinkedList are horridly slow.
My question is, should I….
- Avoid a LinkedList entirely and just go with an ArrayList
- Use an ArrayList or similar, shuffle, then put the contents into the LinkedList
- Construct an ArrayList, then make my own shuffling routine (aka not using Random) that adds to the LinkedList as we go
- Shuffle the LinkedList anyway (as in, it’s not really that bad)
This is not for homework; it is to assist in having fun 🙂
You can represent cards by plain integers. If an integer represents a type of card, and Uno has multiple cards of the same type, just use the integer corresponding to that card more than once.
Shuffling and dealing is easy.
To start the game, set up a fixed size, dumb array of type integer (no fancy linked lists or Arraylist need apply) that can hold the entire deck (size = N). Fill this array with the integers representing the Uno deck including the duplicate integers representing duplicate cards. Set UNDEALT to N.
To shuffle, execute the following code some modest (100?) times:
To deal:
You can do all this with fancier data structures, too, but there just isn’t any point. Given that the total information involved is 100 data items, unless you do something outrageously dumb, it’ll be faster than people. But my motto is: if simple works, stick with simple.