Skip to content

Commit

Permalink
Avoid hazards
Browse files Browse the repository at this point in the history
  • Loading branch information
bethanyj28 committed Sep 26, 2021
1 parent ad4316b commit 77dc043
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion internal/battle/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (s *Snake) Move(state internal.GameState) (internal.Action, error) {
return internal.Action{Move: possibleDirections[0]}, nil
}

// Calculate need to find food and prioritize
food := make([]string, 4)
switch {
case state.You.Health > 50:
Expand All @@ -39,9 +40,17 @@ func (s *Snake) Move(state internal.GameState) (internal.Action, error) {
foodPriorityMap[dir] = 1
}

// Avoid hazards if possible
avoidHazards := util.AvoidHazards(state.Board, state.You.Head)
avoidHazardsPriorityMap := map[string]int{}

for _, dir := range avoidHazards {
avoidHazardsPriorityMap[dir] = 2
}

avoidSelfPriorityMap := util.MoveAwayFromSelf(state.You)

return internal.Action{Move: findOptimal(possibleDirections, foodPriorityMap, avoidSelfPriorityMap)}, nil
return internal.Action{Move: findOptimal(possibleDirections, foodPriorityMap, avoidSelfPriorityMap, avoidHazardsPriorityMap)}, nil
}

// Info returns the style info for a simple snake
Expand Down
8 changes: 8 additions & 0 deletions internal/battle/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ func AvoidOthers(board internal.Board, head internal.Coord) []string {
return decideDir(pos, avoid)
}

// AvoidHazards returns moves that prevent snake from running into hazards
func AvoidHazards(board internal.Board, head internal.Coord) []string {
pos := potentialPositions(head)
avoid := convertCoordsToGrid(board.Hazards)

return decideDir(pos, avoid)
}

// FindFood returns moves that gives food
func FindFood(board internal.Board, head internal.Coord) []string {
noFood := AvoidFood(board, head)
Expand Down

0 comments on commit 77dc043

Please sign in to comment.