Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

bot source pure

Package bot is everything a bot author needs: what a bot sees each turn, the moves it can make, and helpers for findi...

Overview

Package bot is everything a bot author needs: what a bot sees each turn, the moves it can make, and helpers for finding its way around the board.

A bot is either a type with a Move method (it can live in its own realm and keep memory between turns) or a plain Strategy function.

Constants 2

const Size, Turns

1const (
2	Size  = 9  // the board is Size x Size
3	Turns = 50 // turns per match
4)
source

const Stay, Up, Down, Left, Right, Attack, Defend

1const (
2	Stay   Action = iota // do nothing
3	Up                   // move one square up
4	Down                 // move one square down
5	Left                 // move one square left
6	Right                // move one square right
7	Attack               // steal one coin from an adjacent enemy
8	Defend               // block an attack this turn
9)
source

Functions 6

func Adjacent

1func Adjacent(a, b Pos) bool
source

Adjacent reports whether a and b are one move apart, which is attack range.

func Dist

1func Dist(a, b Pos) int
source

Dist is the number of moves between a and b.

func OnBoard

1func OnBoard(p Pos) bool
source

OnBoard reports whether p is inside the board.

func StepAway

1func StepAway(from, danger Pos) Action
source

StepAway returns a move that takes from one step further from danger, staying on the board. It returns Stay if no move increases the distance.

func StepToward

1func StepToward(from, to Pos) Action
source

StepToward returns the move that takes from one step closer to to, moving along X first. It returns Stay when from == to.

func Apply

1func Apply(p Pos, a Action) Pos
source

Apply returns where p ends up after a. Moves off the edge leave p in place.

Types 5

type Action

ident
1type Action int
source

Action is what a bot does on one turn.

Methods on Action

func String

method on Action
1func (a Action) String() string
source

func Valid

method on Action
1func (a Action) Valid() bool
source

Valid reports whether a is one of the defined actions.

type Bot

interface
1type Bot interface {
2	Move(s State) Action
3}
source

Bot is a bot with a Move method. Implement it on a type in your own realm to keep memory between turns.

type Pos

struct
1type Pos struct{ X, Y int }
source

Pos is a square on the board. X grows to the right, Y grows downwards.

type State

struct
 1type State struct {
 2	Match     int    // match id, so bots with memory can tell matches apart
 3	EnemyBot  string // the opponent's registered name, so bots can keep notes on it
 4	Turn      int    // 0 .. Turns-1
 5	Me        Pos
 6	Enemy     Pos
 7	MyGold    int
 8	EnemyGold int
 9	Coins     []Pos  // coins still on the board
10	MyLast    Action // what this bot did last turn (Stay on turn 0)
11	EnemyLast Action // what the enemy did last turn (Stay on turn 0)
12}
source

State is what a bot sees on its turn. It is a fresh copy every turn, so changing it has no effect on the real match.

Methods on State

func NearestCoin

method on State
1func (s State) NearestCoin() (Pos, bool)
source

NearestCoin returns the closest coin to the bot, if any are left.

func TurnsLeft

method on State
1func (s State) TurnsLeft() int
source

TurnsLeft is how many turns remain after this one.

func Winning

method on State
1func (s State) Winning() bool
source

Winning reports whether the bot has more gold than the enemy.

type Strategy

func
1type Strategy func(s State) Action
source

Strategy is a bot written as a plain function, usually in a /p/ package.

Dependencies

Imports 0

Imports nothing.

/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/bot

Imported by indexer

Source Files 3