v1

package
v0.0.0-...-af1c52e Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HazardDamagePerTurn = 15
	MaximumSnakeHealth  = 100
)
View Source
const (
	RulesetStandard    = "standard"
	RulesetSolo        = "solo"
	RulesetRoyale      = "royale"
	RulesetSquad       = "squad"
	RulesetConstrictor = "constrictor"
	RulesetWrapped     = "wrapped"
)

Variables

View Source
var ErrNoPossibleMove = fmt.Errorf("no possible moves")

ErrNoPossibleMove indicates that no legal move is possible from the current position

Functions

This section is empty.

Types

type Battlesnake

type Battlesnake struct {
	ID     string    `json:"id"`
	Name   string    `json:"name"`
	Health int32     `json:"health"`
	Body   CoordList `json:"body"`
	Head   Coord     `json:"head"`
	Length int32     `json:"length"`
	Shout  string    `json:"shout"`
}

func (Battlesnake) IsValid

func (bs Battlesnake) IsValid(b Board, g Game) bool

IsValid determines if the snake is 'valid' on the given board. Valid snakes: * have possible moves * have non-zero health

func (Battlesnake) PossibleMoves

func (bs Battlesnake) PossibleMoves(b Board, g Game) (CoordList, error)

PossibleMoves returns the list of possible coords the Battlesnake could take based on its current position and the provided board. It takes the board bounds and hazards into consideration. An error will the thrown if no moves are possible.

func (Battlesnake) Project

func (bs Battlesnake) Project(loc Coord, board Board) Battlesnake

Project moves the Battlesnake to the given coordinate on the given board

type Board

type Board struct {
	Height  int           `json:"height"`
	Width   int           `json:"width"`
	Food    CoordList     `json:"food"`
	Hazards CoordList     `json:"hazards"`
	Snakes  []Battlesnake `json:"snakes"`
}

type BoardState

type BoardState struct {
	Turn  int         `json:"turn"`
	Board Board       `json:"board"`
	You   Battlesnake `json:"you"`
}

type Coord

type Coord struct {
	X         int       `json:"x"`
	Y         int       `json:"y"`
	Direction Direction `json:"-"`
	Score     float64   `json:"-"`
}

Coord represents a point on the game board, optionally also containing the direction that resulted in the Coord.

func (Coord) DistanceFrom

func (c Coord) DistanceFrom(other Coord) float64

DistanceFrom returns the distance this Coord is from the given Coord

func (Coord) Project

func (c Coord) Project(d Direction) Coord

Project shifts the Coord in the given direction and returns the result. The resulting position is not guaranteed to be valid.

func (Coord) WithinBounds

func (c Coord) WithinBounds(b Board) bool

WithinBounds determines if the Coord is within the boundaries of the given Board. The game board is represented by a standard 2D grid, oriented with (0,0) in the bottom left. The Y-Axis is positive in the up direction, and X-Axis is positive to the right. Coordinates begin at zero, such that a board that is 11x11 will have coordinates ranging from [0, 10].

func (Coord) WrapForBoard

func (c Coord) WrapForBoard(b Board) Coord

WrapForBoard wraps the given coordinate around the edge of the given game board so that it remains valid. Note this is only valid for the 'wrapped' ruleset.

type CoordList

type CoordList []Coord

CoordList is a list of Coords

func (CoordList) AverageDistance

func (cl CoordList) AverageDistance(c Coord) float64

AverageDistance returns the average distance the given coordinate is from the list by calculating the distance from each point and averaging them.

func (CoordList) Contains

func (cl CoordList) Contains(c Coord) bool

Contains determines if the given Coord is present in the list

func (CoordList) Directions

func (cl CoordList) Directions() []Direction

Directions returns a slice of Direction referenced by the list

func (CoordList) Eliminate

func (cl CoordList) Eliminate(candidates CoordList) CoordList

Eliminate returns a subset CoordList by eliminating any Coord present in the candidates list

func (CoordList) First

func (cl CoordList) First(n int) CoordList

Return a subset list made of the first N items

func (CoordList) Rand

func (cl CoordList) Rand() Coord

Rand returns a random coordinate from the list

type Direction

type Direction string
const (
	UP    Direction = "up"
	DOWN  Direction = "down"
	LEFT  Direction = "left"
	RIGHT Direction = "right"
)

type Game

type Game struct {
	ID      string  `json:"id"`
	Ruleset Ruleset `json:"ruleset"`
	Timeout int32   `json:"timeout"`
}

type GameRequest

type GameRequest struct {
	Game  Game        `json:"game"`
	Turn  int         `json:"turn"`
	Board Board       `json:"board"`
	You   Battlesnake `json:"you"`
}

func (GameRequest) ToBoardState

func (gr GameRequest) ToBoardState() BoardState

type Ruleset

type Ruleset struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

type SolveOptions

type SolveOptions struct {
	Lookahead                bool
	ConsiderOpponentNextMove bool
	UseSingleBestOption      bool
	FoodReward               int
	HazardPenalty            int
}
var DefaultSolveOptions SolveOptions = SolveOptions{
	Lookahead:                true,
	ConsiderOpponentNextMove: true,
	UseSingleBestOption:      false,
	FoodReward:               20,
	HazardPenalty:            40,
}

type Solver

type Solver struct {
	Game  Game
	Turn  int
	Board Board
	You   Battlesnake
	// contains filtered or unexported fields
}

func CreateSolver

func CreateSolver(gr GameRequest) *Solver

func (Solver) PickMove

func (s Solver) PickMove(possibleMoves CoordList, opts SolveOptions) (Direction, error)

func (Solver) PossibleMoves

func (s Solver) PossibleMoves(opts SolveOptions) (CoordList, error)

PossibleMoves returns a list of possible moves that could be taken next for the given game state. An error is raised if something prevents that.

func (*Solver) WithLogger

func (s *Solver) WithLogger(l log.Logger) *Solver

Jump to

Keyboard shortcuts

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