suppressPackageStartupMessages({
  library(dplyr)
  library(magick)

  library(nonogram)
})
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7, fig.height=6
)

Create your own puzzles

To make, print and solve your own nonograms, you just need to create a puzzle string.

The puzzle string format used to define puzzles is quite simple:

puzzle_string <- "3:1:1,1-3:1:1,1"
solution      <- solve_puzzle(puzzle_string)
create_puzzle_plot(puzzle_string, solution)

Create your own puzzles from an image

Load a small black and white image

library(magick)
im <- magick::image_read(system.file("img", "Rlogo.jpg", package="jpeg"))


im <- im %>% 
  image_quantize(max=2, colorspace = 'gray', dither=TRUE) %>%
  image_scale(geometry = geometry_size_pixels(width=25, height=20, preserve_aspect=FALSE)) 

im

From the image, create an integer matrix with just zeros and ones

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Manipulating the image data to to matrix and then threshold, invert + tranpose
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mat <- t(1L - 1L * (im[[1]][1,,] > 180))

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# nonogram has a method for plotting a matrix in a standard way
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nonogram::create_matrix_plot(mat)

Create the puzzle from the matrix

Puzzles in nonogram are represented in 2 ways:

puzzle        <- create_puzzle_from_matrix(mat)
puzzle_string <- convert_puzzle_to_puzzle_string(puzzle)
cat("> puzzle")
deparse(puzzle) %>% paste(collapse="\n") %>% cat()

cat('> puzzle_string')
strwrap(puzzle_string, width = 60) %>% cat

Create a plot of the matrix and clues

create_puzzle_plot(puzzle, mat, show_clues=TRUE)

Create a blank plot of just the clues

create_puzzle_plot(puzzle, show_clues=TRUE)


coolbutuseless/nonogram documentation built on May 5, 2019, 3:45 a.m.