Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best data structure to store playing cards held in a player's hand?

I'm beginner in java and I'm currently creating a card game like gin rummy for Android. I want to know what's the best implementation for creating Hand class? What's the best way where to store the card returned by Deck.dealt()?

  1. Array
  2. ArrayList
  3. Vector
  4. HashSet
  5. LinkedList

Also, I would appreciate if anyone could provide a gin rummy open source links.

like image 884
Zack Avatar asked Feb 03 '12 12:02

Zack


People also ask

How do you organize playing cards?

Within each set of four cards of the same value, they should arrange the cards in the order of Clubs, Hearts, Spades, and Diamonds.


1 Answers

If you really want to understand the nuances between the collection types, here goes.

List isn't technically appropriate except when the game is Bohnanza (which, ahem, is one of the greatest card games of all time, but I'mma let me finish).

List says, among other things, that one hand containing the Ace and King of Clubs, and another hand containing the King and Ace of Clubs, are fundamentally not the same hand. This is a much stronger dependence on order than simply "well, I want to remember the order that the user wants to see their cards in", which is a property that tons of non-List collections have, like LinkedHashSet and Guava's ImmutableSet.

List also implies that there is some particular significance attached to the card that sits in index N. This is true of no card game I know.

Set isn't generally appropriate for card games -- only the ones that use a single deck of completely unique cards.

To allow duplicates but still have order-independent equality, the type to use is Guava's Multiset. For example HashMultiset or ImmutableMultiset. Note that most multiset implementations represent multiple "equal" cards by storing just the card and a count, so when iterating over them, duplicates of a card you have in your hand must always appear together. If it's important to let the user freely control the order of cards in her hand, you would need LinkedListMultiset.

Now that lesson time is over.... well, let's be honest. Calling myHand.equals(yourHand), or using an entire hand as a key in a Map is not actually something you're ever going to do... so go ahead and use the ArrayList, you'll be fine. :-)

like image 123
Kevin Bourrillion Avatar answered Oct 06 '22 00:10

Kevin Bourrillion