Skip to content

Commit

Permalink
moved punishment helpers to vector.go
Browse files Browse the repository at this point in the history
  • Loading branch information
val antonini committed Apr 29, 2024
1 parent 0b85c60 commit 31217ed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
14 changes: 3 additions & 11 deletions astar.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,36 +141,28 @@ func (p Pathfinder) Find(startPos, endPos Vec2) []Vec2 {
return []Vec2{}
}

// punishChangeDirection returns a punishment for changing direction that can be applied to g.
func punishChangeDirection(q node, successor, end Vec2) int {
if q.parent == nil {
return 0
}
punishment := abs(successor.X-end.X) + abs(successor.Y-end.Y)

isHorizAdj := func(a, b Vec2) bool {
return a.Y-b.Y == 0
}
if !isHorizAdj(q.pos, successor) {
if isHorizAdj(q.pos, q.parent.pos) {
return punishment
}
}

isVertAdj := func(a, b Vec2) bool {
return a.X-b.X == 0
}
if !isVertAdj(q.pos, successor) {
if isVertAdj(q.pos, q.parent.pos) {
return punishment
}
}

// todo: check option if diagonal enabled
isDiagonal := func(a, b Vec2) bool {
return abs(a.X-b.X) == abs(a.Y-b.Y)
}
if !isDiagonal(q.pos, successor) {
if isDiagonal(q.pos, q.parent.pos) {
if !isDiagAdj(q.pos, successor) {
if isDiagAdj(q.pos, q.parent.pos) {
return punishment
}
}
Expand Down
8 changes: 4 additions & 4 deletions astar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func TestPath_Diagonal2(t *testing.T) {
}

func TestPath_PunishChangeDirection(t *testing.T) {
t.Skip()
t.Skip("unimplemented")
weights := []int{
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
Expand All @@ -253,9 +253,9 @@ func TestPath_PunishChangeDirection(t *testing.T) {

want := []Vec2{
{1, 3},
{2, 3},
{3, 3},
{3, 2},
{1, 2},
{1, 1},
{2, 1},
{3, 1},
}
equal(t, got, want, &grid)
Expand Down
15 changes: 15 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ type Vec2 struct {
X int
Y int
}

// isHorizAdj returns true if a and b are horizontally adjacent.
func isHorizAdj(a, b Vec2) bool {
return a.Y-b.Y == 0
}

// isVertAdj returns true if a and b are vertically adjacent.
func isVertAdj(a, b Vec2) bool {
return a.X-b.X == 0
}

// isDiagAdj returns true if a and b are diagonally adjacent.
func isDiagAdj(a, b Vec2) bool {
return abs(a.X-b.X) == abs(a.Y-b.Y)
}

0 comments on commit 31217ed

Please sign in to comment.