FiftyTwo Build Status Gem Version

A standard 52-card deck, written in Ruby.

Installation

Add this line to your application's Gemfile:

# update with the version of your choice
gem 'fiftytwo'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install fiftytwo

Usage

Get a deck of cards:

require "fiftytwo"

deck = FiftyTwo::Deck.standard
# => #<FiftyTwo::Deck:0x00007f905c154b10 @cards=[...]>

deck.count
# => 52

Examine how neatly organized the cards are, and then do something about it:

deck[0..7].map(&:to_s)
# => ["2 of Clubs", "2 of Diamonds", "2 of Hearts", "2 of Spades", "3 of Clubs", "3 of Diamonds", "3 of Hearts", "3 of Spades"]

deck.shuffle! # you can always deck.sort! later
deck[0..7].map(&:to_s)
# => ["5 of Diamonds", "Jack of Spades", "Ace of Hearts", "6 of Hearts", "9 of Hearts", "2 of Diamonds", "3 of Spades", "7 of Spades"]

Take a deep dive into a card:

card = deck.first
# => #<FiftyTwo::Card:0x00007f905c154548 ...>

card.rank
#  => #<FiftyTwo::Rank:0x00007f9056a04068 @value=5, @name="5", @category=:pip>

card.suit
# => #<FiftyTwo::Suit:0x00007f9056a1f138 @name="diamonds", @color=#<FiftyTwo::Suit::Color:0x00007f9056a1f638 @name="red", @rgb="ff0000">, @symbol="♦">

card.red?
# => true

card.spades?
# => false

card.pip?
# => true

card.king?
# => false

Look for types of cards in your deck, as they are currently ordered:

deck.kings.count
# => 4

deck.faces.reds.count
# => 6

deck.faces.reds.first.name
# => "Queen of Hearts"

deck.locate("AS").name
# => "Ace of Spades"

deck.locate("10C").name
# => "Ten of Clubs"

Draw a card from the deck, and give it back later:

deck.shuffle!
card = deck.draw
# => #<FiftyTwo::Card:0x00007fb3d2284960 ...>

deck.count
# => 51
card.name
# => "9 of Diamonds"

deck << card
deck.count
# => 52
deck.last.name
# => "9 of Diamonds"

Deal some cards from the deck to yourself and a friend:

my_hand = FiftyTwo::Hand.new
# => #<FiftyTwo::Hand:0x00007fb3d50ca7c0 @cards=[]>
your_hand = FiftyTwo::Hand.new
# => #<FiftyTwo::Hand:0x00007fb3d21c68c0 @cards=[]>

deck.deal([my_hand, your_hand], hand_size: 5)
"Deck has #{deck.count} cards, I have #{my_hand.count} cards, you have #{your_hand.count} cards"
# => "Deck has 42 cards, I have 5 cards, you have 5 cards"

puts my_hand.render, your_hand.render # by the way renderings are colored red/black in your terminal, just like the suit!
 3

Hands, just like the deck, can be shuffled, sorted, searched, etc:

your_hand.sort!
puts your_hand.render
 5

Pass your cards around:

my_hand.transfer("4S", your_hand)
puts my_hand.render, your_hand.render
 3

Problems?

Please submit an issue. We'll figure out how to get you up and running with FiftyTwo as smoothly as possible.