@@ -50,6 +50,10 @@ func convertBoardToMatrix(state internal.GameState) matrix {
5050 obj = you
5151 }
5252 for _ , coord := range snake .Body {
53+ if coord .Equals (snake .Head ) && obj != you {
54+ grid = addCoordToMatrix (coord , grid , snake .ID )
55+ continue
56+ }
5357 grid = addCoordToMatrix (coord , grid , obj .String ())
5458 }
5559 }
@@ -75,25 +79,54 @@ func addCoordToMatrix(coord internal.Coord, grid matrix, object string) matrix {
7579 return grid
7680}
7781
78- func potentialPositions (head internal.Coord ) choices {
79- return choices {
82+ func potentialPositions (head internal.Coord , state internal. GameState ) choices {
83+ c := choices {
8084 left : internal.Coord {X : head .X - 1 , Y : head .Y },
8185 right : internal.Coord {X : head .X + 1 , Y : head .Y },
8286 up : internal.Coord {X : head .X , Y : head .Y + 1 },
8387 down : internal.Coord {X : head .X , Y : head .Y - 1 },
8488 }
89+
90+ if state .Game .Ruleset .Name == "wrapped" {
91+ return wrappedPotentialPositions (c , state .Board )
92+ }
93+
94+ return c
8595}
8696
87- func checkPossible ( initialState internal. GameState , grid matrix , potential internal.Coord , otherSnakeLength map [ string ] int32 ) bool {
88- // check walls
89- if initialState . Game . Ruleset . Name != "wrapped" {
90- if potential .X >= initialState . Board . Width || potential . X < 0 {
91- return false
97+ func wrappedPotentialPositions ( c choices , board internal.Board ) choices {
98+ for _ , coord := range c {
99+ if coord . X >= board . Width {
100+ coord .X = 0
101+ continue
92102 }
93103
94- if potential .Y >= initialState .Board .Height || potential .Y < 0 {
95- return false
104+ if coord .X < 0 {
105+ coord .X = board .Width - 1
106+ continue
96107 }
108+
109+ if coord .Y >= board .Height {
110+ coord .Y = 0
111+ continue
112+ }
113+
114+ if coord .Y < 0 {
115+ coord .Y = board .Height - 1
116+ continue
117+ }
118+ }
119+ return c
120+ }
121+
122+ func checkPossible (initialState internal.GameState , grid matrix , potential internal.Coord , otherSnakeLength map [string ]int32 ) bool {
123+ // check walls
124+ if potential .X >= initialState .Board .Width || potential .X < 0 {
125+ return false
126+ }
127+
128+ if potential .Y >= initialState .Board .Height || potential .Y < 0 {
129+ return false
97130 }
98131
99132 // check hazards
@@ -148,14 +181,14 @@ func findOptimal(state internal.GameState, movesLeft int) string {
148181 if snake .ID == state .You .ID {
149182 continue
150183 }
151- p := potentialPositions (snake .Head )
184+ p := potentialPositions (snake .Head , state )
152185 for _ , pp := range p {
153186 grid = moveEnemiesForward (snake .Head , pp , snake , grid )
154187 }
155188 }
156189
157190 health := float64 (state .You .Health ) / 100
158- potential := potentialPositions (state .You .Head )
191+ potential := potentialPositions (state .You .Head , state )
159192 analysis := map [direction ]float64 {}
160193 otherSnakes := mapSnakeLengths (state .Board .Snakes )
161194 for d , p := range potential {
@@ -216,7 +249,7 @@ func lookahead(movesLeft int, r route, initialState internal.GameState, otherSna
216249 }
217250 }
218251
219- potential := potentialPositions (option )
252+ potential := potentialPositions (option , initialState )
220253 grid = addCoordToMatrix (option , grid , you .String ())
221254 for _ , p := range potential {
222255 movesLeft --
0 commit comments