grid

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SortCells

func SortCells(cells []Cell)

SortCells sorts a slice of cells in place. Sorting is done first by Y coordinate, then by X coordinate.

Types

type Cell

type Cell struct {
	X int
	Y int
}

Cell is a coordinate of a cell in the infinite Game of Life plane.

func NewCell

func NewCell() *Cell

NewCell returns a zero-valued cell (0,0).

func NewCellFromCords

func NewCellFromCords(x, y int) *Cell

NewCellFromCords returns a cell with the given coordinates.

func (Cell) GetNeighbors added in v0.5.0

func (c Cell) GetNeighbors() [8]Cell

func (Cell) Inside added in v0.3.0

func (c Cell) Inside(rect *Rectangle) bool

type ClearCellObserverEvent added in v0.3.0

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

func (ClearCellObserverEvent) Data added in v0.3.0

func (e ClearCellObserverEvent) Data() Cell

func (ClearCellObserverEvent) Type added in v0.3.0

type ClearGridObserverEvent added in v0.3.0

type ClearGridObserverEvent struct{}

func (ClearGridObserverEvent) Type added in v0.3.0

type GlobalObserver added in v0.3.0

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

func NewGlobalObserver added in v0.3.0

func NewGlobalObserver(updateFunc func(event ObserverEvent)) *GlobalObserver

func (*GlobalObserver) Update added in v0.3.0

func (o *GlobalObserver) Update(event ObserverEvent)

type Grid

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

Grid holds the set of live cells and provides concurrent-safe operations to evolve and query a Game of Life universe.

func NewGrid

func NewGrid() *Grid

NewGrid creates an empty grid.

func NewGridFromBitmap

func NewGridFromBitmap(rect Rectangle, bitmap [][]bool) *Grid

NewGridFromBitmap creates a grid from a 2D boolean matrix positioned at rect.MinX, rect.MinY. bitmap[y][x] corresponds to the cell at (rect.MinX + x, rect.MinY + y).

func NewGridFromCells

func NewGridFromCells(cells ...Cell) *Grid

NewGridFromCells constructs a grid with the provided live cells. Usage: NewGridFromCells(Cell{X:1,Y:2}, Cell{X:3,Y:4})

func NewGridFromMap

func NewGridFromMap(alive map[Cell]bool) *Grid

NewGridFromMap constructs a grid from a map[Cell]bool (true means alive).

func NewGridFromStrings

func NewGridFromStrings(lines []string, aliveChars ...rune) *Grid

NewGridFromStrings creates a grid from ASCII art lines at origin (0,0). Any of the characters in aliveChars will be treated as a live cell. If none are provided, defaults to one of: '*', '#', 'X', 'O', '1'.

func NewGridFromXY

func NewGridFromXY(coords [][2]int) *Grid

NewGridFromXY constructs a grid from coordinate pairs. Example: NewGridFromXY([][2]int{{1,2},{3,4}})

func (*Grid) AddObserver added in v0.3.0

func (g *Grid) AddObserver(observer Observer)

func (*Grid) Bitmap

func (g *Grid) Bitmap(rect Rectangle) [][]bool

Bitmap returns a dense boolean matrix covering rect where true marks a live cell. bitmap[y][x] corresponds to the cell at (rect.MinX + x, rect.MinY + y).

func (*Grid) Bounds

func (g *Grid) Bounds() Rectangle

Bounds returns the minimal inclusive rectangle that contains all live cells. If the grid is empty, it returns (0,0,0,0).

func (*Grid) Clear added in v0.2.0

func (g *Grid) Clear()

func (*Grid) ClearCell

func (g *Grid) ClearCell(x, y int)

ClearCell marks the cell at (x, y) as dead.

func (*Grid) Clone

func (g *Grid) Clone() *Grid

Clone returns a deep copy of the grid.

func (*Grid) Compare

func (g1 *Grid) Compare(g2 *Grid) bool

Compare checks if the grid is identical to another grid.

func (*Grid) ComputeNextGrid

func (g *Grid) ComputeNextGrid() ([]Cell, []Cell)

ComputeNextGrid computes the next generation and returns the cells that will be born and that will die, without mutating the current grid.

func (*Grid) ComputeNextGridN

func (g *Grid) ComputeNextGridN(n int) ([]Cell, []Cell)

ComputeNextGridN computes the cells that would be born and die after n generations, without mutating the current grid.

func (*Grid) Diff

func (g *Grid) Diff(other *Grid) ([]Cell, []Cell)

Diff compares the current grid with another grid and returns the born and died cells.

func (*Grid) Hash

func (g *Grid) Hash() uint64

