Add maze pkg
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
// Package cell represents a single cell in a maze grid.
|
||||
package cell
|
||||
|
||||
import (
|
||||
"code.chimeric.al/dwrz/minotaur/pkg/maze/direction"
|
||||
)
|
||||
|
||||
// Cell is a bitmask of open passages from a cell to its neighbors.
|
||||
// Bit d (1 << d) is 1 when the passage in direction d is open, and 0 when a
|
||||
// wall blocks that direction.
|
||||
type Cell uint8
|
||||
|
||||
// HasDirection reports whether the passage in direction d is open.
|
||||
func (c Cell) HasDirection(d direction.Direction) bool {
|
||||
return c&(1<<d) != 0
|
||||
}
|
||||
|
||||
// SetDirection opens the passage in direction d.
|
||||
func (c *Cell) SetDirection(d direction.Direction) {
|
||||
*c |= 1 << d
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cell
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.chimeric.al/dwrz/minotaur/pkg/maze/direction"
|
||||
)
|
||||
|
||||
func TestSetDirection(t *testing.T) {
|
||||
var c Cell
|
||||
t.Logf("start: %08b", c)
|
||||
|
||||
c.SetDirection(direction.Right)
|
||||
t.Logf("set right: %08b", c)
|
||||
|
||||
c.SetDirection(direction.Down)
|
||||
t.Logf("set down: %08b", c)
|
||||
|
||||
var (
|
||||
hasRight = c.HasDirection(direction.Right)
|
||||
hasDown = c.HasDirection(direction.Down)
|
||||
hasLeft = c.HasDirection(direction.Left)
|
||||
hasUp = c.HasDirection(direction.Up)
|
||||
)
|
||||
|
||||
t.Logf("has right: %v", hasRight)
|
||||
t.Logf("has down: %v", hasDown)
|
||||
t.Logf("has left: %v", hasLeft)
|
||||
t.Logf("has up: %v", hasUp)
|
||||
|
||||
if !hasRight {
|
||||
t.Errorf("expected cell to have right")
|
||||
}
|
||||
if !hasDown {
|
||||
t.Errorf("expected cell to have down")
|
||||
}
|
||||
if hasLeft {
|
||||
t.Errorf("expected cell to not have left")
|
||||
}
|
||||
if hasUp {
|
||||
t.Errorf("expected cell to not have up")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user