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.gno

3.91 Kb · 144 lines
  1// Package bot is everything a bot author needs: what a bot sees each turn,
  2// the moves it can make, and helpers for finding its way around the board.
  3//
  4// A bot is either a type with a Move method (it can live in its own realm and
  5// keep memory between turns) or a plain Strategy function.
  6package bot
  7
  8const (
  9	Size  = 9  // the board is Size x Size
 10	Turns = 50 // turns per match
 11)
 12
 13// Pos is a square on the board. X grows to the right, Y grows downwards.
 14type Pos struct{ X, Y int }
 15
 16// Action is what a bot does on one turn.
 17type Action int
 18
 19const (
 20	Stay   Action = iota // do nothing
 21	Up                   // move one square up
 22	Down                 // move one square down
 23	Left                 // move one square left
 24	Right                // move one square right
 25	Attack               // steal one coin from an adjacent enemy
 26	Defend               // block an attack this turn
 27)
 28
 29var actionNames = []string{"stay", "up", "down", "left", "right", "attack", "defend"}
 30
 31// Valid reports whether a is one of the defined actions.
 32func (a Action) Valid() bool { return a >= Stay && a <= Defend }
 33
 34func (a Action) String() string {
 35	if !a.Valid() {
 36		return "invalid"
 37	}
 38	return actionNames[a]
 39}
 40
 41// State is what a bot sees on its turn. It is a fresh copy every turn, so
 42// changing it has no effect on the real match.
 43type State struct {
 44	Match     int    // match id, so bots with memory can tell matches apart
 45	EnemyBot  string // the opponent's registered name, so bots can keep notes on it
 46	Turn      int // 0 .. Turns-1
 47	Me        Pos
 48	Enemy     Pos
 49	MyGold    int
 50	EnemyGold int
 51	Coins     []Pos  // coins still on the board
 52	MyLast    Action // what this bot did last turn (Stay on turn 0)
 53	EnemyLast Action // what the enemy did last turn (Stay on turn 0)
 54}
 55
 56// Bot is a bot with a Move method. Implement it on a type in your own realm
 57// to keep memory between turns.
 58type Bot interface {
 59	Move(s State) Action
 60}
 61
 62// Strategy is a bot written as a plain function, usually in a /p/ package.
 63type Strategy func(s State) Action
 64
 65func abs(n int) int {
 66	if n < 0 {
 67		return -n
 68	}
 69	return n
 70}
 71
 72// Dist is the number of moves between a and b.
 73func Dist(a, b Pos) int { return abs(a.X-b.X) + abs(a.Y-b.Y) }
 74
 75// Adjacent reports whether a and b are one move apart, which is attack range.
 76func Adjacent(a, b Pos) bool { return Dist(a, b) == 1 }
 77
 78// OnBoard reports whether p is inside the board.
 79func OnBoard(p Pos) bool { return p.X >= 0 && p.Y >= 0 && p.X < Size && p.Y < Size }
 80
 81// NearestCoin returns the closest coin to the bot, if any are left.
 82func (s State) NearestCoin() (Pos, bool) {
 83	best, found := Pos{}, false
 84	for _, c := range s.Coins {
 85		if !found || Dist(s.Me, c) < Dist(s.Me, best) {
 86			best, found = c, true
 87		}
 88	}
 89	return best, found
 90}
 91
 92// Winning reports whether the bot has more gold than the enemy.
 93func (s State) Winning() bool { return s.MyGold > s.EnemyGold }
 94
 95// TurnsLeft is how many turns remain after this one.
 96func (s State) TurnsLeft() int { return Turns - 1 - s.Turn }
 97
 98// StepToward returns the move that takes from one step closer to to,
 99// moving along X first. It returns Stay when from == to.
100func StepToward(from, to Pos) Action {
101	switch {
102	case to.X > from.X:
103		return Right
104	case to.X < from.X:
105		return Left
106	case to.Y > from.Y:
107		return Down
108	case to.Y < from.Y:
109		return Up
110	}
111	return Stay
112}
113
114// StepAway returns a move that takes from one step further from danger,
115// staying on the board. It returns Stay if no move increases the distance.
116func StepAway(from, danger Pos) Action {
117	best, bestDist := Stay, Dist(from, danger)
118	for _, a := range []Action{Up, Down, Left, Right} {
119		p := Apply(from, a)
120		if d := Dist(p, danger); d > bestDist {
121			best, bestDist = a, d
122		}
123	}
124	return best
125}
126
127// Apply returns where p ends up after a. Moves off the edge leave p in place.
128func Apply(p Pos, a Action) Pos {
129	n := p
130	switch a {
131	case Up:
132		n.Y--
133	case Down:
134		n.Y++
135	case Left:
136		n.X--
137	case Right:
138		n.X++
139	}
140	if !OnBoard(n) {
141		return p
142	}
143	return n
144}