2019-01-15 20:25:32 +00:00
|
|
|
package soko
|
|
|
|
|
|
|
|
import "opslag.de/schobers/geom"
|
|
|
|
|
|
|
|
type Direction int
|
|
|
|
|
|
|
|
const (
|
|
|
|
DirectionUp Direction = iota
|
|
|
|
DirectionRight
|
|
|
|
DirectionDown
|
|
|
|
DirectionLeft
|
|
|
|
)
|
|
|
|
|
|
|
|
var Directions = [4]Direction{DirectionUp, DirectionRight, DirectionDown, DirectionLeft}
|
|
|
|
|
2019-01-16 06:33:04 +00:00
|
|
|
func (d Direction) Invert() Direction {
|
|
|
|
switch d {
|
|
|
|
case DirectionUp:
|
|
|
|
return DirectionDown
|
|
|
|
case DirectionRight:
|
|
|
|
return DirectionLeft
|
|
|
|
case DirectionDown:
|
|
|
|
return DirectionUp
|
|
|
|
case DirectionLeft:
|
|
|
|
return DirectionRight
|
|
|
|
}
|
|
|
|
panic("invalid direction")
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:25:32 +00:00
|
|
|
func (d Direction) String() string {
|
|
|
|
switch d {
|
|
|
|
case DirectionUp:
|
|
|
|
return "up"
|
|
|
|
case DirectionRight:
|
|
|
|
return "right"
|
|
|
|
case DirectionDown:
|
|
|
|
return "down"
|
|
|
|
case DirectionLeft:
|
|
|
|
return "left"
|
|
|
|
}
|
|
|
|
return "invalid"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d Direction) ToPoint() geom.Point {
|
|
|
|
switch d {
|
|
|
|
case DirectionUp:
|
|
|
|
return geom.Pt(0, -1)
|
|
|
|
case DirectionRight:
|
|
|
|
return geom.Pt(1, 0)
|
|
|
|
case DirectionDown:
|
|
|
|
return geom.Pt(0, 1)
|
|
|
|
case DirectionLeft:
|
|
|
|
return geom.Pt(-1, 0)
|
|
|
|
}
|
|
|
|
return geom.Point{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Neighbours(p geom.Point) []geom.Point {
|
|
|
|
neighbours := make([]geom.Point, 4)
|
|
|
|
for i, dir := range Directions {
|
|
|
|
neighbours[i] = p.Add(dir.ToPoint())
|
|
|
|
}
|
|
|
|
return neighbours
|
|
|
|
}
|