R/rule.R

#' A Reference Class to represent a rule to be applied to a board
#'
#' @field survive vector of integers that determines which count of neighbors of a livin cell leads to survival
#' @field revive vector of integers that determines which count of neighbors of a dead cell leads to revival
#' @exportClass rule
#' @export Rule
Rule = setRefClass("rule", fields = list(survive = "numeric", revive = "numeric"),
  methods = list(apply = function(sums, living) {
    "Takes neighborhood sums and indication of living or dead and returns new states"
    states <- vector(mode = "numeric", length = length(sums))
    states[living] <- ifelse(sums[living] %in% .self$survive, TRUE, FALSE)
    states[!living] <- ifelse(sums[!living] %in% .self$revive, TRUE, FALSE)
    states
  }))
chrlen/gameOfLifeAgain documentation built on July 9, 2019, 11:42 a.m.