-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.go
109 lines (87 loc) · 3.3 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
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 (
"log"
"math/rand"
)
// 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: "", // TODO: Your Battlesnake username
Color: "#888888", // TODO: Choose color
Head: "default", // TODO: Choose head
Tail: "default", // TODO: 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
}
// TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
// boardWidth := state.Board.Width
// boardHeight := state.Board.Height
// TODO: Step 2 - Prevent your Battlesnake from colliding with itself
// mybody := state.You.Body
// TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
// opponents := state.Board.Snakes
// Are there any safe moves left?
safeMoves := []string{}
for move, isSafe := range isMoveSafe {
if isSafe {
safeMoves = append(safeMoves, move)
}
}
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))]
// 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 main() {
RunServer()
}