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.

game source pure

Package game is the match engine: the board, the coins and the rules for one turn. It has no idea where moves come fr...

Overview

Package game is the match engine: the board, the coins and the rules for one turn. It has no idea where moves come from, so the arena and bot authors (for local testing) can both drive it.

A game is stored compactly: the seed plus one byte per move. Everything else (positions, gold, coins, replays) is rebuilt from those, because the rules are deterministic. That keeps on-chain storage, which is paid per byte, to a few hundred bytes per match.

Constants 1

const CoinCount, RespawnEvery, MaxCoins

1const (
2	// CoinCount is how many coins a board starts with.
3	CoinCount = 9
4	// RespawnEvery is how often (in turns) a new mirrored pair of coins
5	// appears, so the board never runs dry.
6	RespawnEvery = 5
7	// MaxCoins caps how many coins can be on the board at once.
8	MaxCoins = 12
9)
source

Functions 2

func PriorityAt

1func PriorityAt(t int) int
source

PriorityAt is the side that wins a contested square on turn t.

func New

1func New(seed int) *Game
source

New makes a board from seed. Coins are placed point-symmetric around the centre, so neither starting corner has an advantage.

Types 3

type Events

struct
1type Events struct {
2	Stole   [2]bool // side took a coin from the enemy with an attack
3	Parried [2]bool // side defended against an attack and took a coin back
4	Spawned int     // coins that appeared at the end of the turn
5}
source

Events is what happened in one turn besides the moves themselves.

type Game

struct
 1type Game struct {
 2	Seed  int
 3	Turn  int
 4	CellA int    // side 0's square, as X*Size+Y
 5	CellB int    // side 1's square
 6	GoldA int    // side 0's gold
 7	GoldB int    // side 1's gold
 8	Coins string // one byte per coin still on the board, X*Size+Y
 9	Moves string // two bytes per turn played: side 0's action, side 1's action
10}
source

Game is one match in progress. Side 0 starts top-left, side 1 bottom-right.

Methods on Game

func At

method on Game
1func (g *Game) At(t int) Snapshot
source

At rebuilds the board after turn t (0 is the starting board) by replaying the recorded moves on a fresh board with the same seed.

func CoinList

method on Game
1func (g *Game) CoinList() []bot.Pos
source

CoinList returns the coins still on the board.

func Gold

method on Game
1func (g *Game) Gold(side int) int
source

Gold returns side's gold.

func Over

method on Game
1func (g *Game) Over() bool
source

Over reports whether all turns have been played.

func Pos

method on Game
1func (g *Game) Pos(side int) bot.Pos
source

Pos returns side's square.

func Step

method on Game
1func (g *Game) Step(a, b bot.Action) (ev Events)
source

Step plays one turn and reports what happened. The rules, in order:

  1. Invalid actions count as Stay.
  2. Fights, only when the two bots are adjacent: - Attack against anything but Defend steals one coin from the enemy. - Attack against Defend is parried: the defender takes one coin from the attacker instead. - Two attacks both land, so they cancel out. A bot with no gold has nothing to lose.
  3. Moves: both bots move at the same time, and never share a square. If both step onto the same square, the bot with priority this turn gets it (PriorityAt: side 0 on even turns, side 1 on odd turns) and the other stays put. A bot can't step into the other's square or swap with it.
  4. Pickup: a bot on a coin takes it.
  5. Every RespawnEvery turns a new mirrored pair of coins appears on free squares, up to MaxCoins on the board.

func Timeline

method on Game
1func (g *Game) Timeline() []Events
source

Timeline replays the match once and returns what happened on each turn: element i is turn i+1.

func View

method on Game
1func (g *Game) View(matchID, side int) bot.State
source

View is what side sees: a fresh copy of the board from its point of view.

func Winner

method on Game
1func (g *Game) Winner() int
source

Winner returns 0 or 1 for the side with more gold, or -1 for a draw.

type Snapshot

struct
1type Snapshot struct {
2	Turn   int
3	Pos    [2]bot.Pos
4	Gold   [2]int
5	Coins  []bot.Pos
6	Act    [2]bot.Action // actions of this turn (zero at turn 0)
7	Events Events        // what happened this turn
8}
source

Snapshot is the board after some turn, for replays.

Dependencies

Imports 2

/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/game

Imported by indexer

Source Files 3