Hash returns a stable 64-bit FNV-1a hash of the set of live cells. The hash is deterministic across runs and independent of map iteration order.

func (*Grid) IsAlive

func (g *Grid) IsAlive(x, y int) bool

IsAlive reports whether the cell at (x, y) is alive.

func (*Grid) Population

func (g *Grid) Population() int

Population returns the number of live cells in the grid.

func (*Grid) RemoveObserver added in v0.3.0

func (g *Grid) RemoveObserver(observer Observer)

func (*Grid) SetCell

func (g *Grid) SetCell(x, y int)

SetCell marks the cell at (x, y) as alive.

func (*Grid) Step

func (g *Grid) Step(n int) ([]Cell, []Cell)

Step advances the grid by n generations and returns the aggregate born and died cells.

func (*Grid) Subgrid

func (g *Grid) Subgrid(rect Rectangle) *Grid

Subgrid returns a new grid containing only the live cells within rect. Coordinates are preserved (absolute positions).

func (*Grid) SubgridNormalized

func (g *Grid) SubgridNormalized(rect Rectangle) *Grid

SubgridNormalized returns a new grid containing only the live cells within rect, translated so that (rect.MinX, rect.MinY) becomes (0, 0).

func (*Grid) Tick

func (g *Grid) Tick() ([]Cell, []Cell)

Tick advances the grid by one generation and returns the born and died cells.

func (*Grid) Translate

func (g *Grid) Translate(dx, dy int)

Translate shifts all live cells by (dx, dy). Positive dx moves right; positive dy moves down.

type Observer added in v0.3.0

type Observer interface {
	Update(event ObserverEvent)
}

type ObserverEvent added in v0.3.0

type ObserverEvent interface {
	Type() ObserverEventType
}

type ObserverEventType added in v0.3.0

type ObserverEventType string
const (
	SetCellEventType   ObserverEventType = "set_cell"
	ClearCellEventType ObserverEventType = "clear_cell"
	TickEventType      ObserverEventType = "tick"
	ClearGridEventType ObserverEventType = "clear_grid"
	StepEventType      ObserverEventType = "step"
)

type Rectangle

type Rectangle struct {
	X1 int
	Y1 int
	X2 int
	Y2 int
}

Rectangle is an inclusive axis-aligned bounding box on the integer grid.

func NewRectangle

func NewRectangle(x1, y1, x2, y2 int) *Rectangle

NewRectangle constructs a rectangle (inclusive bounds).

func (*Rectangle) Height added in v0.3.1

func (r *Rectangle) Height() int

Height returns the height of the rectangle.

func (*Rectangle) Max added in v0.9.0

func (r *Rectangle) Max() (int, int)

func (*Rectangle) Min added in v0.9.0

func (r *Rectangle) Min() (int, int)

func (*Rectangle) Normalized added in v0.9.0

func (r *Rectangle) Normalized() *Rectangle

func (*Rectangle) PointInside added in v0.7.0

func (r *Rectangle) PointInside(x, y int) bool

account for inverted cords

func (*Rectangle) ToArray added in v0.4.0

func (r *Rectangle) ToArray() [4]int

func (*Rectangle) ToNestedArray added in v0.4.0

func (r *Rectangle) ToNestedArray() [2][2]int

func (*Rectangle) Width added in v0.3.1

func (r *Rectangle) Width() int

Width returns the width of the rectangle.

type RegionObserver added in v0.3.0

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

func NewRegionObserver added in v0.3.0

func NewRegionObserver(region Rectangle, updateFunc func(event ObserverEvent)) *RegionObserver

func (*RegionObserver) GetRegion added in v0.3.0

func (o *RegionObserver) GetRegion() Rectangle

func (*RegionObserver) SetRegion added in v0.3.0

func (o *RegionObserver) SetRegion(region Rectangle)

func (*RegionObserver) Update added in v0.3.0

func (o *RegionObserver) Update(event ObserverEvent)

type SetCellObserverEvent added in v0.3.0

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

func (SetCellObserverEvent) Data added in v0.3.0

func (e SetCellObserverEvent) Data() Cell

func (SetCellObserverEvent) Type added in v0.3.0

type StepObserverEvent added in v0.3.0

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

func (StepObserverEvent) Data added in v0.3.0

func (e StepObserverEvent) Data() (int, []Cell, []Cell)

func (StepObserverEvent) Type added in v0.3.0

type TickObserverEvent added in v0.3.0

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

func (TickObserverEvent) Data added in v0.3.0

func (e TickObserverEvent) Data() ([]Cell, []Cell)

func (TickObserverEvent) Type added in v0.3.0

Jump to

Keyboard shortcuts

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