Skip to content

Instantly share code, notes, and snippets.

@andland
Created January 13, 2020 16:09
Show Gist options
  • Save andland/d6712a5a63faf5267e972a3a2059eb0f to your computer and use it in GitHub Desktop.
Save andland/d6712a5a63faf5267e972a3a2059eb0f to your computer and use it in GitHub Desktop.
Simulates the probability of winning the First Orchard Game https://www.habausa.com/my-very-first-games-first-orchard/
roll_die <- function(status, strategy) {
roll = sample(c(names(status), "basket"), 1)
if (roll == "basket") {
trees = status[names(status) != "raven" & status > 0]
if (strategy == "optimal") {
biggest_trees = trees[trees == max(trees)]
roll = sample(names(biggest_trees), 1)
} else if (strategy == "random") {
roll = sample(names(trees), 1)
} else if (strategy == "worst") {
smallest_trees = trees[trees == min(trees)]
roll = sample(names(smallest_trees), 1)
}
}
status[[roll]] = max(0, status[[roll]] - 1)
return(status)
}
play_game <- function(strategy = c("optimal", "random", "worst")) {
strategy = match.arg(strategy)
status = c(
red = 4,
green = 4,
blue = 4,
yellow = 4,
raven = 5
)
while (TRUE) {
status = roll_die(status, strategy)
if (status[["raven"]] == 0) {
return(0)
} else if (all(status[names(status) != "raven"] == 0)) {
return(1)
}
}
}
optimal_games = vapply(1:100000, function(x) play_game(strategy = "optimal"), FUN.VALUE = 0)
random_games = vapply(1:100000, function(x) play_game(strategy = "random"), FUN.VALUE = 0)
worst_games = vapply(1:100000, function(x) play_game(strategy = "worst"), FUN.VALUE = 0)
mean(optimal_games) # 0.63175
mean(random_games) # 0.59786
mean(worst_games) # 0.55344
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment