From 0331ea65f840701b5d4f580113cb1117d51c8214 Mon Sep 17 00:00:00 2001 From: Jiri Novotny <55046279+jiricodes@users.noreply.github.com> Date: Sat, 3 Jul 2021 06:42:16 +0300 Subject: [PATCH] fixed panic when printing snake out of bounds (#46) Co-authored-by: Jiri Novotny --- cli/commands/play.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cli/commands/play.go b/cli/commands/play.go index 5b110c2..cfdf0dc 100644 --- a/cli/commands/play.go +++ b/cli/commands/play.go @@ -4,9 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/BattlesnakeOfficial/rules" - "github.com/google/uuid" - "github.com/spf13/cobra" "io/ioutil" "log" "math/rand" @@ -16,6 +13,10 @@ import ( "strconv" "sync" "time" + + "github.com/BattlesnakeOfficial/rules" + "github.com/google/uuid" + "github.com/spf13/cobra" ) type Battlesnake struct { @@ -496,7 +497,9 @@ func printMap(state *rules.BoardState, outOfBounds []rules.Point, gameTurn int32 o.WriteString(fmt.Sprintf("Food ⚕: %v\n", state.Food)) for _, s := range state.Snakes { for _, b := range s.Body { - board[b.X][b.Y] = Battlesnakes[s.ID].Character + if b.X >= 0 && b.X < state.Width && b.Y >= 0 && b.Y < state.Height { + board[b.X][b.Y] = Battlesnakes[s.ID].Character + } } o.WriteString(fmt.Sprintf("%v %c: %v\n", Battlesnakes[s.ID].Name, Battlesnakes[s.ID].Character, s)) }