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.
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")
::install_github("mightymetrika/mmcards") devtools
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
<- NULL
p1 <- NULL
p2
# Create a shuffled standard deck of 52 cards using a random number seed for reproducibility
<- shuffle_deck(seed = 147)
game_deck
# Deal a card to each player
<- deal_card(game_deck)
game_deck <- game_deck$dealt_card
p1
<- deal_card(game_deck)
game_deck <- game_deck$dealt_card
p2
# 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"
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.
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
<- i_deck(deck = shuffle_deck(seed = 42),
ic i_path = paste0(gsub("mmcards", "", getwd()),
"PNG-cards-1.3/PNG-cards-1.3"))
# UI
<- fluidPage(
ui 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
<- function(input, output, session) {
server
# Initialize reactive variable to store current deck
<- reactiveVal(ic)
current_deck
# Output for card image
$cardImage <- renderImage({
output# Initialize or get the current deck
<- current_deck()
deck
# Return image path
list(src = deck$icard[1],
width = 250,
height = 350)
deleteFile = FALSE)
},
# Output for card value
$cardValue <- renderText({
output# Initialize or get the current deck
<- current_deck()
deck
# Return card value
return(deck$value[1])
})
# Deal card logic
observeEvent(input$deal_card, {
# Get the current deck
<- current_deck()
deck
# Deal a card
<- deal_card(deck)
new_deck
# Update the current deck
current_deck(new_deck$updated_deck)
})
}
# Run the app
shinyApp(ui, server)