maze

package
v0.0.0-...-60c38a5 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package maze generates a 2D maze

Index

Constants

View Source
const (
	// X Represents the X value of X,Y coordinates in a 2D maze
	X = iota
	// Y Represents the Y value of X,Y coordinates in a 2D maze
	Y
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

type Cell = int

Cell represents a cell in a grid or maze ranging from 0 to X*Y-1

type Coords

type Coords [2]int

Coords represents the X,Y coordinates of a cell in a maze

type Direction

type Direction = int

Direction is an enum representing 4 possible values Up, Down, Left and Right

const (
	Right Direction = iota
	Down
	Up
	Left
)

Valid Directions

type Maze

type Maze struct {
	// DimX and DimY are the dimensions of the maze
	DimX, DimY int
	// Cells is a slice of DimX * DimY maze cells, each cell has an array of two booleans represnting the existance of the passages Right and Down
	// If DimX is 5 then the x,y coordinates of cell of index 7 are 2,1 (index % DimX, index/DimY)
	// If the passage to the right of the cell is open then the first boolean is true
	// If the passage down from the cell is open then the second boolean is true.
	Cells [][2]bool
}

Maze represents a 2D maze. The zero value is a very uninteresting case of a maze with no cells. Use New to generate a valid maze of the desired dimensions.

func New

func New(x, y int) Maze

New generates a random maze using Prim's algorithm (non-simplified) The maze doesn't contain cycles, to generate a cycle remove any wall at random

func (Maze) CellFromCoords

func (m Maze) CellFromCoords(coords Coords) Cell

CellFromCoords takes x,y coordinates and returns the correrlating cell

func (Maze) CoordsFromCell

func (m Maze) CoordsFromCell(c Cell) Coords

CoordsFromCell takes a cell and returns its x,y coordinates

func (Maze) PathDown

func (m Maze) PathDown(cell Cell) bool

PathDown returns true if the path between the current cell and the cell below is open

func (Maze) PathRight

func (m Maze) PathRight(cell Cell) bool

PathRight returns true if the path between the current cell and the cell to its right is open

func (*Maze) RemoveWall

func (m *Maze) RemoveWall(c Cell, d Direction)

RemoveWall doesn't validate the existence of the wall to remove - use at own peril

func (Maze) Walls

func (m Maze) Walls(c Cell) (walls []Wall)

Walls returns the valid walls of Cell c

type Wall

type Wall struct {
	C, T Cell      // C for current Cell, T for target cell
	D    Direction // D for Direction
}

Wall represents a wall between 2 cells in a 2D maze

Directories

Path Synopsis
Package travel provides travel for package maze
Package travel provides travel for package maze

Jump to

Keyboard shortcuts

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