nonogram

package module
v0.0.0-...-ca0f091 Latest Latest
Warning

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

Go to latest
Published: May 10, 2025 License: MIT Imports: 8 Imported by: 0

README

nonogram

Nonogram solver written in pure go

Problem

Nonograms, also known as Hanjie, Paint by Numbers, Griddlers, Pic-a-Pix, and Picross, are picture logic puzzles in which cells in a grid must be colored or left blank according to numbers at the edges of the grid to reveal a hidden picture. See for more details. Example of solved nonogram: drawing

Get started

package main

import (
    "github.com/Arzeeq/nonogram"
)

func main() {
    rows := nonogram.FillPattern{{1}, {1, 1}, {1}}
	columns := nonogram.FillPattern{{1}, {1, 1}, {1}}

	var s nonogram.Solver
	err := s.Solve(rows, columns)
	if err != nil {
		panic(err)
	}
	print(s.String())
}

See example and solver app for more details.

Output mode

After solving nonogram with nonogram.Solver you can access the result using 4 modes:
String() StringCaged(cage int) PrettyString() PrettyStringCaged(cage int)

xx##xxxx######x     xx##x│xxx##│####x     ╳╳██╳╳╳╳██████╳     ╳╳██╳│╳╳╳██│████╳
#xx#xxxxx######     #xx#x│xxxx#│#####     █╳╳█╳╳╳╳╳██████     █╳╳█╳│╳╳╳╳█│█████
xxx###########x     xxx##│#####│####x     ╳╳╳███████████╳     ╳╳╳██│█████│████╳
#xxxxx###xxxx##     #xxxx│x###x│xxx##     █╳╳╳╳╳███╳╳╳╳██     █╳╳╳╳│╳███╳│╳╳╳██
###x#######xx##     ###x#│#####│#xx##     ███╳███████╳╳██     ███╳█│█████│█╳╳██
xx#x#x#####xxx#     ─────┼─────┼─────     ╳╳█╳█╳█████╳╳╳█     ─────┼─────┼─────
##########xxxxx     xx#x#│x####│#xxx#     ██████████╳╳╳╳╳     ╳╳█╳█│╳████│█╳╳╳█
xxx#######xxxxx     #####│#####│xxxxx     ╳╳╳███████╳╳╳╳╳     █████│█████│╳╳╳╳╳
xxxx##xx##xxxxx     xxx##│#####│xxxxx     ╳╳╳╳██╳╳██╳╳╳╳╳     ╳╳╳██│█████│╳╳╳╳╳
xxxxx#xxx##xxxx     xxxx#│#xx##│xxxxx     ╳╳╳╳╳█╳╳╳██╳╳╳╳     ╳╳╳╳█│█╳╳██│╳╳╳╳╳
#xxxxxxxx#xx###     xxxxx│#xxx#│#xxxx     █╳╳╳╳╳╳╳╳█╳╳███     ╳╳╳╳╳│█╳╳╳█│█╳╳╳╳
xxx##xx###xx##x     ─────┼─────┼─────     ╳╳╳██╳╳███╳╳██╳     ─────┼─────┼─────
######xx##xx#xx     #xxxx│xxxx#│xx###     ██████╳╳██╳╳█╳╳     █╳╳╳╳│╳╳╳╳█│╳╳███
##x#xxx#####xxx     xxx##│xx###│xx##x     ██╳█╳╳╳█████╳╳╳     ╳╳╳██│╳╳███│╳╳██╳
##x#xxx####xxxx     #####│#xx##│xx#xx     ██╳█╳╳╳████╳╳╳╳     █████│█╳╳██│╳╳█╳╳
                    ##x#x│xx###│##xxx                         ██╳█╳│╳╳███│██╳╳╳
                    ##x#x│xx###│#xxxx                         ██╳█╳│╳╳███│█╳╳╳╳

or you can call solver.ToNonogram() and use the same modes:
String() StringCaged(cage int) PrettyString() PrettyStringCaged(cage int)

