Skip to content

haguro/go-battlesnake-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-battlesnake-server

Go version License

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.

Installation

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

Usage

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.

Contributing

Contributions are welcome! If you have any ideas, improvements, or bug fixes, please open an issue or submit a pull request.

License

This project is licensed under the MIT License.

About

A Battlesnake HTTP server written in Go

Topics

Resources

License

Stars

Watchers

Forks

Languages