minesweeper

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 10 Imported by: 0

README

Minesweeper-Go

A PRNG deterministic game of minesweeper written in Go.

You can use it as a module for your project or with the CLI interfaces.

Documentation

Overview

Package minesweeper is a deterministic implementation of the classic game.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSize       = errors.New("size must be a positive number")
	ErrInvalidTotalMines = errors.New("total mines must be a positive number")
	ErrOutOfBounds       = errors.New("coordinates out of bounds")
	ErrInvalidAction     = errors.New("invalid action")
	ErrInvalidBoard      = errors.New("board must be greater than 3x3 and place (NxM)/2 mines")
)

Functions

This section is empty.

Types

type Action

type Action struct {
	Kind ActionKind
	X, Y int
}

func NewAction

func NewAction(kind ActionKind, x, y int) Action

func ParseAction

func ParseAction(s string) (*Action, error)

func ParseActions

func ParseActions(s string) ([]*Action, error)

func (Action) String

func (a Action) String() string

type ActionKind

type ActionKind byte
const (
	ActionToggleFlag ActionKind = 'F'
	ActionRevealCell ActionKind = 'C'
)

func (ActionKind) String

func (a ActionKind) String() string

type Board

type Board struct {
	// contains filtered or unexported fields
}

func NewBoard

func NewBoard(width, height int) *Board

func (*Board) AdjacentCells

func (b *Board) AdjacentCells(x, y int) iter.Seq[*Cell]

func (*Board) Cell

func (b *Board) Cell(x, y int) *Cell

func (*Board) Cells

func (b *Board) Cells() iter.Seq[*Cell]

func (*Board) Height

func (b *Board) Height() int

func (*Board) InBounds

func (b *Board) InBounds(x, y int) bool

func (*Board) Width

func (b *Board) Width() int

type Cell

type Cell struct {
	X, Y          int
	AdjacentMines uint8
	IsMine        bool
	IsRevealed    bool
	IsFlagged     bool
}

func NewCell

func NewCell(x, y int) *Cell

func (*Cell) IsEmpty

func (c *Cell) IsEmpty() bool

func (*Cell) Reveal

func (c *Cell) Reveal()

func (*Cell) ToggleFlag

func (c *Cell) ToggleFlag()

type CellSymbol

type CellSymbol = byte
const (
	SymbolUnrevealed CellSymbol = 'o'
	SymbolEmpty      CellSymbol = '_'
	SymbolFlag       CellSymbol = '!'
	SymbolMine       CellSymbol = 'x'
	SymbolBreakln    CellSymbol = '\n'
)

type CellView

type CellView struct {
	X        int  `json:"x"`
	Y        int  `json:"y"`
	Revealed bool `json:"revealed"`
	Flagged  bool `json:"flagged"`

	// -1 (Game Over)
	// 0-8 revealed adjacents value
	// 0 unrevealed cell
	Value int `json:"value"`
}

func (CellView) MarshalJSON

func (c CellView) MarshalJSON() ([]byte, error)

func (*CellView) UnmarshalJSON

func (c *CellView) UnmarshalJSON(data []byte) error

type Game

type Game struct {
	// contains filtered or unexported fields
}

func NewGame

func NewGame(width, height, minesTotal int) (*Game, error)

func NewGameSeeded

func NewGameSeeded(seed uint64, width, height, minesTotal int) (*Game, error)

func (*Game) Board

func (g *Game) Board() *Board

func (*Game) Bytes

func (g *Game) Bytes() []byte

func (*Game) FlagsPlaced

func (g *Game) FlagsPlaced() int

func (*Game) Handle

func (g *Game) Handle(act *Action) error

func (*Game) Mines

func (g *Game) Mines() int

func (*Game) Rand

func (g *Game) Rand() *rand.Rand

func (*Game) Render

func (g *Game) Render(w io.Writer) (err error)

func (*Game) RevealCell

func (g *Game) RevealCell(startX, startY int) error

func (*Game) Seed

func (g *Game) Seed() uint64

func (*Game) Start

func (g *Game) Start(x, y int) error

func (*Game) Status

func (g *Game) Status() GameStatus

func (*Game) String

func (g *Game) String() string

func (*Game) ToJSON

func (g *Game) ToJSON() ([]byte, error)

func (*Game) ToggleFlag

func (g *Game) ToggleFlag(x, y int) error

func (*Game) View

func (g *Game) View() GameView

type GameStatus

type GameStatus uint8
const (
	StatusUnknown GameStatus = iota
	StatusPlaying
	StatusLost
	StatusWon
)

func (GameStatus) String

func (gs GameStatus) String() string

type GameView

type GameView struct {
	Width     int          `json:"width"`
	Height    int          `json:"height"`
	MinesLeft int          `json:"mines_left"`
	Status    string       `json:"status"` // "playing", "won", "lost"
	Board     [][]CellView `json:"board"`
}

func (GameView) MarshalJSON

func (g GameView) MarshalJSON() ([]byte, error)

func (*GameView) UnmarshalJSON

func (g *GameView) UnmarshalJSON(data []byte) error

Directories

Path Synopsis
cmd
minesweeper-cli command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL