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...
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.
1
2
3
type Events
structEvents 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}Game is one match in progress. Side 0 starts top-left, side 1 bottom-right.
Methods on Game
func At
method on GameAt 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 GameCoinList returns the coins still on the board.
func Gold
method on GameGold returns side's gold.
func Over
method on GameOver reports whether all turns have been played.
func Pos
method on GamePos returns side's square.
func Step
method on GameStep plays one turn and reports what happened. The rules, in order:
- Invalid actions count as Stay.
- 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.
- 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.
- Pickup: a bot on a coin takes it.
- Every RespawnEvery turns a new mirrored pair of coins appears on free squares, up to MaxCoins on the board.
func Timeline
method on GameTimeline replays the match once and returns what happened on each turn: element i is turn i+1.
func View
method on GameView is what side sees: a fresh copy of the board from its point of view.
func Winner
method on GameWinner returns 0 or 1 for the side with more gold, or -1 for a draw.
type Snapshot
structSnapshot is the board after some turn, for replays.