Apple Game Simulation for Stockholm Bayes Club

← Back to Teaching

Game rules

In this game you compete against grandma. Grandma moves along a path from start to finish, while you try to collect as many apples as possible before she reaches the end.

Each round a six-sided die is rolled:

  • If the result is 1, 2, or 6, grandma moves forward that many spaces.
  • If the result is 3, 4, or 5, you collect that many apples instead.

There are two special squares on the path:

  • If grandma lands on 9, she takes a shortcut and moves directly to 14.
  • If grandma lands on 18, she must skip her next turn.

The game ends as soon as grandma reaches or passes square 21. At that moment the round stops immediately, and the number of apples you have collected is your final score.

Try one game


Distribution from 10,000 simulated games

Show code
library(ggplot2)

simulate_game <- function() {
  pos <- 0
  apples <- 0
  skip <- FALSE

  while (pos < 21) {

    if (skip) {
      skip <- FALSE
      next
    }

    roll <- sample(1:6, 1)

    if (roll %in% c(1,2,6)) {
      pos <- pos + roll

      if (pos == 9) pos <- 14
      if (pos >= 21) break
      if (pos == 18) skip <- TRUE

    } else {
      apples <- apples + roll
    }
  }

  apples
}

set.seed(123)

n_sim <- 10000
results <- replicate(n_sim, simulate_game())

df <- data.frame(apples = results)

ggplot(df, aes(x = apples)) +
  geom_histogram(
    binwidth = 1,
    boundary = -0.5,
    fill = "#E1635B",
    color = "white"
  ) +
  scale_x_continuous(breaks = seq(0, max(df$apples), by = 5)) +
  scale_y_continuous(
    breaks = scales::pretty_breaks(n = 6),
    labels = scales::comma
  ) +
  labs(
    title = "Distribution of Apples Collected",
    subtitle = "10,000 simulated games",
    x = "Apples collected",
    y = "Number of games"
  ) +
  theme_bw() +
  theme(panel.grid.minor = element_blank())

Estimated probability of winning the game for 10, 20, 30 apples.

Show code
library(scales)

p10 <- percent(mean(results >= 10))
p20 <- percent(mean(results >= 20))
p30 <- percent(mean(results >= 30))

cat(
  " ≥10 apples:", p10,"\n",
  "≥20 apples:", p20,"\n",
  "≥30 apples:", p30
)
 ≥10 apples: 88% 
 ≥20 apples: 65% 
 ≥30 apples: 41%

← Back to Teaching