..##....######.     ..##.│...##│####.       ██    ██████        ██ │   ██│████ 
#..#.....######     #..#.│....#│#####     █  █     ██████     █  █ │    █│█████
...###########.     ...##│#####│####.        ███████████         ██│█████│████ 
#.....###....##     #....│.###.│...##     █     ███    ██     █    │ ███ │   ██
###.#######..##     ###.#│#####│#..##     ███ ███████  ██     ███ █│█████│█  ██
..#.#.#####...#     ─────┼─────┼─────       █ █ █████   █     ─────┼─────┼─────
##########.....     ..#.#│.####│#...#     ██████████            █ █│ ████│█   █
...#######.....     #####│#####│.....        ███████          █████│█████│     
....##..##.....     ...##│#####│.....          ██  ██            ██│█████│     
.....#...##....     ....#│#..##│.....          █   ██             █│█  ██│     
#........#..###     .....│#...#│#....     █        █  ███          │█   █│█    
...##..###..##.     ─────┼─────┼─────        ██  ███  ██      ─────┼─────┼─────
######..##..#..     #....│....#│..###     ██████  ██  █       █    │    █│  ███
##.#...#####...     ...##│..###│..##.     ██ █   █████           ██│  ███│  ██ 
##.#...####....     #####│#..##│..#..     ██ █   ████         █████│█  ██│  █  
                    ##.#.│..###│##...                         ██ █ │  ███│██   
                    ##.#.│..###│#....                         ██ █ │  ███│█    

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCanNotSolve = errors.New("can not solve this puzzle completely")
View Source
var ErrContradiction = errors.New("found contradiction")
View Source
var ErrInvalidGrid = errors.New("grid has less/more data than needed")
View Source
var ErrInvalidSize = errors.New("nonogram has invalid size")
View Source
var ErrNilPattern = errors.New("called solve with nil pattern")

Functions

func EncodedSize

func EncodedSize(n, m int) int

Types

type FillPattern

type FillPattern [][]int

FillPattern is a numbers written at the edge of puzzle. These numbers show the len of unbroken lines of filled-in squares there are in any given row or column. There are at least one blank square between filled-in lines.

       22
    09922440
   ┌────────┐
  0│........│
  4│.####...│
  6│.######.│
  6│.######.│
2 2│.##..##.│
2 2│.##..##.│
  4│.####...│
  2│.##.....│
  2│.##.....│
  2│.##.....│
  0│........│
   └────────┘

type Nonogram

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

func FromGrid

func FromGrid(n, m int, grid []uint64) (*Nonogram, error)

func Gen

func Gen(n, m int) *Nonogram

func New

func New(n, m int) *Nonogram

func (*Nonogram) Clear

func (n *Nonogram) Clear(i, j int)

func (*Nonogram) Fill

func (n *Nonogram) Fill(i, j int)

func (*Nonogram) FillPatterns

func (n *Nonogram) FillPatterns() (FillPattern, FillPattern)

return rows and columns FillPattern respectively

func (*Nonogram) Get

func (n *Nonogram) Get(i, j int) bool

func (*Nonogram) PrettyString

func (n *Nonogram) PrettyString() string

func (*Nonogram) PrettyStringCaged

func (n *Nonogram) PrettyStringCaged(cage int) string

func (*Nonogram) String

func (n *Nonogram) String() string

func (*Nonogram) StringCaged

func (n *Nonogram) StringCaged(cage int) string

type Solver

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

func (*Solver) PrettyString

func (s *Solver) PrettyString() string

func (*Solver) PrettyStringCaged

func (s *Solver) PrettyStringCaged(cage int) string

func (*Solver) SavePNG

func (s *Solver) SavePNG(name string, scale int) error

When solver saves png, it paints cells in black if it's Filled, in white if it's Blank and in red if it's Unknown

func (*Solver) Solve

func (s *Solver) Solve(rows FillPattern, columns FillPattern) error

func (*Solver) String

func (s *Solver) String() string

func (*Solver) StringCaged

func (s *Solver) StringCaged(cage int) string

func (*Solver) ToNonogram

func (s *Solver) ToNonogram() *Nonogram

type State

type State int
const (
	Unknown State = iota
	Filled
	Blank
)

Directories

Path Synopsis
cmd
solver command

Jump to

Keyboard shortcuts

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