38 lines
1.1 KiB
R
38 lines
1.1 KiB
R
|
|
||
|
|
||
|
#' Combine Dice
|
||
|
#'
|
||
|
#' This function is a way to combine two independent dice rolls into a new die.
|
||
|
#'
|
||
|
#' @param x A die
|
||
|
#' @param y A die
|
||
|
#' @param fun A function that takes two vectors containing the possible
|
||
|
#' combinations of die rolls of \code{x} and \code{y}; the output should be of
|
||
|
#' the same length. Example - \code{pmax} is such a function and is used to
|
||
|
#' take the maximum of two rolls.
|
||
|
#'
|
||
|
#' @return A new die with the possible possibilities and their corresponding
|
||
|
#' probabilties
|
||
|
#' @export
|
||
|
#'
|
||
|
#' @examples
|
||
|
#' summed.die <- combineDice(d4, d6, `+`)
|
||
|
#' advantaged.die <- combineDice(d4, d4, pmax)
|
||
|
combineDice <- function(x, y, fun){
|
||
|
rolls <- expand.grid(x.numbers = x$numbers, y.numbers = y$numbers)
|
||
|
rolls$x.probs <- getProbabilitiy(x, rolls$x.numbers)
|
||
|
rolls$y.probs <- getProbabilitiy(y, rolls$y.numbers)
|
||
|
rolls$combined.prob <- rolls$x.probs*rolls$y.probs
|
||
|
rolls$combined.num <- fun(rolls$x.numbers, rolls$y.numbers)
|
||
|
|
||
|
new.die.probs <- rolls %>%
|
||
|
group_by(combined.num) %>%
|
||
|
summarize(prob = sum(combined.prob)) %>%
|
||
|
arrange(combined.num)
|
||
|
|
||
|
new.die <- makeDie(new.die.probs$combined.num, new.die.probs$prob)
|
||
|
|
||
|
return(new.die)
|
||
|
|
||
|
}
|