From 6a6003cbb673886b797b0058e7eb7207ddd02485 Mon Sep 17 00:00:00 2001
From: Matthew Henderson <matthew.james.henderson@gmail.com>
Date: Tue, 23 Jul 2024 08:29:59 +0000
Subject: [PATCH] Add the Room R6 class.

---
 NAMESPACE      |  2 ++
 R/room-class.R | 65 +++++++++++++++++++++++++++++++++++++++++++
 man/Room.Rd    | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 142 insertions(+)
 create mode 100644 R/room-class.R
 create mode 100644 man/Room.Rd

diff --git a/NAMESPACE b/NAMESPACE
index ad89be0..ebf3205 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -1,5 +1,6 @@
 # Generated by roxygen2: do not edit by hand
 
+export(Room)
 export(empty_cells)
 export(empty_room)
 export(is_maximal_proom)
@@ -8,3 +9,4 @@ export(is_room)
 export(n_filled_cells)
 export(unused_pairs)
 export(volume)
+importFrom(R6,R6Class)
diff --git a/R/room-class.R b/R/room-class.R
new file mode 100644
index 0000000..a7b4aa1
--- /dev/null
+++ b/R/room-class.R
@@ -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)
+    }
+    
+  )
+)
\ No newline at end of file
diff --git a/man/Room.Rd b/man/Room.Rd
new file mode 100644
index 0000000..f42c395
--- /dev/null
+++ b/man/Room.Rd
@@ -0,0 +1,75 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/room-class.R
+\docType{class}
+\name{Room}
+\alias{Room}
+\title{Create a Room square}
+\format{
+An \code{\link{R6Class}} generator object
+}
+\description{
+Create a Room square
+
+Create a Room square
+}
+\section{Methods}{
+\subsection{Public methods}{
+\itemize{
+\item \href{#method-Room-new}{\code{Room$new()}}
+\item \href{#method-Room-set}{\code{Room$set()}}
+\item \href{#method-Room-is_available}{\code{Room$is_available()}}
+\item \href{#method-Room-clone}{\code{Room$clone()}}
+}
+}
+\if{html}{\out{<hr>}}
+\if{html}{\out{<a id="method-Room-new"></a>}}
+\if{latex}{\out{\hypertarget{method-Room-new}{}}}
+\subsection{Method \code{new()}}{
+\subsection{Usage}{
+\if{html}{\out{<div class="r">}}\preformatted{Room$new(size = NA)}\if{html}{\out{</div>}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{<div class="arguments">}}
+\describe{
+\item{\code{size}}{the order of the Room square to be created}
+}
+\if{html}{\out{</div>}}
+}
+}
+\if{html}{\out{<hr>}}
+\if{html}{\out{<a id="method-Room-set"></a>}}
+\if{latex}{\out{\hypertarget{method-Room-set}{}}}
+\subsection{Method \code{set()}}{
+\subsection{Usage}{
+\if{html}{\out{<div class="r">}}\preformatted{Room$set(e, p)}\if{html}{\out{</div>}}
+}
+
+}
+\if{html}{\out{<hr>}}
+\if{html}{\out{<a id="method-Room-is_available"></a>}}
+\if{latex}{\out{\hypertarget{method-Room-is_available}{}}}
+\subsection{Method \code{is_available()}}{
+\subsection{Usage}{
+\if{html}{\out{<div class="r">}}\preformatted{Room$is_available(e, p)}\if{html}{\out{</div>}}
+}
+
+}
+\if{html}{\out{<hr>}}
+\if{html}{\out{<a id="method-Room-clone"></a>}}
+\if{latex}{\out{\hypertarget{method-Room-clone}{}}}
+\subsection{Method \code{clone()}}{
+The objects of this class are cloneable with this method.
+\subsection{Usage}{
+\if{html}{\out{<div class="r">}}\preformatted{Room$clone(deep = FALSE)}\if{html}{\out{</div>}}
+}
+
+\subsection{Arguments}{
+\if{html}{\out{<div class="arguments">}}
+\describe{
+\item{\code{deep}}{Whether to make a deep clone.}
+}
+\if{html}{\out{</div>}}
+}
+}
+}