go-battlesnake-server
is a Battlesnake HTTP server written in Go. It builds upon the official Go Starter Snake, with a couple of logging improvements and a slightly simpler setup, so you can quickly dive into creating your Battlesnake AI.
You will need to have Go 1.18 or higher installed. You can install the package by running the following command:
go get github.com/your-username/go-battlesnake-server
To get started, import the package into your Go code, define your move function and pass it to the server along with a log.Logger
and few other options. Below is a example main.go
to demonstrate an example usage.
package main
import (
"log"
"os"
"github.com/haguro/go-battlesnake-server/server"
)
const (
version = "0.1"
author = "you"
color = "#33ccff"
head = "default"
tail = "default"
)
func main() {
logOptions := server.LDefault
if os.Getenv("DEBUG") == "TRUE" {
logOptions |= server.LDebug
}
logger := log.New(os.Stderr, "[my_battlesnake]", log.LstdFlags|log.Lmicroseconds)
logger.Fatal(
server.New(
"8080",
&server.InfoResponse{
Author: author,
Color: color,
Head: head,
Tail: tail,
Version: version,
},
logger,
logOptions,
moveSnake,
).Start(),
)
}
func moveSnake(state *server.GameState, l *server.Logger) server.MoveResponse {
l.Info("Hello from %s", state.You.Name)
//TODO your snake AI here
return server.MoveResponse{
Move: "left",
Shout: "",
}
}
Run with DEBUG
environment variable set to "TRUE"
to allow debug logging and write raw GameState
json to the log file:
DEBUG="TRUE" go run main.go
Check out the Battlesnake documentation for more information on implementing your Battlesnake AI.
Contributions are welcome! If you have any ideas, improvements, or bug fixes, please open an issue or submit a pull request.
This project is licensed under the MIT License.