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()?
Also, I would appreciate if anyone could provide a gin rummy open source links.
Within each set of four cards of the same value, they should arrange the cards in the order of Clubs, Hearts, Spades, and Diamonds.
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. :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With