forked from BattlesnakeOfficial/starter-snake-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
262 lines (211 loc) · 7.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
// Welcome to
// __________ __ __ .__ __
// \______ \_____ _/ |__/ |_| | ____ ______ ____ _____ | | __ ____
// | | _/\__ \\ __\ __\ | _/ __ \ / ___// \\__ \ | |/ // __ \
// | | \ / __ \| | | | | |_\ ___/ \___ \| | \/ __ \| <\ ___/
// |________/(______/__| |__| |____/\_____>______>___|__(______/__|__\\_____>
//
// This file can be a nice home for your Battlesnake logic and helper functions.
//
// To get you started we've included code to prevent your Battlesnake from moving backwards.
// For more info see docs.battlesnake.com
import (
"fmt"
"log"
)
// info is called when you create your Battlesnake on play.battlesnake.com
// and controls your Battlesnake's appearance
// TIP: If you open your Battlesnake URL in a browser you should see this data
func info() BattlesnakeInfoResponse {
log.Println("INFO")
return BattlesnakeInfoResponse{
APIVersion: "1",
Author: "yusufmalikul", // Your Battlesnake username
Color: "#0040ff", // Choose color
Head: "earmuffs", // Choose head
Tail: "bolt", // Choose tail
}
}
// start is called when your Battlesnake begins a game
func start(state GameState) {
log.Println("GAME START")
}
// end is called when your Battlesnake finishes a game
func end(state GameState) {
log.Printf("GAME OVER\n\n")
}
// move is called on every turn and returns your next move
// Valid moves are "up", "down", "left", or "right"
// See https://docs.battlesnake.com/api/example-move for available data
func move(state GameState) BattlesnakeMoveResponse {
isMoveSafe := map[string]bool{
"up": true,
"down": true,
"left": true,
"right": true,
}
// We've included code to prevent your Battlesnake from moving backwards
myHead := state.You.Body[0] // Coordinates of your head
myNeck := state.You.Body[1] // Coordinates of your "neck"
if myNeck.X < myHead.X { // Neck is left of head, don't move left
isMoveSafe["left"] = false
} else if myNeck.X > myHead.X { // Neck is right of head, don't move right
isMoveSafe["right"] = false
} else if myNeck.Y < myHead.Y { // Neck is below head, don't move down
isMoveSafe["down"] = false
} else if myNeck.Y > myHead.Y { // Neck is above head, don't move up
isMoveSafe["up"] = false
}
// Step 1 - Prevent your Battlesnake from moving out of bounds
boardWidth := state.Board.Width - 1
boardHeight := state.Board.Height - 1
if isMoveSafe["up"] && myHead.Y == boardHeight {
isMoveSafe["up"] = false
}
if isMoveSafe["down"] && myHead.Y == 0 {
isMoveSafe["down"] = false
}
if isMoveSafe["right"] && myHead.X == boardWidth {
isMoveSafe["right"] = false
}
if isMoveSafe["left"] && myHead.X == 0 {
isMoveSafe["left"] = false
}
log.Println("myHead.Y", myHead.Y, "boardHeight", boardHeight)
log.Println("myHead.X", myHead.X, "boardWidth", boardWidth)
// Step 2 - Prevent your Battlesnake from colliding with itself
// log.Println("start debug body")
mybody := state.You.Body
for _, v := range mybody {
// log.Println("body X:", v.X, "body Y:", v.Y)
if isMoveSafe["up"] && myHead.Y+1 == v.Y && myHead.X == v.X {
isMoveSafe["up"] = false
}
if isMoveSafe["down"] && myHead.Y-1 == v.Y && myHead.X == v.X {
isMoveSafe["down"] = false
}
if isMoveSafe["right"] && myHead.X+1 == v.X && myHead.Y == v.Y {
isMoveSafe["right"] = false
}
if isMoveSafe["left"] && myHead.X-1 == v.X && myHead.Y == v.Y {
isMoveSafe["left"] = false
}
}
// log.Println("end debug body")
// Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
opponents := state.Board.Snakes
for _, v := range opponents {
for _, snake := range v.Body {
// log.Println("body X:", snake.X, "body Y:", snake.Y)
if isMoveSafe["up"] && myHead.Y+1 == snake.Y && myHead.X == snake.X {
isMoveSafe["up"] = false
}
if isMoveSafe["down"] && myHead.Y-1 == snake.Y && myHead.X == snake.X {
isMoveSafe["down"] = false
}
if isMoveSafe["right"] && myHead.X+1 == snake.X && myHead.Y == snake.Y {
isMoveSafe["right"] = false
}
if isMoveSafe["left"] && myHead.X-1 == snake.X && myHead.Y == snake.Y {
isMoveSafe["left"] = false
}
}
}
// Prevent entering closed grid with no way to escape
// we will use flood fill algorithm
// First we mark all filled grid (filled by our snake body and other snake body)
moveSafeGridSize := map[string]int{
"up": 0,
"down": 0,
"left": 0,
"right": 0,
}
if isMoveSafe["up"] {
total := fill(prepareGrid(boardWidth+1, boardHeight+1, opponents), boardWidth, boardHeight, myHead.X, myHead.Y+1, "", "@")
moveSafeGridSize["up"] = total
}
if isMoveSafe["down"] {
total := fill(prepareGrid(boardWidth+1, boardHeight+1, opponents), boardWidth, boardHeight, myHead.X, myHead.Y-1, "", "@")
moveSafeGridSize["down"] = total
}
if isMoveSafe["right"] {
total := fill(prepareGrid(boardWidth+1, boardHeight+1, opponents), boardWidth, boardHeight, myHead.X+1, myHead.Y, "", "@")
moveSafeGridSize["right"] = total
}
if isMoveSafe["left"] {
total := fill(prepareGrid(boardWidth+1, boardHeight+1, opponents), boardWidth, boardHeight, myHead.X-1, myHead.Y, "", "@")
moveSafeGridSize["left"] = total
}
fmt.Println("isMoveSafe", isMoveSafe)
fmt.Println("moveSafeGridSize", moveSafeGridSize)
// Are there any safe moves left?
safeMoves := []string{}
for move, isSafe := range isMoveSafe {
if isSafe {
safeMoves = append(safeMoves, move)
}
}
log.Println("safeMoves:", safeMoves)
if len(safeMoves) == 0 {
log.Printf("MOVE %d: No safe moves detected! Moving down\n", state.Turn)
return BattlesnakeMoveResponse{Move: "down"}
}
// Choose a random move from the safe ones
// nextMove := safeMoves[rand.Intn(len(safeMoves))]
// Choose best move with largest room
nextMove := "up"
largestRoom := moveSafeGridSize["up"]
for i := range moveSafeGridSize {
if moveSafeGridSize[i] > largestRoom {
largestRoom = moveSafeGridSize[i]
nextMove = i
}
}
// TODO: Step 4 - Move towards food instead of random, to regain health and survive longer
// food := state.Board.Food
log.Printf("MOVE %d: %s\n", state.Turn, nextMove)
return BattlesnakeMoveResponse{Move: nextMove}
}
func fill(grid [][]string, boardWidth, boardHeight, x int, y int, old string, new string) int {
total := 0
if x < 0 {
return 0
}
if y < 0 {
return 0
}
if x > boardWidth {
return 0
}
if y > boardHeight {
return 0
}
if grid[x][y] == new || grid[x][y] != old {
return 0
}
grid[x][y] = "X"
total++
total += fill(grid, boardWidth, boardHeight, x+1, y, old, new)
total += fill(grid, boardWidth, boardHeight, x-1, y, old, new)
total += fill(grid, boardWidth, boardHeight, x, y+1, old, new)
total += fill(grid, boardWidth, boardHeight, x, y-1, old, new)
return total
}
func prepareGrid(width int, height int, opponents []Battlesnake) [][]string {
markedGrid := make([][]string, width)
for i := range markedGrid {
markedGrid[i] = make([]string, height)
}
for _, v := range opponents {
// fmt.Println("debug grid")
for _, snake := range v.Body {
// fmt.Println("X", snake.X, "Y", snake.Y)
markedGrid[snake.X][snake.Y] = "X"
}
}
return markedGrid
}
func main() {
RunServer()
}