maze

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 7 Imported by: 10

README

maze

CI Status

maze

Usage

The maze command without the arguments prints the random maze to the standard output.

maze

maze

We can play the maze on the terminal with --interactive.

maze --interactive

maze

The --format color is a good option to print the colored maze. Also we can specify the size of the maze with --width and --height.

maze --width 20 --height 10 --format color

maze

We can toggle the solution with the s key. maze

If we change the font size of the terminal smaller, we get a large maze. maze

Installation

Homebrew
brew install itchyny/tap/maze
Build from source
go install github.com/itchyny/maze/cmd/maze@latest

Bug Tracker

Report bug at Issues・itchyny/maze - GitHub.

Author

itchyny (https://github.com/itchyny)

License

This software is released under the MIT License, see LICENSE.

Special thanks

Special thanks to the termbox-go library.

References

Documentation

Overview

Package maze provides Maze struct and generating algorithm.

Index

Constants

View Source
const (
	Up = 1 << iota
	Down
	Left
	Right
)

Maze cell configurations The paths of the maze is represented in the binary representation.

View Source
const (
	SolutionOffset = 4
	VisitedOffset  = 8
)

The solution path is represented by (Up|Down|Left|Right) << SolutionOffset. The user's path is represented by (Up|Down|Left|Right) << VisitedOffset.

Variables

View Source
var Color = &Format{
	Wall:               "\x1b[7m  \x1b[0m",
	Path:               "  ",
	StartLeft:          "S ",
	StartRight:         " S",
	GoalLeft:           "G ",
	GoalRight:          " G",
	Solution:           "\x1b[44;1m  \x1b[0m",
	SolutionStartLeft:  "\x1b[44;1mS \x1b[0m",
	SolutionStartRight: "\x1b[44;1m S\x1b[0m",
	SolutionGoalLeft:   "\x1b[44;1mG \x1b[0m",
	SolutionGoalRight:  "\x1b[44;1m G\x1b[0m",
	Visited:            "\x1b[42;1m  \x1b[0m",
	VisitedStartLeft:   "\x1b[42;1mS \x1b[0m",
	VisitedStartRight:  "\x1b[42;1m S\x1b[0m",
	VisitedGoalLeft:    "\x1b[42;1mG \x1b[0m",
	VisitedGoalRight:   "\x1b[42;1m G\x1b[0m",
	Cursor:             "\x1b[43;1m  \x1b[0m",
}

Color format

View Source
var Default = &Format{
	Wall:               "##",
	Path:               "  ",
	StartLeft:          "S ",
	StartRight:         " S",
	GoalLeft:           "G ",
	GoalRight:          " G",
	Solution:           "::",
	SolutionStartLeft:  "S:",
	SolutionStartRight: ":S",
	SolutionGoalLeft:   "G:",
	SolutionGoalRight:  ":G",
	Visited:            "..",
	VisitedStartLeft:   "S.",
	VisitedStartRight:  ".S",
	VisitedGoalLeft:    "G.",
	VisitedGoalRight:   ".G",
	Cursor:             "::",
}

Default format

View Source
var Directions = []int{Up, Down, Left, Right}

Directions is the set of all the directions

View Source
var Opposite = map[int]int{Up: Down, Down: Up, Left: Right, Right: Left}

Opposite directions

Functions

This section is empty.

Types

type Format

type Format struct {
	Wall               string
	Path               string
	StartLeft          string
	StartRight         string
	GoalLeft           string
	GoalRight          string
	Solution           string
	SolutionStartLeft  string
	SolutionStartRight string
	SolutionGoalLeft   string
	SolutionGoalRight  string
	Visited            string
	VisitedStartLeft   string
	VisitedStartRight  string
	VisitedGoalLeft    string
	VisitedGoalRight   string
	Cursor             string
}

Format is the printing format of the maze

type Maze

type Maze struct {
	Directions [][]int
	Height     int
	Width      int
	Start      *Point
	Goal       *Point
	Cursor     *Point
	Solved     bool
	Started    bool
	Finished   bool
	// contains filtered or unexported fields
}

Maze represents the configuration of a maze

func NewMaze

func NewMaze(height int, width int, options ...Option) *Maze

NewMaze creates a new maze

func (*Maze) Clear

func (maze *Maze) Clear()

Clear the solution

func (*Maze) Connected

func (maze *Maze) Connected(point *Point, target *Point) bool

Connected judges whether the two points is connected by a path on the maze

func (*Maze) Contains

func (maze *Maze) Contains(point *Point) bool

Contains judges whether the argument point is inside the maze or not

func (*Maze) Generate

func (maze *Maze) Generate()

Generate the maze

func (*Maze) Move

func (maze *Maze) Move(direction int)

Move the cursor

func (*Maze) Neighbors

func (maze *Maze) Neighbors(point *Point) (neighbors []int)

Neighbors gathers the nearest undecided points

func (*Maze) Next

func (maze *Maze) Next(point *Point) *Point

Next advances the maze path randomly and returns the new point

func (*Maze) Print

func (maze *Maze) Print(writer io.Writer, format *Format)

Print out the maze to the IO writer

func (*Maze) PrintPNG added in v0.0.10

func (maze *Maze) PrintPNG(writer io.Writer, scale int)

PrintPNG outputs the maze to the IO writer as PNG image

func (*Maze) PrintSVG added in v0.0.10

func (maze *Maze) PrintSVG(writer io.Writer, scale int)

PrintSVG outputs the maze to the IO writer as SVG image

func (*Maze) Solve

func (maze *Maze) Solve()

Solve the maze

func (*Maze) String added in v0.0.8

func (maze *Maze) String(format *Format) string

func (*Maze) Undo

func (maze *Maze) Undo()

Undo the visited path

type Option added in v0.0.10

type Option func(*Maze)

Option is the option for the maze

func WithGoal added in v0.0.10

func WithGoal(goal *Point) Option

WithGoal sets the goal point of the maze

func WithRandSource added in v0.0.10

func WithRandSource(source rand.Source) Option

WithRandSource sets the random source of the maze

func WithStart added in v0.0.10

func WithStart(start *Point) Option

WithStart sets the start point of the maze

type Point

type Point struct {
	X, Y int
}

Point on the maze

func (*Point) Advance

func (point *Point) Advance(direction int) *Point

Advance the point forward by the argument direction

func (*Point) Equal

func (point *Point) Equal(target *Point) bool

Equal judges the equality of the two points

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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