kstreet13 / mazing

Utilities for making and plotting mazes
Other
4 stars 1 forks source link

`print.maze()` #3

Open trevorld opened 6 months ago

trevorld commented 6 months ago
# remotes::install_github("trevorld/bittermelon")
print.maze <- function(x, ..., walls = FALSE, start = NULL, end = NULL) {
    stopifnot(requireNamespace("bittermelon", quietly = TRUE),
              packageVersion("bittermelon") >= "1.2.0-2")
    bm <- bittermelon::as_bm_bitmap(x, walls = walls, start = start, end = end)
    print(bm, ...)
}
set.seed(42)
m <- mazing::maze(16L, 32L) 
print(m, fg = "red", bg = "black", compress = "v")

maze1

print(m, start = "top", end = "bottom",
      px = c("\u2588", "\u2588", "\u25cf"), 
      fg = c("black", "white", "red"), bg = "white")

maze2

kstreet13 commented 6 months ago

Wow, thanks for sharing, this is so cool!

It looks a little different for me in R Studio and I'm not sure why I'm getting the gaps between lines:

Screenshot 2024-05-09 at 10 43 42 AM

But I still like this much better than the default (which is just a matrix and not even visually recognizable as a maze, aside from the extra attr(,"class") / [1] "maze" at the end).

I think I'll plan on adding bittermelon to Suggests and implementing the print method like this, so that it still does something if users don't have the right version:

print.maze <- function(x, ..., walls = FALSE, start = NULL, end = NULL) {
  if(requireNamespace("bittermelon", quietly = TRUE) &&
     packageVersion("bittermelon") >= "1.2.0-2"){
    bm <- bittermelon::as_bm_bitmap(x, walls = walls, start = start, end = end)
    print(bm, ...)
  }else{
    print.default(x)
  }
}

Thanks again, this is great!

trevorld commented 6 months ago

Cool!

It looks a little different for me in R Studio and I'm not sure why I'm getting the gaps between lines

You need the line spacing (often called line height) to be set to 1 for it to look good. I usually work in a Linux terminal where this is the default but it looks like RStudio sets this higher by default in their Console. I notice that the help for cli::boxx() singles out RStudio for doing this but doesn't mention a fix.

Theoretically I think you can customize this but after a couple minutes searching couldn't find an example of someone actually doing this for the RStudio console (there is an example of doing it for the editor). The relevant CSS property is line-height.

trevorld commented 5 months ago
trevorld commented 5 months ago

Experimentally using the proposed new by = 0.5 parameter for solve_maze() gives the following (non-compressed) output for maze solutions in my terminal:

Screenshot from 2024-05-12 11-27-34

trevorld commented 4 months ago
set.seed(42)
m <- mazing::maze(16L, 32L)
pal <- grDevices::palette.colors()
m |> as_bm_pixmap(start = "top", end = "bottom",
                  col = c(pal[6L], "white", pal[7L], pal[5L])) |>
   bm_pad(sides = 1L) |>
   print(compress = "vertical")

Screenshot_maze