Skip to content

Commit 77dc043

Browse files
committed
Avoid hazards
1 parent ad4316b commit 77dc043

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

internal/battle/simple/simple.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (s *Snake) Move(state internal.GameState) (internal.Action, error) {
2626
return internal.Action{Move: possibleDirections[0]}, nil
2727
}
2828

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

43+
// Avoid hazards if possible
44+
avoidHazards := util.AvoidHazards(state.Board, state.You.Head)
45+
avoidHazardsPriorityMap := map[string]int{}
46+
47+
for _, dir := range avoidHazards {
48+
avoidHazardsPriorityMap[dir] = 2
49+
}
50+
4251
avoidSelfPriorityMap := util.MoveAwayFromSelf(state.You)
4352

44-
return internal.Action{Move: findOptimal(possibleDirections, foodPriorityMap, avoidSelfPriorityMap)}, nil
53+
return internal.Action{Move: findOptimal(possibleDirections, foodPriorityMap, avoidSelfPriorityMap, avoidHazardsPriorityMap)}, nil
4554
}
4655

4756
// Info returns the style info for a simple snake

internal/battle/util/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ func AvoidOthers(board internal.Board, head internal.Coord) []string {
6666
return decideDir(pos, avoid)
6767
}
6868

69+
// AvoidHazards returns moves that prevent snake from running into hazards
70+
func AvoidHazards(board internal.Board, head internal.Coord) []string {
71+
pos := potentialPositions(head)
72+
avoid := convertCoordsToGrid(board.Hazards)
73+
74+
return decideDir(pos, avoid)
75+
}
76+
6977
// FindFood returns moves that gives food
7078
func FindFood(board internal.Board, head internal.Coord) []string {
7179
noFood := AvoidFood(board, head)

0 commit comments

Comments
 (0)