-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
286 lines (234 loc) · 7.06 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
)
type Game struct {
ID string `json:"id"`
Timeout int32 `json:"timeout"`
}
type Coord struct {
X int `json:"x"`
Y int `json:"y"`
}
type Battlesnake struct {
ID string `json:"id"`
Name string `json:"name"`
Health int32 `json:"health"`
Body []Coord `json:"body"`
Head Coord `json:"head"`
Length int32 `json:"length"`
Shout string `json:"shout"`
}
type Board struct {
Height int `json:"height"`
Width int `json:"width"`
Food []Coord `json:"food"`
Snakes []Battlesnake `json:"snakes"`
}
type BattlesnakeInfoResponse struct {
APIVersion string `json:"apiversion"`
Author string `json:"author"`
Color string `json:"color"`
Head string `json:"head"`
Tail string `json:"tail"`
}
type GameRequest struct {
Game Game `json:"game"`
Turn int `json:"turn"`
Board Board `json:"board"`
You Battlesnake `json:"you"`
}
type MoveResponse struct {
Move string `json:"move"`
Shout string `json:"shout,omitempty"`
}
// HandleIndex is called when your Battlesnake is created and refreshed
// by play.battlesnake.com. BattlesnakeInfoResponse contains information about
// your Battlesnake, including what it should look like on the game board.
func HandleIndex(w http.ResponseWriter, r *http.Request) {
response := BattlesnakeInfoResponse{
APIVersion: "1",
Author: "shogibear", // Your Battlesnake username
Color: "#45b9e3", // Personalize Color
Head: "gamer", // Personalize Head
Tail: "bolt", // Personalize Tail
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
if err != nil {
log.Fatal(err)
}
}
// HandleStart is called at the start of each game your Battlesnake is playing.
// The GameRequest object contains information about the game that's about to start.
func HandleStart(w http.ResponseWriter, r *http.Request) {
request := GameRequest{}
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
log.Fatal(err)
}
// Nothing to respond with here
fmt.Print("START\n")
}
// HandleMove is called for each turn of each game.
// Valid responses are "up", "down", "left", or "right".
func HandleMove(w http.ResponseWriter, r *http.Request) {
request := GameRequest{}
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
log.Fatal(err)
}
Turn := request.Turn
fmt.Println(Turn)
/* trying to get names to print alongside the turn numbers
for _, players := range request.Board.Snakes {
PlayerNames := players.Name
fmt.Println(PlayerNames)
}
*/
// Choose a random direction to move in
PossibleMoves := []string{"up", "right", "down", "left"}
//or start by just choosing one like "right"
//move := "up"
//move in a circle
//move := possibleMoves[request.Turn % 4]
head := request.You.Head
body := request.You.Body
enemy := request.Board.Snakes
BoardHeight := request.Board.Height -1
BoardWidth := request.Board.Width -1
//Avoid walls
if head.Y == BoardHeight {
PossibleMoves = Remove(PossibleMoves, "up")
}
if head.X == BoardWidth {
PossibleMoves = Remove(PossibleMoves, "right")
}
if head.Y == 0 {
PossibleMoves = Remove(PossibleMoves, "down")
}
if head.X == 0 {
PossibleMoves = Remove(PossibleMoves, "left")
}
//Avoid Self
for _, pos := range body {
if head.X -1 == pos.X && head.Y == pos.Y {
PossibleMoves = Remove(PossibleMoves, "left")
}
if head.X +1 == pos.X && head.Y == pos.Y {
PossibleMoves = Remove(PossibleMoves, "right")
}
if head.Y +1 == pos.Y && head.X == pos.X {
PossibleMoves = Remove(PossibleMoves, "up")
}
if head.Y -1 == pos.Y && head.X == pos.X {
PossibleMoves = Remove(PossibleMoves, "down")
}
}
//avoid enemy Snakes
for _, snake := range enemy {
for _, pos := range snake.Body{
if head.X -1 == pos.X && head.Y == pos.Y {
PossibleMoves = Remove(PossibleMoves, "left")
}
if head.X +1 == pos.X && head.Y == pos.Y {
PossibleMoves = Remove(PossibleMoves, "right")
}
if head.Y +1 == pos.Y && head.X == pos.X {
PossibleMoves = Remove(PossibleMoves, "up")
}
if head.Y -1 == pos.Y && head.X == pos.X {
PossibleMoves = Remove(PossibleMoves, "down")
}
//don't hit head straight on
if head.X -2 == snake.Head.X && head.Y == snake.Head.Y {
PossibleMoves = Remove(PossibleMoves, "left")
fmt.Println("avoided head-on")
}
if head.X +2 == snake.Head.X && head.Y == snake.Head.Y{
PossibleMoves = Remove(PossibleMoves, "right")
fmt.Println("avoided head-on")
}
if head.Y -2 == snake.Head.Y && head.X == snake.Head.X{
PossibleMoves = Remove(PossibleMoves, "down")
fmt.Println("avoided head-on")
}
if head.Y +2 == snake.Head.Y && head.X == snake.Head.X{
PossibleMoves = Remove(PossibleMoves, "up")
fmt.Println("avoided head-on")
}
//avoid diagonals
//NW diag
if head.X -1 == snake.Head.X && head.Y +1 == snake.Head.Y {
PossibleMoves = Remove(PossibleMoves, "left")
PossibleMoves = Remove(PossibleMoves, "up")
fmt.Println("avoided diagonal")
}
//NE diag
if head.X +1 == snake.Head.X && head.Y +1 == snake.Head.Y {
PossibleMoves = Remove(PossibleMoves, "right")
PossibleMoves = Remove(PossibleMoves, "up")
fmt.Println("avoided diagonal")
}
//SW diag
if head.X -1 == snake.Head.X && head.Y -1 == snake.Head.Y {
PossibleMoves = Remove(PossibleMoves, "left")
PossibleMoves = Remove(PossibleMoves, "down")
fmt.Println("avoided diagonal")
}
//SE diag
if head.X +1 == snake.Head.X && head.Y -1 == snake.Head.Y {
PossibleMoves = Remove(PossibleMoves, "left")
PossibleMoves = Remove(PossibleMoves, "down")
fmt.Println("avoided diagonal")
}
}
}
move := PossibleMoves[rand.Intn(len(PossibleMoves))]
response := MoveResponse{
Move: move,
}
fmt.Println(PossibleMoves)
fmt.Printf("MOVE: %s\n", response.Move)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
log.Fatal(err)
}
}
func Remove(s []string, elem string) []string {
for k, v := range s {
if v == elem {
return append(s[:k:k], s[k+1:]...)
}
}
return s
}
// HandleEnd is called when a game your Battlesnake was playing has ended.
// It's purely for informational purposes, no response required.
func HandleEnd(w http.ResponseWriter, r *http.Request) {
request := GameRequest{}
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
log.Fatal(err)
}
// Nothing to respond with here
fmt.Print("END\n")
}
func main() {
port := os.Getenv("PORT")
if len(port) == 0 {
port = "8080"
}
http.HandleFunc("/", HandleIndex)
http.HandleFunc("/start", HandleStart)
http.HandleFunc("/move", HandleMove)
http.HandleFunc("/end", HandleEnd)
fmt.Printf("Starting Battlesnake Server at http://0.0.0.0:%s...\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}