Skip to content

Instantly share code, notes, and snippets.

@sshine
Created January 28, 2014 07:08
Show Gist options
  • Save sshine/8663407 to your computer and use it in GitHub Desktop.
Save sshine/8663407 to your computer and use it in GitHub Desktop.
# RISK Attack Simulation
attack <- function(na, nb) {
die <- 1:6
while (na > 0 & nb > 0) {
attackers <- min(na, 3)
defenders <- min(nb, 2)
fights <- min(attackers, defenders)
hit_a <- sort(sample(die, attackers), decreasing=T)[1:fights]
hit_b <- sort(sample(die, defenders), decreasing=T)[1:fights]
victories_a <- sum(hit_a > hit_b)
victories_b <- sum(hit_a <= hit_b)
na <- na - victories_b
nb <- nb - victories_a
}
na > 0
}
probVictory <- function(na, nb) {
n <- 1000
sum(replicate(n, attack(na, nb), "array")) / n * 100
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment