Title: | Playing Cards Utility Functions |
---|---|
Description: | 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. |
Authors: | Mackson Ncube [aut, cre], mightymetrika, LLC [cph, fnd] |
Maintainer: | Mackson Ncube <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1.9000 |
Built: | 2025-02-17 04:45:12 UTC |
Source: | https://github.com/mightymetrika/mmcards |
This function deals the top card from a given deck and returns the dealt card along with the updated deck.
deal_card(current_deck)
deal_card(current_deck)
current_deck |
A data frame representing the current deck of cards. This can either be a standard deck, an anonymous deck, or an interleaved deck. The function also accepts an object of class "UpDeck" which contains an updated deck and the last dealt card. |
A list containing two elements: dealt_card
, a data frame representing
the card that was dealt, and updated_deck
, a data frame representing the
remaining cards in the deck. The list has the class attribute "UpDeck".
# Using a standard deck std_deck <- standard_deck() result <- deal_card(std_deck) result$dealt_card result$updated_deck # Using an "UpDeck" object result2 <- deal_card(result) result2$dealt_card result2$updated_deck
# Using a standard deck std_deck <- standard_deck() result <- deal_card(std_deck) result$dealt_card result$updated_deck # Using an "UpDeck" object result2 <- deal_card(result) result2$dealt_card result2$updated_deck
This function takes a deck of cards and adds image paths to each card. It produces a new deck that includes the original information along with the image paths. The function is designed to work with various types of decks and allows for customization of image paths.
i_deck( deck, i_path, cards = c("2C", "2D", "2H", "2S", "3C", "3D", "3H", "3S", "4C", "4D", "4H", "4S", "5C", "5D", "5H", "5S", "6C", "6D", "6H", "6S", "7C", "7D", "7H", "7S", "8C", "8D", "8H", "8S", "9C", "9D", "9H", "9S", "10C", "10D", "10H", "10S", "JC", "JD", "JH", "JS", "QC", "QD", "QH", "QS", "KC", "KD", "KH", "KS", "AC", "AD", "AH", "AS"), i_names = vector_playing_cards, i_type = "png" )
i_deck( deck, i_path, cards = c("2C", "2D", "2H", "2S", "3C", "3D", "3H", "3S", "4C", "4D", "4H", "4S", "5C", "5D", "5H", "5S", "6C", "6D", "6H", "6S", "7C", "7D", "7H", "7S", "8C", "8D", "8H", "8S", "9C", "9D", "9H", "9S", "10C", "10D", "10H", "10S", "JC", "JD", "JH", "JS", "QC", "QD", "QH", "QS", "KC", "KD", "KH", "KS", "AC", "AD", "AH", "AS"), i_names = vector_playing_cards, i_type = "png" )
deck |
A data frame representing a deck of cards. Default is standard_deck(), but the function is also optimized to work with shuffled_deck(). |
i_path |
The file path to the folder containing the card images. |
cards |
A vector of card names corresponding to the images. Default includes all cards based on a deck created with standard_deck() |
i_names |
A vector of image file names corresponding to the cards.
Should be in the same order as |
i_type |
The file extension of the image files (e.g., "png", "jpg"). |
A data frame that includes the original deck along with image paths for each card. Inherits the class "ImgDeck" in addition to the original deck's classes.
#The following example shows how to add image paths to the 'mmcards' default #StandardDeck when the images referenced in ?vector_playing_cards are stored #in the working directory. image_deck <- i_deck(deck = standard_deck(), i_path = getwd()) head(image_deck) #See the README for an example of how to use i_deck to incorporate images #into a 'shiny' application.
#The following example shows how to add image paths to the 'mmcards' default #StandardDeck when the images referenced in ?vector_playing_cards are stored #in the working directory. image_deck <- i_deck(deck = standard_deck(), i_path = getwd()) head(image_deck) #See the README for an example of how to use i_deck to incorporate images #into a 'shiny' application.
This function shuffles a deck of cards and returns the shuffled deck. The function can handle standard decks, anonymous decks, and interleaved decks. For interleaved decks, an option to pair shuffle is also available.
shuffle_deck( deck_of_cards = function(x) { standard_deck() }, seed = NULL, paired = FALSE )
shuffle_deck( deck_of_cards = function(x) { standard_deck() }, seed = NULL, paired = FALSE )
deck_of_cards |
An anonymous function that returns a deck of cards as
either a data frame or a list of two numeric vectors for interleaved decks.
Default is |
seed |
An optional seed for reproducibility. Default is NULL. |
paired |
Logical flag to indicate if the interleaved deck should be pair shuffled. Default is FALSE. |
A data frame representing the shuffled deck of cards. The data frame
inherits various classes based on its type. All shuffled decks will have the
classes "ShuffledDeck" and "data.frame". Additional class inheritance depends
on the deck_of_cards
parameter:
"StandardDeck" if deck_of_cards
returns a standard deck (default)
"AnonymousDeck" if deck_of_cards
returns a single vector
"InterleavedDeck" if deck_of_cards
returns a list of two vectors.
If the paired
parameter is set to TRUE, an interleaved deck will also
inherit the class "PairedDeck".
# Standard deck std_deck <- shuffle_deck() head(std_deck) # Anonymous deck anon_deck <- shuffle_deck(deck_of_cards = function(x){runif(52, 1, 10)}) head(anon_deck) # Interleaved deck interleaved_deck <- shuffle_deck( deck_of_cards = function(x){list(runif(26, 1, 5), runif(26, 6, 10))}) head(interleaved_deck) # Paired interleaved deck paired_deck <- shuffle_deck( deck_of_cards = function(x){list(runif(26, 1, 5), runif(26, 6, 10))}, paired = TRUE) head(paired_deck)
# Standard deck std_deck <- shuffle_deck() head(std_deck) # Anonymous deck anon_deck <- shuffle_deck(deck_of_cards = function(x){runif(52, 1, 10)}) head(anon_deck) # Interleaved deck interleaved_deck <- shuffle_deck( deck_of_cards = function(x){list(runif(26, 1, 5), runif(26, 6, 10))}) head(interleaved_deck) # Paired interleaved deck paired_deck <- shuffle_deck( deck_of_cards = function(x){list(runif(26, 1, 5), runif(26, 6, 10))}, paired = TRUE) head(paired_deck)
This function creates a standard deck of playing cards represented as a data frame. The deck includes suits, ranks, and values for each card.
standard_deck( suits = c("C", "D", "H", "S"), ranks = c("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"), values = seq(2, 14.75, by = 0.25) )
standard_deck( suits = c("C", "D", "H", "S"), ranks = c("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"), values = seq(2, 14.75, by = 0.25) )
suits |
A character vector specifying the suits in the deck. Default is c('C', 'D', 'H', 'S') for Clubs, Diamonds, Hearts, and Spades. |
ranks |
A character vector specifying the ranks in the deck. Default is c('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A') for ranks 2 to Ace. |
values |
A numeric vector specifying the values assigned to each card in the deck. Default is a sequence from 2 to 14.75 incremented by 0.25. |
A data frame representing the deck of cards. The data frame has four
columns: rank
, suit
, card
, and value
. The data frame also has class
attributes "StandardDeck" and "data.frame".
deck <- standard_deck() head(deck) tail(deck)
deck <- standard_deck() head(deck) tail(deck)
Image names for vector playing cards
vector_playing_cards
vector_playing_cards
vector_playing_cards
A vector of length 52
Names of cards for a 52 card deck
https://code.google.com/archive/p/vector-playing-cards/