install.packages("devtools")
devtools::install_github("MonashARP/game-package-bilbies")Blackjack simulator: A gamified R package
![]()
The idea
Blackjack is a game of probability. Every decision — hit or stand — 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 with Andy Tran and Rong Xu. 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] "A♠"
card_rank(card)[1] "Ace"
card_value(card)[1] 11
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
What I learned
Building a package rather than just a script forced a completely different way of thinking. Every function needs documentation, every edge case needs a unit test, and everything needs to pass R CMD check before it can be shared. The C++ component for deal_hand() was particularly challenging — it was my first time writing compiled code inside an R package — but the performance improvement made it worth it.
The simulation function is what I’m most proud of. Watching the distribution of outcomes emerge over thousands of rounds is a genuinely satisfying demonstration of probability in action.