Skip to content

Commit

Permalink
Add the Room R6 class.
Browse files Browse the repository at this point in the history
  • Loading branch information
MHenderson committed Jul 23, 2024
1 parent 90e3894 commit 6a6003c
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(Room)
export(empty_cells)
export(empty_room)
export(is_maximal_proom)
Expand All @@ -8,3 +9,4 @@ export(is_room)
export(n_filled_cells)
export(unused_pairs)
export(volume)
importFrom(R6,R6Class)
65 changes: 65 additions & 0 deletions R/room-class.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#' Create a Room square
#'
#' @docType class
#' @importFrom R6 R6Class
#'
#' @param size the order of the Room square to be created
#'
#' @export
#' @format An \code{\link{R6Class}} generator object
Room <- R6::R6Class(
classname = "Room",
public = list(

size = NULL,
cells = NULL,
symbols = NULL,
free_pairs = NULL,
empty_cells = NULL,

initialize = function(size = NA) {

self$size <- size
self$symbols <- 0:(self$size - 1)

self$cells <- tidyr::expand_grid(row = 1:(self$size - 1), col = 1:(self$size - 1)) |>
dplyr::mutate(first = as.integer(NA), second = as.integer(NA)) |>
dplyr::mutate(avail = list(0:(self$size - 1)))

self$free_pairs <- all_pairs(self$size)
self$empty_cells <- all_ordered_pairs(self$size - 1)

},

set = function(e, p) {

self$cells[self$cells$row == e[1] & self$cells$col == e[2], "first"] <- p[1]
self$cells[self$cells$row == e[1] & self$cells$col == e[2], "second"] <- p[2]

self$cells[self$cells$row == e[1], "avail"]$avail <- lapply(self$cells[self$cells$row == e[1], "avail"]$avail, remove_both, p)
self$cells[self$cells$col == e[2], "avail"]$avail <- lapply(self$cells[self$cells$col == e[2], "avail"]$avail, remove_both, p)

self$free_pairs <- self$free_pairs[-match(list(p), self$free_pairs)]
self$empty_cells <- self$empty_cells[-match(list(e), self$empty_cells)]

},

is_available = function(e, p) {
p[1] %in% self$cells[self$cells$row == e[1] & self$cells$col == e[2], "avail"]$avail[[1]] && p[2] %in% self$cells[self$cells$row == e[1] & self$cells$col == e[2], "avail"]$avail[[1]]
}

),
active = list(

n_filled = function() {
self$cells |>
dplyr::filter(!is.na(first)) |>
nrow()
},

volume = function() {
round(self$n_filled/choose(max(self$cells$col) + 1, 2), 6)
}

)
)
75 changes: 75 additions & 0 deletions man/Room.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6a6003c

Please sign in to comment.