Skip to content

Commit 54e01d6

Browse files
committed
Fix assertive logic and better update for wrapped
1 parent 473eac0 commit 54e01d6

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

internal/battle/clairvoyant/util.go

+45-12
Original file line numberDiff line numberDiff line change
@@ -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--

internal/structs.go

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ type Coord struct {
5353
Y int `json:"y"`
5454
}
5555

56+
// Equals returns if the coordinate is equal to another one
57+
func (c Coord) Equals(other Coord) bool {
58+
if c.X == other.X && c.Y == other.Y {
59+
return true
60+
}
61+
return false
62+
}
63+
5664
// Style is the style of the snake
5765
type Style struct {
5866
Color string `json:"color"`

0 commit comments

Comments
 (0)