ANN: Cards.jl

The topic of special types for cards inspired me, so I created a Cards package:

https://github.com/StefanKarpinski/Cards.jl/blob/master/src/Cards.jl

This code uses 2 bits to represent suits, 6 bits to represent each card (2 suit bits + 4 rank bits), and can represent an arbitrary hand (set of cards) as a 64-bit value (64 = 2^6). It has different card values for low and high aces (each are used in some games), and low and high jokers (each used in some games). This lets you represent hands from essentially any card game really compactly and perform operations on them efficiently. Now someone just needs to write some code that actually deals and plays games :grin:

15 Likes

I tired to get it to work by my Julia 0.6 could not parse struct. Is this a new language feature?

yes

It is the same as immutable on 0.5 (you can still use immutable in 0.6 but it will be deprecated next version).

1 Like

This is reminiscent of the pattern used by the now unavailable “pokerstove”.

This pokerstove?

https://github.com/andrewprock/pokerstove/blob/master/README.md

I just googled the name and this came up. It looks like it’s been released under a 3-clause BSD license.

That looks like it, although I believe this is a command line version. The original had a simple gui whose grid was used by aspiring poker pros to study and compare hand ranges, and that has since been adopted by many other poker training tools. It builds a lookup table for speed. The core evaluator can be found at [pokerstove eval] (https://github.com/andrewprock/pokerstove/tree/master/src/lib/pokerstove/peval).

If I only had the time, a pure Julia version + Escher UI…

I removed my link because it contained GPL code.

Note that this Hand data structure assumes that there is only one of each card, which doesn’t work for games like pinochle that have 2 (or more) of each card (by combining multiple standard decks or by using custom decks). And the Hand constructor doesn’t check for this possibility (it ignores cards that appear more than once).

(Also, pinochle uses a nonstandard card ranking: A 10 K Q J 9, and there are no cards below a 9. So maybe it is too weird to support. :wink: )

Non standard card rankings are pretty common for german card games. I also know at least two games with multiple sets of cards.
This also applies to the suits: In germany, the usual order is Clubs, Spades, Hearts, Diamonds.

A card type could also use a value field for scoring.

In Italy the order is (from lowest to highest): spades, clubs, diamonds, hearts.

I would think that the mapping from cards to values would be a property of a game, i.e a separate dictionary-like object, not a property of individual cards.

I intentionally did not order the suits, although, of course, I had to pick an encoding. The encoding I chose is based on bridge ordering of suits – since I play bridge, and it’s a game where suit ordering is significant. In poker, suit ordering mostly doesn’t matter. It would certainly be possible to have a multiset hand type, but I think that’s beyond the scope of an initial cut of such a package.

1 Like

In Spain, they also have many 2 deck games, and the suits are different, cups, swords, clubs, and coins (red, silver, green, gold) Also, many games are played with 40 card decks, which have only: Ace, 2 - 7, and Knave (Sota), Knight (Caballo), King (Rei) for the face cards.

Also, pinochle uses a nonstandard card ranking: A 10 K Q J 9, and there are no cards below a 9. So maybe it is too weird to support. :wink:

My favorite card game Jass (from Switzerland) has no cards below 6, and the rank order is different, but only for the trump suit. In the trump suit, the order is (high-low) J 9 A K Q 10 8… but in all others it’s A-6.

This thread has turned into such a wonderful metaphor for open source software development.

12 Likes

My favorite card game Jass (from Switzerland)

Hmmm… the names are so similar that I wonder if Jass is related to the Dutch game of Klaverjassen

Jass, first mentioned in Switzerland in 1796,[1] was originally the name of the highest trump, the Jack, in a family of related games originally spread from the Netherlands during the Late Middle Ages.

Well, that implies they are! Funny how neither page mentions either though - you can dive down the rabbit hole of wikipedia cardgame pages and have no clue of their connection:

I hope a history PhD is working on a card games family network graph somewhere to fix this :stuck_out_tongue: