crossword

package module
v1.6.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2026 License: Apache-2.0 Imports: 16 Imported by: 4

README

Go Crossword

Package to generate a valid crossword puzzle from a list of words/clues.

Loosely based on: https://stackoverflow.com/a/22256214

The package does not guarantee all words can be placed in the grid but in general will usually get there if it's possible to do so (given enough attempts).

Interactive: DEMO

Example:

  $ go run example/simple/main.go sample/words.json  

or to see completed:

  $ SOLVE_ALL=true go run example/simple/main.go sample/words.json  

This will generate the following crossword from the given words file: sample/words.json

Note on interactive crosswords

If the crossword is being solved interactively you would need to store the generated Crossword (e.g. json encode it to a file). This can easily be decoded and rendered without altering the layout.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderPNG

func RenderPNG(c *Crossword, width, height int, opts ...RenderOption) (*gg.Context, error)

func RenderText

func RenderText(cw *Crossword, opts ...RenderOption) string

Types

type Cell

type Cell struct {
	Char    rune
	CharIdx int
}

func (Cell) Empty added in v1.2.0

func (c Cell) Empty() bool

func (Cell) String added in v1.2.0

func (c Cell) String() string

type Crossword

type Crossword struct {
	Grid       Grid
	Words      []Placement
	TotalScore int
}

func Generate

func Generate(gridSize int, words []Word, attempts int, opts ...GeneratorOpt) *Crossword

func (*Crossword) CellPlacements

func (cw *Crossword) CellPlacements(cellX, cellY int) []Placement

func (*Crossword) Solve

func (cw *Crossword) Solve()

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

func NewGenerator

func NewGenerator(gridSize int) *Generator

func (*Generator) Generate

func (g *Generator) Generate(words []Word, attempts int, opts ...GeneratorOpt) *Crossword

type GeneratorOpt added in v1.2.0

type GeneratorOpt func(opts *generatorOpts)

func WithAllAttempts added in v1.4.0

func WithAllAttempts(allAttempts bool) GeneratorOpt

WithAllAttempts will prevent return until all attempts have been exhausted. The result with the most crossed words will be returned.

func WithKeepSpecialCharacters added in v1.2.0

func WithKeepSpecialCharacters(keep bool) GeneratorOpt

func WithRevealFirstLetterOfEachWord added in v1.2.0

func WithRevealFirstLetterOfEachWord(reveal bool) GeneratorOpt

type Grid

type Grid [][]Cell

func NewGrid

func NewGrid(size int) Grid

type Placement

type Placement struct {
	ID       int
	Word     Word
	X        int
	Y        int
	Vertical bool
	// Solved reveals the characters of the word when it's rendered.
	Solved bool
}

func (Placement) ClueID

func (p Placement) ClueID() string

type RenderOption added in v1.1.0

type RenderOption func(opts *renderOpts)

func WithAllSolved added in v1.1.0

func WithAllSolved(solveAll bool) RenderOption

func WithBackgroundColor added in v1.3.0

func WithBackgroundColor(cl color.Color) RenderOption

func WithBorder added in v1.3.0

func WithBorder(width int) RenderOption

func WithClueColor added in v1.5.0

func WithClueColor(color color.Color) RenderOption

func WithClueFontSize added in v1.6.2

func WithClueFontSize(size float64) RenderOption

func WithClues added in v1.5.0

func WithClues(clues bool) RenderOption

func WithLabelColor added in v1.3.0

func WithLabelColor(cl color.Color) RenderOption

func WithRandomSolved added in v1.5.0

func WithRandomSolved() RenderOption

func WithWordBackgroundColor added in v1.3.0

func WithWordBackgroundColor(cl color.Color) RenderOption

func WithWordColor added in v1.3.0

func WithWordColor(cl color.Color) RenderOption

func WithWordFontSize added in v1.6.5

func WithWordFontSize(size float64) RenderOption

type Word

type Word struct {
	Word  string
	Clue  string
	Label *string

	// The number of letters in the original words(s). Since they are concatinated, we need this
	// to know the letter counts for multiple words.
	LettersCounts []int

	// CharacterHints allows subset of characters to be revealed (e.g. []int{0} would reveal
	// the first char of a word by default)
	CharacterHints []int
}

func WordsFromCSV added in v1.3.0

func WordsFromCSV(f io.Reader) ([]Word, error)

WordsFromCSV creates a word list from a CSV with 2 columns (word, clue) todo: update to support multiple words in the "word"

func (Word) LetterCountStr added in v1.6.0

func (w Word) LetterCountStr() string

Directories

Path Synopsis
server command
example
simple command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL