R/board.R

#' A Reference Class to represent the Board of a game
#'
#' @field matrix structure that supports basic vector indexing to store states of cells
#' @field shrink_counter counts how many steps passed since the last time the board was shrinked
#' @export Board
#' @exportClass board
#' @import Matrix
#' @import raster
Board = setRefClass("board",
fields = list(
  matrix = "Matrix",
  shrink_counter = "numeric"
  ),
  methods = list(
    applyRule = function(rule) {
      "Applies given rule to the boards matrix"
      too_small <- check_size_too_small(matrix)
      if (TRUE %in% too_small) {
        matrix <<- enlarge_matrix(matrix, too_small)
        shrink_counter <<- 0
      }

      if (shrink_counter >= 10) {
        too_big <- check_size_too_big(matrix)
        if (TRUE %in% too_big) {
          shrink_counter <<- 0
          matrix <<- shrink_matrix(matrix, too_big)
        }
      }
      matrix <<- iterate_board(matrix,rule)
      shrink_counter <<- shrink_counter + 1
    },

  plotBoard = function() {
      "Plots board matrix"
      plot(
      raster(as.matrix(matrix)),
      legend = FALSE,
      col = c("white", "blue"),
      xaxt = 'n',
      yaxt = 'n'
    )}))
chrlen/gameOfLifeAgain documentation built on July 9, 2019, 11:42 a.m.