Example 1: Basic Crossword Puzzle

Crossword puzzles are required to be intersecting, so it may not be possible to include every word that is provided. Choosing the method = 'optimal' will place words in order of maximal overlap with every other word, while method = 'random' will place words in a random order and may therefore result in more words being excluded from the puzzle.

dat <- 
  dplyr::tribble(
    ~word,   ~clue,
    "dog",   "Bark. Bark. Bark.",
    "cat",   "Purrr",
    "horse", "Neighhhhh",
    "frog",  "Ribbit Ribbit",
    "cow",   "Moooooooo",
    "fox",   "Nee Nee Nee (What does the ____ say?)",
    "sheep", "Bleat",
    "snake", "Hissss",
    "duck",  "Quack",
    "bird",  "Chirp"
  )
ex1 <- crossword(words = dat$word, clues = dat$clue, r = 40, c = 40)
#> Could not place the following words:
#> 
#> COW
#> FROG
#> DOG
#> DUCK
#> BIRD
#> Found positions for 5/10 words.
plot(ex1, solution = TRUE, clues = TRUE)

Example 2: Math Problem Puzzle

Here we create math problems randomly, and then use the english package to automatically convert the numeric solutions into words that are then placed into the puzzle.

library(english)
#> Warning: package 'english' was built under R version 4.1.3
dat <- dplyr::tibble(
  n1 = round(20 * runif(20)),
  n2 = round(20 * runif(20)),
  sign = sample(c("+", "-"), 20, replace = T),
  problem = paste(n1, sign, n2),
  solution = purrr::map_dbl(problem, ~eval(parse(text = .x))),
  solution_text = as.english(solution)
) %>%
  dplyr::filter(
    solution > 0,           # only solutions > 0
    !duplicated(solution)   # no duplicates allowed
  )

ex2 <- crossword(words = dat$solution_text, clues = dat$problem, r = 50, c = 50)
#> Found positions for 14/14 words.
plot(ex2, clues = TRUE, solution = TRUE, title = "Math Makes Me Cross")