rules

package module
v1.0.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2021 License: AGPL-3.0 Imports: 3 Imported by: 14

README

BattlesnakeOfficial/rules

codecov

Battlesnake rules and game logic, implemented as a Go module. This code is used in production at play.battlesnake.com. Issues and contributions welcome!

CLI for Running Battlesnake Games Locally

This repo provides a simple CLI tool to run games locally against your dev environment.

Installation

Download precompiled binaries here:
https://github.com/BattlesnakeOfficial/rules/releases

Install as a Go package. Requires Go 1.15 or higher. [Download]

go get github.com/BattlesnakeOfficial/rules/cli/battlesnake

Compile from source. Also requires Go 1.15 or higher.

git clone git@github.com:BattlesnakeOfficial/rules.git
cd rules
go build -o battlesnake ./cli/battlesnake/main.go
Usage

Example command to run a game locally:

battlesnake play -W 11 -H 11 --name <SNAKE_NAME> --url <SNAKE_URL> -g solo -v

For more details, see the CLI README.

FAQ

How is this different from the old Battlesnake engine?

The old game engine was re-written in early 2020 to handle a higher volume of concurrent games. As part of that rebuild we moved the game logic into a separate Go module that gets compiled into the production engine.

This provides two benefits: it makes it much simpler/easier to build new game modes, and it allows the community to get more involved in game development (without the maintenance overhead of the entire game engine).

Can I run games locally?

Yes! See the included CLI.

The Y-Axis appears to be implemented incorrectly!?!?

This is because the game rules implement an inverted Y-Axis. Older versions of the Battlesnake API operated this way, and several highly competitive Battlesnakes still rely on this behaviour. The current game engine accounts for this by translating the Y-Axis (or not) based on which version of the API each Battlesnake implements. More info here and here.

In the future we might switch this to make the rules easier to develop? But until we drop support for the older API version it doesn't make sense to make that change.

Documentation

Index

Constants

View Source
const (
	MoveUp    = "up"
	MoveDown  = "down"
	MoveRight = "right"
	MoveLeft  = "left"

	BoardSizeSmall  = 7
	BoardSizeMedium = 11
	BoardSizeLarge  = 19

	SnakeMaxHealth = 100
	SnakeStartSize = 3

	// bvanvugt - TODO: Just return formatted strings instead of codes?
	NotEliminated                   = ""
	EliminatedByCollision           = "snake-collision"
	EliminatedBySelfCollision       = "snake-self-collision"
	EliminatedByOutOfHealth         = "out-of-health"
	EliminatedByHeadToHeadCollision = "head-collision"
	EliminatedByOutOfBounds         = "wall-collision"

	// TODO - Error consts
	ErrorTooManySnakes   = RulesetError("too many snakes for fixed start positions")
	ErrorNoRoomForSnake  = RulesetError("not enough space to place snake")
	ErrorNoRoomForFood   = RulesetError("not enough space to place food")
	ErrorNoMoveFound     = RulesetError("move not provided for snake")
	ErrorZeroLengthSnake = RulesetError("snake is length zero")
)
View Source
const EliminatedBySquad = "squad-eliminated"

Variables

This section is empty.

Functions

This section is empty.

Types

type BoardState

type BoardState struct {
	Height int32
	Width  int32
	Food   []Point
	Snakes []Snake
}

type ConstrictorRuleset added in v1.0.17

type ConstrictorRuleset struct {
	StandardRuleset
}

func (*ConstrictorRuleset) CreateInitialBoardState added in v1.0.17

func (r *ConstrictorRuleset) CreateInitialBoardState(width int32, height int32, snakeIDs []string) (*BoardState, error)

func (*ConstrictorRuleset) CreateNextBoardState added in v1.0.17

func (r *ConstrictorRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error)

type Point

type Point struct {
	X int32
	Y int32
}

type RoyaleRuleset added in v1.0.9

type RoyaleRuleset struct {
	StandardRuleset

	Seed int64

	Turn              int32
	ShrinkEveryNTurns int32
	DamagePerTurn     int32

	// Output
	OutOfBounds []Point
}

func (*RoyaleRuleset) CreateNextBoardState added in v1.0.9

func (r *RoyaleRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error)

type Ruleset

type Ruleset interface {
	CreateInitialBoardState(width int32, height int32, snakeIDs []string) (*BoardState, error)
	CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error)
	IsGameOver(state *BoardState) (bool, error)
}

type RulesetError added in v1.0.17

type RulesetError string

func (RulesetError) Error added in v1.0.17

func (err RulesetError) Error() string

type Snake

type Snake struct {
	ID              string
	Body            []Point
	Health          int32
	EliminatedCause string
	EliminatedBy    string
}

type SnakeMove

type SnakeMove struct {
	ID   string
	Move string
}

type SoloRuleset added in v1.0.4

type SoloRuleset struct {
	StandardRuleset
}

func (*SoloRuleset) IsGameOver added in v1.0.4

func (r *SoloRuleset) IsGameOver(b *BoardState) (bool, error)

type SquadRuleset added in v1.0.8

type SquadRuleset struct {
	StandardRuleset

	SquadMap map[string]string

	// These are intentionally designed so that they default to a standard game.
	AllowBodyCollisions bool
	SharedElimination   bool
	SharedHealth        bool
	SharedLength        bool
}

func (*SquadRuleset) CreateNextBoardState added in v1.0.8

func (r *SquadRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error)

func (*SquadRuleset) IsGameOver added in v1.0.8

func (r *SquadRuleset) IsGameOver(b *BoardState) (bool, error)

type StandardRuleset

type StandardRuleset struct {
	FoodSpawnChance int32 // [0, 100]
	MinimumFood     int32
}

func (*StandardRuleset) CreateInitialBoardState

func (r *StandardRuleset) CreateInitialBoardState(width int32, height int32, snakeIDs []string) (*BoardState, error)

func (*StandardRuleset) CreateNextBoardState added in v1.0.4

func (r *StandardRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error)

func (*StandardRuleset) IsGameOver added in v1.0.4

func (r *StandardRuleset) IsGameOver(b *BoardState) (bool, error)

Directories

Path Synopsis
cli
battlesnake command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL