Cards.jl

A package for representing hands of cards (quite compactly)
Author StefanKarpinski
Popularity
29 Stars
Updated Last
1 Year Ago
Started In
March 2017

Cards

This package defines three types:

• `Suit` uses 2 low bits of a `UInt8` to represent four suits of cards: , ♢, ♡, .

• `Card` uses 6 low bits of a `UInt8` to represent 64 possible card values:

• 2 bits for the `Suit` (, ♢, ♡, )
• 4 bits for the rank from 0-15, meaning:
• 0 – low joker
• 1 – low ace
• 2-10 – number cards
• 11-13 – jack, queen, king
• 14 – high ace
• 15 – high joker
• `Hand` uses 64 bits of a `UInt64` to represent all possible hands (sets) of cards.

The design of having high and low aces and jokers allows hands from many different games to be represented in a single scheme, with consistent rank ordering. If you're representing hands from a game with aces high, use the `A♣`, `A♢`, `A♡`, `A♠` cards; if you're representing hands from a game with aces low, use the `1♣`, `1♢`, `1♡`, `1♠` cards instead.

Example usage:

```julia> using Cards

julia> hand = rand(Hand)
Hand([2♣, 3♣, 6♣, 7♣, 8♣, 9♣, 2♢, 3♢, 4♢, 7♢, 10♢, J♢, A♢, 4♡, 5♡, 6♡, 7♡, Q♡, K♡, A♡, 4♠, 6♠, 9♠, K♠, A♠])

julia> 2♣ in hand
true

julia> 4♣ in hand
false

julia> A♣ in hand
false

julia> A♠ in hand
true

julia> ♡ ∩ hand
Hand([4♡, 5♡, 6♡, 7♡, Q♡, K♡, A♡])

julia> ♠ ∩ hand
Hand([4♠, 6♠, 9♠, K♠, A♠])

julia> length(♣ ∩ hand)
6```