knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
   fig.width = 7, 
  out.width = '100%'
)

Example 1: Basic Crossword Puzzle

library(worrrd)
library(tidyverse)
library(rvest)
library(ggtext)
library(emoji)

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)
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)
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)
plot(ex2, clues = TRUE, solution = TRUE, title = "Math Makes Me Cross")


anthonypileggi/worrrd documentation built on Jan. 13, 2023, 11:15 a.m.