maze

package
v0.0.0-...-ca62e5c Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Maze

type Maze struct {
	Size       image.Rectangle
	Start, End image.Point
	// contains filtered or unexported fields
}

func Make

func Make(size image.Rectangle, r *rand.Rand) Maze

Make creates a new maze of the given size, using the given random number generator.

If r is nil, the default random number generator will be used.

func (Maze) Down

func (m Maze) Down(p image.Point) bool

Returns true if you can move down from the given point.

func (Maze) Draw

func (m Maze) Draw(path []image.Point) *image.RGBA

Returns a drawing of the maze, mostly for debugging purposes. An optional path can be drawn on top of the maze.

func (Maze) Left

func (m Maze) Left(p image.Point) bool

Returns true if you can move to the left from the given point.

func (Maze) Right

func (m Maze) Right(p image.Point) bool

Returns true if you can move to the right from the given point.

func (Maze) Up

func (m Maze) Up(p image.Point) bool

Returns true if you can move up from the given point.

type Problem

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

A Problem is intended to be used by the Solve function to find a path through a maze.

This is more of a demonstration of how to use the Solve function, a dedicated maze solving algorithm would more efficient. In particular, the solver manages a max heap for discarding states when at capacity, which a maze solver would not need - a maze has a finite number of cells, which for our purposes represent a state, and we'd never exceed (and thus need to keep track of and discard) that many states.

func NewProblem

func NewProblem(size image.Rectangle, start, end image.Point, rightTest func(image.Point) bool, downTest func(image.Point) bool) *Problem

NewMazeProblem creates a new maze problem with the given size, start and end points, and well as two functions that test if movement to the right and down is allowed from a given point.

func (Problem) Discard

func (p Problem) Discard(image.Point)

This function would be used to release any resources associated with a given search state, but since a state for this problem is just a point, and all the actual state information is stored in the cells slice, there's nothing to do here.

func (Problem) Finish

func (p Problem) Finish(c image.Point) ([]image.Point, error)

Converts the given position, the one that passed the Solved test above, into a list of points that form a path from the start to the end.

func (*Problem) Initialize

func (p *Problem) Initialize(out []image.Point) ([]image.Point, error)

Initializes the maze search problem, and appends the start point to the given slice.

func (Problem) Next

func (p Problem) Next(c image.Point, out []image.Point) ([]image.Point, error)

Appends the next positions to consider from the given position.

func (Problem) OptimisticLess

func (p Problem) OptimisticLess(a, b image.Point) bool

Returns true if state a should be explored before b.

func (Problem) PessimisticLess

func (p Problem) PessimisticLess(a, b image.Point) bool

Similar to OptimisticLess, but we scale the heuristic to penalize uncertainty about the actual remaining distance.

Note that the solver uses this with a max heap to determine which states to prune when at capacity.

This will typically be similar to OptimisticLess, but with a penalty for uncertainty.

For our purposes, we're just going to use the same heuristic as OptimisticLess. Assuming that you give the solver a capacity equal to the number of cells in the maze, it won't need to prune any states.

func (Problem) Solved

func (p Problem) Solved(c image.Point) bool

Returns true if the given position is the end of the maze.

We're also assuming that the point was returned by Initialize or Next, otherwise we wouldn't know how to reach the start position, and this wouldn't actually be solved.

Jump to

Keyboard shortcuts

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