install.packages("devtools")
devtools::install_github("MonashARP/game-package-bilbies")Blackjack simulator: A gamified R package
![]()
Blackjack is a game of probability. Every decision has a mathematically optimal answer depending on your hand and the dealer’s visible card. So when our team was tasked with building a gamified R package, Blackjack felt like a natural fit. It’s simple enough to explain in one sentence, but deep enough to keep a statistician busy for hours.
The result is blackjackr: an interactive R package that lets you play Blackjack, simulate thousands of rounds, and test strategies, all from your R console.
My contributions
This was a team project. My specific contributions across both parts of the project were:
- C++ programming for the
deal_hand()function for performance - Building core functions and unit tests
- Writing the introduction vignette and README
- Debugging errors during R CMD checks
- Refining roxygen documentation comments
Installing the package
Playing a round
Once installed, you can play a full interactive round of Blackjack straight from the console. The game prompts you to hit, stand, double down, or split depending on your hand and balance.
library(blackjack)
# Play a single round with a starting bet of $100
play_blackjack(bet = 100, n_decks = 4)You can also run a multiplayer session with up to several players each taking turns against the dealer:
play_blackjack_multi(num_players = 3, bet = 50)Working with cards
The package includes a set of utility functions for inspecting card properties. These are useful for building game logic or debugging:
library(blackjack)
deck <- create_shuffled_deck()
card <- deck[1]
card_name(card)[1] "5♥"
card_rank(card)[1] "Five"
card_value(card)[1] 5
card_is_face(card)[1] FALSE
Simulating strategy
The most analytically interesting part of the package is simulation_blackjack(). It lets you simulate thousands of rounds automatically and observe outcomes. This is where the probability really comes to life.
results <- simulation_blackjack(
n_simulations = 1000,
bet = 100,
n_decks = 4
)
summary(results)Number of cases in table: 1000
Number of factors: 1
From script to package
There’s a significant gap between writing a script that works on your machine and building a package that someone else can install, run, and trust. Closing that gap meant documentation for every function, unit tests for edge cases, and a Git workflow with a collaborator branching, reviewing, merging, and catching each other’s oversights before they became problems. The constraints of proper package development are uncomfortable at first, but they produce better software.