Code Monkey home page Code Monkey logo

playingcards.jl's Introduction

PlayingCards.jl

Docs Build docs build
Documentation dev
GHA CI gha ci
Code Coverage codecov
Bors enabled bors

A package for representing playing cards for card games (for a standard deck of fifty two).

Cards

A playing Card is consists of a suit (,,,) and a rank:

  • Rank(N::Int) where 1 ≤ N ≤ 13 where
  • N = 1 represents an Ace (which can have high or low values via high_value and low_value)
  • N = 11 represents a Jack
  • N = 12 represents a Queen
  • N = 13 represents a King

The value of the rank can be retrieved from high_value and low_value:

  • high_value(c::Card) == low_value(c::Card) == c.rank
  • high_value(::Card) = 14 for Ace
  • low_value(::Card) = 1 for Ace

Cards have convenience constructors and methods for extracting information about them:

julia> using PlayingCards

julia> card = A♡
A♡

julia> string(card)
"A♡"

julia> suit(A♡)
Heart()

julia> rank(A♠)
Ace()

julia> high_value(A♢)
14

julia> high_value(J♣)
11

julia> low_value(A♡)
1

Decks

A Deck is a struct with a Vector of Cards, which has a few convenience methods:

julia> using PlayingCards

julia> deck = ordered_deck()
A♣ 23456789♣  T♣  J♣  Q♣  K♣
A♠ 23456789♠  T♠  J♠  Q♠  K♠
A♡ 23456789♡  T♡  J♡  Q♡  K♡
A♢ 23456789♢  T♢  J♢  Q♢  K♢


julia> shuffle!(deck)

julia> hand = pop!(deck, 2)
(5♣, 8♢)

julia> deck
Q♣  T♣  5♢  K♠  J♢  4♢  T♡  K♢  25288♠
K♣  T♠  A♣  Q♠  Q♢  276♣  J♡  96♢  A♢  7♠
A♡  7337♢  J♠  54943♠  J♣  696♠  T♢  3♡  A♠  8♡  K♡  24♠  Q♡  9

Related packages

Package Development status Purpose
PlayingCards.jl Perhaps stable Representing cards
PokerHandEvaluator.jl Perhaps stable Comparing any two 5-7 card hands
TexasHoldem.jl Likely changes needed Simulating multi-player games of TexasHoldem
PokerBots.jl very early development Battling bots with prescribed (or learned) strategies
PokerGUI.jl very early development Visualizing TexasHoldem games via a GUI

Acknowledgements

Some ideas used here were inspired by

playingcards.jl's People

Contributors

bors[bot] avatar charleskawczynski avatar moelf avatar sbuercklin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

playingcards.jl's Issues

Would you like riffle and cut?

Hi Charles,

First, thanks for your offer to help with FreeCell, but please see my comments there responding to the two issues you raised. I would like to understand docs and coverage for some of my other packages and would welcome your assistance.

Now to the main point:

I tested my cut and riffle functions on your implementation of PlayingCards and they work fine. Do you want to include this functionality? If so, you can just copy the file
shuffle.jl
to your src directory and add the line include("shuffle.jl") to PlayingCards.jl.

Ed

Add Joker

Joker cards may be good placeholders to enforce type stability.

Define AbstractDeck

Users may want to make custom decks, so we should provide an abstract type for that.

Card values

Hi again. I'd like to see the functions low_value and high_value defined for all cards.

julia> C = 5♠
5♠

julia> value(C)
5

julia> high_value(C)
ERROR: UndefVarError: high_value not defined
Stacktrace:
 [1] top-level scope
   @ REPL[15]:1

julia> low_value(C)
5

For Poker, you'd want to use high_value; for solitaire, I'd use low_value. This makes PlayingCards "agnostic" about ace high vs. ace low.

My further suggestion is not to define value at all, and let the user either choose which value function to use, or else define their own function either value=low_value or value=high_value.

Let me know if you'd like me to edit PlayingCards.jl to implement, or send some code snippets you can cut/paste.

Thanks,
Ed

Behavior of number * suit

Hi Charlie:

Is this the behavior you want?

julia> using PlayingCards

julia> [ k*♣ for k=1:13 ]
13-element Vector{Card{_A, Club} where _A<:Rank}:
 1♣
 2♣
 3♣
 4♣
 5♣
 6♣
 7♣
 8♣
 9♣
 T♣
 11♣
 12♣
 13♣

I've got mine rigged to do this:

julia> using PlayingCards52

julia> [ k*♣ for k=1:13 ]
13-element Vector{Card}:
 A♣
 2♣
 3♣
 4♣
 5♣
 6♣
 7♣
 8♣
 9♣
 T♣
 J♣
 Q♣
 K♣

Define MaskedDeck

We might as well define this here. It would be useful if users could use a deck that has a non-allocating pop! function-- where we internally track which cards have been popped (to avoid allocations).

FYI - Unicode versions

As part of some font testing recently, I worked out how to use the Unicode playing cards glyphs. Here's an alternative show() method that should work, if there are fonts around with the requisite symbols.

function Base.show(io::IO, c::Card)
    r = rank(c)
    # ace of spades   : u1F0A1 :   0
    # ace of hearts   : u1F0B1 : + 16
    # ace of diamonds : u1F0C1 : + 32
    # ace of clubs    : u1F0D1 : + 48
    if 1  r  14
        if PlayingCards.suit(c) == Spade()
            suitoffset = 0
        elseif PlayingCards.suit(c) == Heart()
            suitoffset = 16
        elseif PlayingCards.suit(c) == Diamond()
            suitoffset = 32
        elseif PlayingCards.suit(c) == Club()
            suitoffset = 48
        end
        # 11 is Knight, so move up
        (12 <= r <= 13) && (r += 1)
        print(io, Char(0x1f0a0 + r + suitoffset))
    else
        # joker ? 
        print(io, Char(0x1f0e0))
    end
end

I think the suit selection here could be improved... :)

To be honest, the display in the Terminal of these fonts isn't brilliant, because they can be very small, particularly if it's really monospaced...

Screenshot 2021-06-21 at 13 29 33

If there's some font substitution occurring, the card glyphs won't be monospaced (on my Mac it picks up the glyphs from DejaVu Sans):

Screenshot 2021-06-21 at 13 33 37

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

Card color

Hi Charles,

I don't feel comfortable editing your code because I might change things you don't want changed (and I might introduce bugs). So I'll use this Issues section to make suggestions. First one is that I'd like to see a color function. Here's a quick way to implement that I think is consistent with your package design.

I'd like this feature because in solitaire games one often builds piles with alternating colors.

Thanks,
Ed


Add this:

export RedSuit, BlackSuit, color

"""
`color(C::Card)` returns `BlackSuit` if the card is a 
club or spade, and returns `RedSuit` is it's a heart or 
a diamond.
"""
function color(C::Card)
    return supertype(typeof(suit(C)))
end

Example:

julia> C = 5♠
5♠

julia> color(C)
BlackSuit

Allow `\:diamonds:` etc.

You can enter (say) the five of diamonds by typing 5+\diamondsuit+TAB and you get 5♢. All is well.

However, 5+\:diamonds:+TAB gives 5♦ which is not valid. Likewise for \heartsuit vs \:hearts:.

Interestingly, the black suits are ok: \clubsuit and \:clubs: give the same character (and likewise for spades).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.