Documentation
¶
Overview ¶
Package game implements Minesweeper rules as a deep, TUI-independent module.
Invariants:
- Before the first Reveal, mines are not placed on the board.
- Only hidden cells carry a mark; revealing a cell clears it.
- A flag blocks revealing; a question mark never changes what an action does.
- After Won or Lost, all mutating operations are no-ops.
Index ¶
- Constants
- func DailyDate(t time.Time) string
- type ActionResult
- type Board
- func (b *Board) CanChord(c Coord) bool
- func (b *Board) CanMark(c Coord) bool
- func (b *Board) CanReveal(c Coord) bool
- func (b *Board) CellView(c Coord) CellView
- func (b *Board) Chord(c Coord) ActionResult
- func (b *Board) CycleMark(c Coord, questions bool) ActionResult
- func (b *Board) Difficulty() Difficulty
- func (b *Board) ElapsedReady() bool
- func (b *Board) FlagCount() int
- func (b *Board) Height() int
- func (b *Board) MarkAt(c Coord) Mark
- func (b *Board) MineCount() int
- func (b *Board) NoGuess() bool
- func (b *Board) RemainingMines() int
- func (b *Board) Reveal(c Coord) ActionResult
- func (b *Board) Seed() Seed
- func (b *Board) SetMark(c Coord, mark Mark) ActionResult
- func (b *Board) Status() Status
- func (b *Board) Width() int
- type Cell
- type CellState
- type CellView
- type Coord
- type Difficulty
- type Mark
- type Move
- type MoveKind
- type Preset
- type Replay
- type Seed
- type Status
Constants ¶
const DailyKeyword = "daily"
DailyKeyword selects the daily challenge seed on the command line.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ActionResult ¶
ActionResult describes the outcome of a board action.
type Board ¶
type Board struct {
// contains filtered or unexported fields
}
Board is the deep module encapsulating all game rules.
func NewBoard ¶
func NewBoard(d Difficulty, seed Seed) *Board
NewBoard creates an empty board; mines are placed on the first Reveal so that the opening move is always safe. The same difficulty and seed always produce the same layout.
func NewNoGuessBoard ¶ added in v0.3.0
func NewNoGuessBoard(d Difficulty, seed Seed) *Board
NewNoGuessBoard is NewBoard for a board that can be cleared by deduction alone, so the player is never asked to flip a coin. Laying one out means searching, which costs time on the opening move and is not always possible; when the search comes up empty the board falls back to an ordinary layout and NoGuess reports false.
func (*Board) Chord ¶
func (b *Board) Chord(c Coord) ActionResult
Chord reveals adjacent hidden cells when flag count matches the number.
func (*Board) CycleMark ¶ added in v0.3.0
func (b *Board) CycleMark(c Coord, questions bool) ActionResult
CycleMark advances the mark on a hidden cell. With questions enabled the cycle is none, flag, question, none; otherwise it is a plain flag toggle. Keeping the choice in the caller lets the preference live with the UI while the board owns what each mark means.
func (*Board) Difficulty ¶
func (b *Board) Difficulty() Difficulty
Difficulty returns the board difficulty.
func (*Board) ElapsedReady ¶
ElapsedReady reports whether the timer should run (first reveal done).
func (*Board) NoGuess ¶ added in v0.3.0
NoGuess reports whether this board is known to be solvable without guessing. It is only meaningful once mines are placed, which happens on the first reveal.
func (*Board) RemainingMines ¶
RemainingMines returns mine count minus flags (may be negative).
func (*Board) Reveal ¶
func (b *Board) Reveal(c Coord) ActionResult
Reveal opens a cell and may trigger flood fill.
type CellView ¶
type CellView struct {
State CellState
Adjacent uint8
ShowMine bool // true when game is lost — reveal all mines
}
CellView is the UI-facing projection of a cell (information hiding).
type Coord ¶
type Coord struct {
X, Y int
}
Coord is a cell position on the board (0-indexed).
type Difficulty ¶
Difficulty describes board dimensions and mine count.
func PresetDifficulty ¶
func PresetDifficulty(p Preset) Difficulty
PresetDifficulty returns the standard settings for a preset.
func (Difficulty) Key ¶
func (d Difficulty) Key() string
Key returns a stable string for high-score storage.
func (Difficulty) Validate ¶
func (d Difficulty) Validate() error
Validate checks that the difficulty is playable with first-click safety.
type Mark ¶ added in v0.3.0
type Mark uint8
Mark is the note a player has put on a hidden cell. A cell carries at most one, which is why this is an enum rather than a pair of booleans.
const ( // MarkNone is an unmarked cell. MarkNone Mark = iota // MarkFlag asserts a mine. Flagged cells cannot be revealed and count // towards the remaining-mine display and chording. MarkFlag // MarkQuestion records a suspicion. It is a note to the player and nothing // more: revealing, flood fill, and chording treat it as plain hidden. MarkQuestion )
type Move ¶ added in v0.4.0
type Move struct {
Kind MoveKind `json:"kind"`
Coord
// TargetMark is the mark left on the cell after a MoveMark. Older replays
// omit this and fall back to a single flag cycle.
TargetMark *Mark `json:"target_mark,omitempty"`
}
Move is one recorded action in order.
type Preset ¶
type Preset int
Preset identifies a built-in difficulty level.
func PresetFromString ¶
PresetFromString parses a preset name.
type Replay ¶ added in v0.4.0
type Replay struct {
ID string `json:"id"`
Seed Seed `json:"seed"`
Difficulty Difficulty `json:"difficulty"`
NoGuess bool `json:"no_guess"`
Moves []Move `json:"moves"`
Won bool `json:"won"`
Seconds int `json:"seconds"`
PlayedAt time.Time `json:"played_at"`
}
Replay is a finished game that can be watched again. The seed pins the mine layout; the moves pin what the player did. NoGuess records which generator built the board: classic and no-guess draw differently from the same seed. Older JSON omits the field and loads as classic.
type Seed ¶
type Seed uint32
Seed identifies a reproducible board layout. It is deliberately 32-bit so that seeds stay short enough to share verbally or in a chat message.
func DailySeed ¶
DailySeed derives the seed for the daily challenge on t's UTC date. Every player who starts the challenge on the same date gets the same board.