package undo
import "github.com/nsgo-dev/game/pkg/undo"Overview
Package undo records the reversible moves of puzzle and turn-based games, with unlimited undo and redo.
A History owns a game's state and a list of moves. Do applies a move and records it, Undo reverts the last applied move, and Redo applies the last undone one again; a new move after an undo discards the moves that could have been redone. Every move is kept until Clear, so a player can undo back to the start of a level however long they played.
Each Move both applies and reverts its change, so undoing every move returns the state to exactly where it started as long as each Revert is the exact inverse of its Apply. A move may record what its Apply changed (the crate it pushed, the piece it captured) in its own fields and read it back in Revert.
A History whose moves are one concrete type can be saved as a versioned YAML Log and restored from it, which replays the applied moves on the starting state and keeps the undone ones for Redo.
Nothing in undo reads the clock, spawns goroutines or keeps package-level state.
Index
- Constants
- func EncodeLog[M any](log Log[M]) ([]byte, error)
- type Func
- type Group
- type History
- func New[S any, M Move[S]](state S) *History[S, M]
- func Restore[S any, M Move[S]](state S, log Log[M]) (*History[S, M], error)
- func (h *History[S, M]) Applied() int
- func (h *History[S, M]) CanRedo() bool
- func (h *History[S, M]) CanUndo() bool
- func (h *History[S, M]) Clear()
- func (h *History[S, M]) Do(m M)
- func (h *History[S, M]) Len() int
- func (h *History[S, M]) Log() Log[M]
- func (h *History[S, M]) Moves() []M
- func (h *History[S, M]) Redo() bool
- func (h *History[S, M]) Seek(n int)
- func (h *History[S, M]) State() S
- func (h *History[S, M]) Undo() bool
- type Log
- type Move
Constants
const LogVersion = 1LogVersion is the schema version of the Log YAML format this package reads and writes.
Functions
func EncodeLog[M any](log Log[M]) ([]byte, error)EncodeLog validates log and writes it as deterministic 2-space YAML.
Types
type Func[S any] struct {
Do func(state S) // makes the change
Undo func(state S) // takes the change back
}Func is a Move made of two functions, for moves that close over what they change.
type Group[S any] []Move[S]Group is a Move made of several moves that undo and redo as one, such as a turn that moves a piece and captures another. It applies its moves in order and reverts them in reverse order.
func (g Group[S]) Revert(state S)Revert reverts each move of g in reverse order.
type History[S any, M Move[S]] struct {
// OnChange, when set, is called after each call that applies or reverts moves or forgets them, so a game can update its undo and redo buttons.
OnChange func()
// contains filtered or unexported fields
}History holds a game's state and the moves made on it, with unlimited linear undo and redo.
M is the type of the moves: Move[S] for moves of several kinds, or one concrete move type, which lets the history be saved as a Log. A History is not safe for concurrent use.
func New[S any, M Move[S]](state S) *History[S, M]New returns an empty History over state, the game's starting state.
func Restore[S any, M Move[S]](state S, log Log[M]) (*History[S, M], error)Restore returns a History over state, the starting state the log was recorded from (the state given to New, or the state at the history's last Clear), with the log's moves: it applies the first log.Applied moves to state in order and keeps the rest for Redo. OnChange is not called.
func (h *History[S, M]) Applied() intApplied returns the number of moves applied, which is the index of the move Redo would apply.
func (h *History[S, M]) CanRedo() boolCanRedo reports whether Redo has a move to apply again.
func (h *History[S, M]) CanUndo() boolCanUndo reports whether Undo has a move to revert.
func (h *History[S, M]) Clear()Clear forgets every move and keeps the state as it is, which becomes the new starting state. A Log taken after Clear holds only the later moves, so a game that saves it must also save the state at the Clear and pass that to Restore.
func (h *History[S, M]) Do(m M)Do applies m to the state and records it as the latest move, discarding any moves that were undone and not redone. It panics, before applying anything, if m is nil or is a Group holding a nil move; a nil or empty Group is a valid move that changes nothing.
func (h *History[S, M]) Len() intLen returns the number of moves recorded, applied and undone.
func (h *History[S, M]) Log() Log[M]Log returns the history's moves as a Log of the current version. The slice is a copy, but moves that are pointers are shared with the history.
func (h *History[S, M]) Moves() []MMoves returns a copy of the recorded moves in the order they were made; the first Applied of them are applied.
func (h *History[S, M]) Redo() boolRedo applies the latest undone move again and reports whether there was one.
func (h *History[S, M]) Seek(n int)Seek undoes or redoes moves until n moves are applied: Seek(0) returns to the starting state and Seek(h.Len()) redoes every move. It panics if n is outside [0, h.Len()].
func (h *History[S, M]) State() SState returns the state the history's moves change.
func (h *History[S, M]) Undo() boolUndo reverts the latest applied move and reports whether there was one.
type Log[M any] struct {
Version int `yaml:"version"` // LogVersion
Applied int `yaml:"applied"` // moves applied, in [0, len(Moves)]
Moves []M `yaml:"moves"` // every recorded move, applied first
}Log is the saved form of a History: its moves in order and how many of them are applied. It does not hold the starting state, which is the state given to New or, after a Clear, the state at that Clear; a game saves that state with the log and passes it to Restore. It is written as YAML with its fields in the order below, all three required:
version: 1 applied: 2 moves: - dir: left - dir: up - dir: up
M must be a concrete type the YAML package encodes and decodes, keeping what a move records during Apply in unexported fields or fields tagged `yaml:"-"`, so a Log holds only what a player chose.
func DecodeLog[M any](data []byte) (Log[M], error)DecodeLog reads a YAML Log and validates it. Missing version, applied or moves keys, a null moves list, unknown fields, in the log or in its moves, and trailing YAML documents are refused.
func (l Log[M]) Validate() errorValidate reports whether l has the current version and an applied count within its moves.
type Move[S any] interface {
Apply(state S)
Revert(state S)
}Move is one reversible change to a game's state S, which is usually a pointer to the game's board or world.
Apply makes the change and Revert takes it back; Revert(s) after Apply(s) must leave s exactly as it was before Apply. Apply may store what it needs to revert in the move itself (a move with a pointer receiver), since a History applies a move again on Redo only to the same state it first saw. Neither method may call the History that holds the move.