DndDice/R/operators.R
2019-03-18 20:56:07 -07:00

89 lines
1.4 KiB
R

#' @export
'+.Die' <- function(x, y){
if(class(x) == "Die" & class(y) == "Die"){
return(sumTwoDie(x, y))
}
if(class(x) %in% c("numeric", "integer")){
die = y
constant = x
} else if(class(y) %in% c("numeric", "integer")){
die = x
constant = y
} else{
stop("Can only add two die or a die and a number")
}
if(length(constant) != 1){
stop("Length of constant must be 1")
}
die$numbers <- die$numbers + constant
return(die)
}
#' @export
'>.Die' <- function(x,y){
return(compareDie(x, y, `>`))
}
#' @export
'>=.Die' <- function(x,y){
return(compareDie(x, y, `>=`))
}
#' @export
'<.Die' <- function(x,y){
return(compareDie(x, y, `<`))
}
#' @export
'<=.Die' <- function(x,y){
return(compareDie(x, y, `<=`))
}
#' @export
'==.Die' <- function(x,y){
return(compareDie(x, y, `==`))
}
#' @export
'*.Die' <- function(x, y){
# At least one of the types should be numeric or integer
if(!(class(x) %in% c("numeric", "integer")) & !(class(y) %in% c("numeric", "integer"))){
stop("At least one type must be numeric")
}
if(class(x) == "Die"){
die = x
times = y
}
else{
die = y
times = x
}
times = as.integer(round(times))
if(times < 1){
stop("Cannot multiply a die by less than 1")
}
if(times == 1){
return(die)
}
result = die
for(i in 2:times){
result = result + die
}
return(result)
}