# ANN: Cards.jl

**URL:** https://discourse.julialang.org/t/ann-cards-jl/2523
**Category:** General Usage
**Created:** [March 7, 2017, 8:03am UTC](https://discourse.julialang.org/t/ann-cards-jl/2523 "2017-03-07T08:03:34Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [March 7, 2017, 8:03am UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/1 "2017-03-07T08:03:34Z")

</div>

The topic of [special types for cards](https://discourse.julialang.org/t/fast-string-processing-in-julia-vs-python/2443/25) inspired me, so I created a [Cards package](https://github.com/StefanKarpinski/Cards.jl):

[https://github.com/StefanKarpinski/Cards.jl/blob/master/src/Cards.jl](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 😁

---

<div class="post-metadata">

### Author: ![mbeltagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbeltagy/32/130_2.png) [@mbeltagy](https://discourse.julialang.org/u/mbeltagy)
#### Post date: [March 7, 2017, 8:20am UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/2 "2017-03-07T08:20:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [March 7, 2017, 8:22am UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/3 "2017-03-07T08:22:52Z")

</div>

yes

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 7, 2017, 8:24am UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/4 "2017-03-07T08:24:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![merl-dev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/merl-dev/32/96_2.png) [@merl-dev](https://discourse.julialang.org/u/merl-dev)
#### Post date: [March 7, 2017, 11:15am UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/5 "2017-03-07T11:15:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [March 7, 2017, 2:41pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/6 "2017-03-07T14:41:29Z")

</div>

This [pokerstove](https://github.com/andrewprock/pokerstove)?

[https://github.com/andrewprock/pokerstove/blob/master/README.md](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.

---

<div class="post-metadata">

### Author: ![merl-dev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/merl-dev/32/96_2.png) [@merl-dev](https://discourse.julialang.org/u/merl-dev)
#### Post date: [March 7, 2017, 2:56pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/7 "2017-03-07T14:56:15Z")

</div>

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](https://github.com/andrewprock/pokerstove/tree/master/src/lib/pokerstove/peval)).

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

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 7, 2017, 3:08pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/9 "2017-03-07T15:08:40Z")

</div>

I removed my link because it contained GPL code.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 7, 2017, 5:07pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/10 "2017-03-07T17:07:11Z")

</div>

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. 😉 )

---

<div class="post-metadata">

### Author: ![shorty66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shorty66/32/3254_2.png) [@shorty66](https://discourse.julialang.org/u/shorty66)
#### Post date: [March 7, 2017, 5:18pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/11 "2017-03-07T17:18:29Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [March 7, 2017, 5:50pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/12 "2017-03-07T17:50:05Z")

</div>

> [@shorty66](#):
>
> In germany, the usual order is Clubs, Spades, Hearts, Diamonds.

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

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 7, 2017, 6:17pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/13 "2017-03-07T18:17:24Z")

</div>

> [@shorty66](#):
>
> A card type could also use a value field for scoring.

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.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [March 7, 2017, 8:50pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/14 "2017-03-07T20:50:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [March 7, 2017, 10:21pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/15 "2017-03-07T22:21:55Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [March 20, 2017, 2:07pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/16 "2017-03-20T14:07:05Z")

</div>

> 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. 😉

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.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [March 20, 2017, 2:16pm UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/17 "2017-03-20T14:16:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Job\_van\_der\_Zwan](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Job\_van\_der\_Zwan](https://discourse.julialang.org/u/Job_van_der_Zwan)
#### Post date: [March 24, 2017, 10:09am UTC](https://discourse.julialang.org/t/ann-cards-jl/2523/18 "2017-03-24T10:09:51Z")

</div>

> 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.

> **[Jass](https://en.wikipedia.org/wiki/Jass)**
>
> Jass (.mw-parser-output .IPA-label-small{font-size:85%}.mw-parser-output .references .IPA-label-small,.mw-parser-output .infobox .IPA-label-small,.mw-parser-output .navbox .IPA-label-small{font-size:100%}German pronunciation: \[ˈjas\]) is a family of trick taking, Ace-Ten card games and, in its key forms, a distinctive branch of the Marriage family. It is popular in its native Switzerland as well as the rest of the Alemannic German-speaking area of Europe, Italian South Tyrol and in a few place Th...

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:

> **[Klaverjassen](https://nl.wikipedia.org/wiki/Klaverjassen)**
>
> Klaverjassen is een kaartspel dat gespeeld wordt door vier personen in twee paren. De twee tegenover elkaar zittende personen vormen een team. Het gaat dus om twee tegen twee. In Frankrijk en Noord-Amerika zijn vergelijkbare spellen bekend onder respectievelijk de namen Belote en Tarbish. In Zwitserland is het afgeleide spel "Jass" zeer populair. De Nederlandse naam is afgeleid van het oude begrip jas, dat staat voor de boer als hoogste troef (denk aan de Franse term 'jacques' voor 'boer'). De na...

> **[Klaberjass](https://en.wikipedia.org/wiki/Klaberjass)**
>
> Klaberjass (.mw-parser-output .IPA-label-small{font-size:85%}.mw-parser-output .references .IPA-label-small,.mw-parser-output .infobox .IPA-label-small,.mw-parser-output .navbox .IPA-label-small{font-size:100%}German: \[ˈklaːbɐˌjas\]) or Bela is a trick-taking Ace-Ten card game that is most popular in German communities. In its basic form it is a 9-card trick-and-draw game for two players using a 32-card piquet pack.: 200f 
> As in other point-trick games of the King–Queen group, players can score p...

> **[Belote](https://en.wikipedia.org/wiki/Belote)**
>
> Belote (.mw-parser-output .IPA-label-small{font-size:85%}.mw-parser-output .references .IPA-label-small,.mw-parser-output .infobox .IPA-label-small,.mw-parser-output .navbox .IPA-label-small{font-size:100%}French pronunciation: \[bəlɔt\]) is a 32-card, trick-taking, Ace-Ten game played primarily in France and certain European countries, namely Armenia, Bulgaria, Croatia, Cyprus, Georgia (mainly Guria), Greece, Luxembourg, Moldova, North Macedonia (mainly Bitola), Bosnia and Herzegovina and also in ...

> **[Pinochle](https://en.wikipedia.org/wiki/Pinochle)**
>
> Pinochle (English: /ˈpiːnʌkəl/), also called pinocle or penuchle, is a trick-taking, Ace-Ten card game typically for two to four players and played with a 48-card deck. It is derived from the card game bezique; players score points by trick-taking and also by forming combinations of characters into melds. It is thus considered part of a "trick-and-meld" category which also includes the game belote. Each hand is played in three phases: bidding, melds, and tricks. The standard game today is cal

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