MyNixOS website logo
Description

Playing Cards Utility Functions.

Early insights in probability theory were largely influenced by questions about gambling and games of chance, as noted by Blitzstein and Hwang (2019, ISBN:978-1138369917). In modern times, playing cards continue to serve as an effective teaching tool for probability, statistics, and even 'R' programming, as demonstrated by Grolemund (2014, ISBN:978-1449359010). The 'mmcards' package offers a collection of utility functions designed to aid in the creation, manipulation, and utilization of playing card decks in multiple formats. These include a standard 52-card deck, as well as alternative decks such as decks defined by custom anonymous functions and custom interleaved decks. Optimized for the development of educational 'shiny' applications, the package is particularly useful for teaching statistics and probability through card-based games. Functions include shuffle_deck(), which creates either a shuffled standard deck or a shuffled custom alternative deck; deal_card(), which takes a deck and returns a list object containing both the dealt card and the updated deck; and i_deck(), which adds image paths to card objects, further enriching the package's utility in the development of interactive 'shiny' application card games.

mmcards

Games of chance serve as essential learning tools for understanding probability and statistics. The ‘mmcards’ package, short for Mighty Metrika cards, offers utility functions for handling playing cards, streamlining the development of educational Shiny applications related to games of chance.

Installation

You can install the released version of ‘mmcards’ from CRAN:

install.packages("mmcards")

To install the development version of ‘mmcards’, you can use the devtools package:

# install.packages("devtools")
devtools::install_github("mightymetrika/mmcards")

Who Has the Highest Card: A Basic Example

This section demonstrates a simple game called “Who Has the Highest Card,” which shows you how to use mmcards to create a shuffled standard deck and deal cards to players.

library(mmcards)

# Define the Players
p1 <- NULL
p2 <- NULL

# Create a shuffled standard deck of 52 cards using a random number seed for reproducibility
game_deck <- shuffle_deck(seed = 147)

# Deal a card to each player
game_deck <- deal_card(game_deck)
p1 <- game_deck$dealt_card

game_deck <- deal_card(game_deck)
p2 <- game_deck$dealt_card

# Display the cards and determine the winner
paste("p1 has:", p1$card, "with a value of", p1$value)
#> [1] "p1 has: 5C with a value of 5"
paste("p2 has:", p2$card, "with a value of", p2$value)
#> [1] "p2 has: KH with a value of 13.5"
paste0("The winner is: ", ifelse(p1$value > p2$value, "p1", "p2"))
#> [1] "The winner is: p2"

Alternative Decks

To accommodate various use-cases, shuffle_deck() allows users to create specialized decks like AnonymousDeck, InterleavedDeck, or PairedDeck. You can learn more about these decks in the “alternative-decks” vignette.

Adding Card Images

Use the i_deck() function to add images to your card decks. You’ll need to have the images stored on your computer.

The following example was used to create a Shiny application which flips through a deck of shuffled cards showing the card image and the card value.

In this example, my working directory was set to “C:/Users/Administrator/Desktop/mmcards” and a deck of PNG vector-playing-cards was stored at “C:/Users/Administrator/Desktop/PNG-cards-1.3/PNG-cards-1.3”.

#install.packages("shiny")
library(shiny)

    # Initialize a shuffled and image-embedded deck
    ic <- i_deck(deck = shuffle_deck(seed = 42),
                 i_path = paste0(gsub("mmcards", "", getwd()),
                                 "PNG-cards-1.3/PNG-cards-1.3"))

    # UI
    ui <- fluidPage(
      titlePanel("Card Deck Shiny App"),

      fluidRow(
        column(6,
               # Image output
               imageOutput("cardImage", height = "auto")
        ),
        column(6,
               # Text output
               textOutput("cardValue"),

               # Deal card button
               actionButton("deal_card", "Deal Card")
        )
      )
    )

    # Server logic
    server <- function(input, output, session) {

      # Initialize reactive variable to store current deck
      current_deck <- reactiveVal(ic)

      # Output for card image
      output$cardImage <- renderImage({
        # Initialize or get the current deck
        deck <- current_deck()

        # Return image path
        list(src = deck$icard[1],
             width = 250,
             height = 350)
      }, deleteFile = FALSE)

      # Output for card value
      output$cardValue <- renderText({
        # Initialize or get the current deck
        deck <- current_deck()

        # Return card value
        return(deck$value[1])
      })

      # Deal card logic
      observeEvent(input$deal_card, {
        # Get the current deck
        deck <- current_deck()

        # Deal a card
        new_deck <- deal_card(deck)

        # Update the current deck
        current_deck(new_deck$updated_deck)
      })
    }

    # Run the app
    shinyApp(ui, server)
    
Metadata

Version

0.1.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows