CardDealer is your go-to gem for creating, shuffling, and dealing decks of cards with ease. Whether you're building a poker night app or a virtual bridge club, CardDealer has got you covered. Enjoy customizable deck options, smooth shuffling algorithms, and simple yet powerful deck manipulation tools that bring the classic card game experience to life.
Install the gem and add to the application's Gemfile by executing:
$ bundle add card_dealer
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install card_dealer
To create a standard 52-card deck, use the CardDealer::BuildDeck.standard52
method:
deck = CardDealer::BuildDeck.standard52
puts deck.cards
To create a standard 36-card deck, use the CardDealer::BuildDeck.standard36
method:
deck = CardDealer::BuildDeck.standard36
puts deck.cards
To create a custom deck of cards, use the CardDealer::BuildDeck.custom
method.
You can specify the number of decks, cards per suit, ranks, and suits:
deck = CardDealer::BuildDeck.custom(
decks: 2,
cards_per_suit: 5,
ranks: :highest,
suits: %w[c d]
)
puts deck.cards
The CardDealer::Deck
class provides methods for shuffling and dealing cards:
deck = CardDealer::BuildDeck.standard52
deck.shuffle
hand = deck.deal(5)
puts hand
You can also burn cards before dealing:
deck = CardDealer::BuildDeck.standard52
deck.shuffle
hand = deck.deal(3, burn: 1)
puts hand
To burn cards without dealing, just pass 0
as the number of cards to deal:
deck = CardDealer::BuildDeck.standard52
deck.shuffle
hand = deck.deal(0, burn: 1)
puts hand
Burned cards are stored within the deck and can be accessed via burned_cards
method:
deck = CardDealer::BuildDeck.standard52
deck.shuffle
deck.deal(0, burn: 10)
puts deck.burned_cards
To encode a deck of cards as a binary string, use the CardDealer::BinaryDeck.encode
method.
This is useful if you'd like to store a deck of cards in a database, cache, or a file:
deck = CardDealer::BuildDeck.standard52
encoded_deck = CardDealer::BinaryDeck.encode(deck)
# - or -
encoded_deck = deck.to_binary_s
puts encoded_deck
To decode a binary string into a deck of cards, use the CardDealer::BinaryDeck.decode
method:
encoded_deck = "\x02\xCDP" # binary string
decoded_deck = CardDealer::BinaryDeck.decode(encoded_deck)
# - or -
decoded_deck = CardDealer::Deck.from_binary(encoded_deck)
puts decoded_deck.cards
After checking out the repo, run bin/setup
to install dependencies. Then, run
rake spec
to run the tests. You can also run bin/console
for an interactive
prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To
release a new version, update the version number in version.rb
, and then run
bundle exec rake release
, which will create a git tag for the version, push
git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/svyatov/card_dealer.
The gem is available as open source under the terms of the MIT License.