fixed panic when printing snake out of bounds (#46)

Co-authored-by: Jiri Novotny <hello@jiricodes.com>
This commit is contained in:
Jiri Novotny 2021-07-03 06:42:16 +03:00 committed by GitHub
parent f65f6d65d7
commit 0331ea65f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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))
}