DndDice/R/internal_functions.R
Joel Therrien 566b48ff62 Bug fixes for when die roll is non-integer
Also added a reroll function and simplified code greatly
2019-05-29 10:35:13 -07:00

44 lines
924 B
R

getProbabilitiy <- function(x, numbers){
positions = match(numbers, x$numbers)
probs = x$probs[positions]
probs[is.na(probs)] = 0
return(probs)
}
# comparerFun(x, y); which both x & y as Die, or one of them as numeric
compareDie <- function(x, y, comparerFun){
if(class(x) == "Die" & class(y) == "Die"){
return(combineDice(x, y, comparerFun))
}
if(class(x) %in% c("numeric", "integer")){
die = y
constant = x
xNumbers = constant
yNumbers = die$numbers
} else if(class(y) %in% c("numeric", "integer")){
die = x
constant = y
xNumbers = die$numbers
yNumbers = constant
} else{
stop("Can only compare two die or a die and a number")
}
if(length(constant) != 1){
stop("Length of constant must be 1")
}
matchResults = comparerFun(xNumbers, yNumbers)
probTrue = sum(die$probs[matchResults])
z = makeDie(c(F,T), probs=c(1-probTrue, probTrue))
}