Class Cards
java.lang.Object
com.github.yellowstonegames.core.Cards
- All Implemented Interfaces:
ISerializersNeeded
A simple class to simulate a deck of cards that can be shuffled or drawn from (without replacement). If the deck is
empty and the user tries to draw a card, the deck is shuffled entirely and then a card is drawn. You can draw a card
simulated by an int index with
An array of String names must be provided (currently) for each card's name, such as "Ace of Spades" or "The Chariot" (for a game like poker, and for tarot, respectively). You can get a name from an int retrieved earlier with
drawInt(), or get a String name for a card drawn with drawName().
An array of String names must be provided (currently) for each card's name, such as "Ace of Spades" or "The Chariot" (for a game like poker, and for tarot, respectively). You can get a name from an int retrieved earlier with
nameForInt(int), or just retrieve the name directly with drawName().-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumPredefined common types of card decks, stored by the names of cards (permitting duplicates). -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCopy constructor; makes a copy of each field (no direct references).Cards(Cards.DeckType type) Creates a new Cards using the specifiedCards.DeckTypeand an unseededXoshiro256MX3Randomgenerator, which is guaranteed to be able to produce all possible shuffles of an up-to-57-card deck.Cards(Cards.DeckType type, com.github.tommyettinger.random.EnhancedRandom random) Creates a new Cards using the specifiedCards.DeckTypeand the givenEnhancedRandomgenerator.Creates a new Cards using the given array of names (which will be used directly, permitting the name of a card to potentially be changed later if the array is modified) and anEnhancedRandomgenerator. -
Method Summary
Modifier and TypeMethodDescriptionintdrawInt()If the deck has any remaining cards, this removes the last card from the deck and returns its index.drawName()If the deck has any remaining cards, this removes the last card from the deck and returns its name.final booleanGets a List of Class instances that must each be registered with a serialization library before this object can be successfully serialized or deserialized.inthashCode()nameForInt(int index) intGets how many cards are left in the deck, before drawing a card would need to shuffle.voidshuffleDeck(boolean full) Iffullis true, shuffles the entire deck back in to restart it from its original size in a new order.toString()
-
Field Details
-
deck
public com.github.tommyettinger.ds.IntDeque deck -
random
public com.github.tommyettinger.random.EnhancedRandom random -
names
-
-
Constructor Details
-
Cards
Creates a new Cards using the given array of names (which will be used directly, permitting the name of a card to potentially be changed later if the array is modified) and anEnhancedRandomgenerator.
If theEnhancedRandom.getMinimumPeriod()of the generator is at least equal to theMathTools.factorial(double)ofnames.length, then the generator will (in theory) be able to produce all possible shuffles of that deck. A generator such asOrbitalRandomwith a period of 2 to the 71 or greater can produce all shuffles for the 22 tarot major arcana. Larger decks might need as much as a period of 2 to the 256 for a typical playing card deck (with or without jokers). CCG-type decks with 60 or more cards likely can't have all shuffles produced by a single generator currently, though the amount of possible shuffles for even the 22 tarot major arcana cards is astronomically large already.- Parameters:
names- a String array of names for cards, which do not need to be unique; used directly (not copied)random- an EnhancedRandom generator that will be used directly (not copied)
-
Cards
Creates a new Cards using the specifiedCards.DeckTypeand the givenEnhancedRandomgenerator.
If theEnhancedRandom.getMinimumPeriod()of the generator is at least equal to theMathTools.factorial(double)ofCards.DeckType.size(), then the generator will (in theory) be able to produce all possible shuffles of that deck type. A generator such asOrbitalRandomwith a period of 2 to the 71 or greater can produce all shuffles forCards.DeckType.TAROT_MAJOR_ARCANA. Larger decks might need as much as a period of 2 to the 256 for a typical playing card deck (with or without jokers). CCG-type decks with 60 or more cards likely can't have all shuffles produced by a single generator currently, though the amount of possible shuffles for even the 22 tarot major arcana cards is astronomically large already.- Parameters:
type- a predefinedCards.DeckTypeenum constant
-
Cards
Creates a new Cards using the specifiedCards.DeckTypeand an unseededXoshiro256MX3Randomgenerator, which is guaranteed to be able to produce all possible shuffles of an up-to-57-card deck.- Parameters:
type- a predefinedCards.DeckTypeenum constant
-
Cards
Copy constructor; makes a copy of each field (no direct references).- Parameters:
other- another Cards instance to copy exactly, including the remaining deck and its order
-
-
Method Details
-
shuffleDeck
public void shuffleDeck(boolean full) Iffullis true, shuffles the entire deck back in to restart it from its original size in a new order. Otherwise, this only shuffles the remaining cards in the deck into a new order.- Parameters:
full- if true, the deck will start again at its original size; if false, only the remaining cards will be shuffled
-
drawInt
public int drawInt()If the deck has any remaining cards, this removes the last card from the deck and returns its index. Otherwise, this shuffles the entire deck back up to its original size, and then removes the last card and returns its index.- Returns:
- an index of a card in the deck
-
drawName
If the deck has any remaining cards, this removes the last card from the deck and returns its name. Otherwise, this shuffles the entire deck back up to its original size, and then removes the last card and returns its name.- Returns:
- a String name of a card in the deck
-
nameForInt
-
remainingCards
public int remainingCards()Gets how many cards are left in the deck, before drawing a card would need to shuffle.- Returns:
- how many cards are left in the deck
-
equals
-
hashCode
-
toString
-
getSerializersNeeded
Description copied from interface:ISerializersNeededGets a List of Class instances that must each be registered with a serialization library before this object can be successfully serialized or deserialized. This isGwtIncompatible; none of the serialization libraries this is meant for have any support for GWT.- Specified by:
getSerializersNeededin interfaceISerializersNeeded- Returns:
- a List of Class instances that must each be registered with a serialization library
-