minesweeper

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DifficultyClassic      = iota
	DifficultyBeginner     = iota
	DifficultyIntermediate = iota
	DifficultyExpert       = iota
)
View Source
const (
	DifficultyRowColMin         = 8
	DifficultyRowColMax         = 99
	DifficultyMineMin           = 9
	DifficultyMineMaxPercentage = 0.8
)
View Source
const SaveFileExtension = ".sav"

Variables

This section is empty.

Functions

func NewErrDifficultyDimension added in v0.2.0

func NewErrDifficultyDimension(row, col int) error

func NewErrDifficultyMineCount added in v0.2.0

func NewErrDifficultyMineCount(mines int) error

func OutOfBounds added in v0.2.4

func OutOfBounds(p Pos, d Difficulty) bool

Check if a position is out of bounds on the given difficulty

Types

type Actions added in v0.3.0

type Actions struct {
	Mines   []Pos
	SafePos []Pos
}

type Difficulty

type Difficulty struct {
	Name     string
	Row, Col int
	Mines    int
}

Represent a difficulty setting for the Game

func Difficulties

func Difficulties() []Difficulty

Exposes pre-defined difficulties in a way that does not allow the original array to be modified

func NewCustomDifficulty added in v0.2.0

func NewCustomDifficulty(mines, row, col int) (Difficulty, error)

type ErrDifficultyDimension added in v0.2.0

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

func (*ErrDifficultyDimension) Error added in v0.2.0

func (e *ErrDifficultyDimension) Error() string

type ErrDifficultyMineCount added in v0.2.0

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

func (*ErrDifficultyMineCount) Error added in v0.2.0

func (e *ErrDifficultyMineCount) Error() string

type Field

type Field struct {
	Checked bool
	Content FieldContent
}

Represents a single field in a minefield

type FieldContent

type FieldContent int

Represents the content of a field. Can be a mine, unknown or the number of mines in the neighboring fields

const (
	Mine    FieldContent = -1
	Unknown FieldContent = -2
)

func (FieldContent) String added in v0.2.3

func (fc FieldContent) String() string

Convert FieldContent to string for logging

type Game

type Game interface {
	// Check a given field and recursevly reveal all neighboring fields that should be revield.
	// Returns the resulting new status of the game
	CheckField(p Pos) (*Status, bool)
	// Check if the given position is out of bounds
	OutOfBounds(p Pos) bool
	// Returns the current status of the game. Only contains the knowledge a player should have.
	Status() *Status
	// Check if Game Over
	Lost() bool
	// Check if the game is won
	Won() bool
	// Reset the current game to be played again
	Replay()
	// Check if the game is a replay
	IsReplay() bool
	// Generate a save from the game
	ToSave() (*Save, error)
}

Interface for playing the game

type LocalGame added in v0.2.0

type LocalGame struct {
	Field      [][]Field
	Difficulty Difficulty

	// Keep these 2 exported for testing in other packages
	GameOver bool
	GameWon  bool
	// contains filtered or unexported fields
}

func NewGameSolvable added in v0.5.0

func NewGameSolvable(d Difficulty, p Pos) *LocalGame

Create a new game that is solvable without random guesses.

func NewGameWithSafeArea added in v0.2.4

func NewGameWithSafeArea(d Difficulty, p Pos) *LocalGame

Create a new game with mines seeded randomly in the map, with the exception of a 3x3 area around the given position.

func NewGameWithSafePos

func NewGameWithSafePos(d Difficulty, p Pos) *LocalGame

Create a new game with mines seeded randomly in the map, with the exception of the given position.

func (*LocalGame) CheckField added in v0.2.0

func (g *LocalGame) CheckField(p Pos) (*Status, bool)

Check a given field and recursevly reveal all neighboring fields that should be revield. Returns the resulting new status of the game and a boolean indicating if there where changes.

func (*LocalGame) IsReplay added in v0.2.0

func (g *LocalGame) IsReplay() bool

Check if the game is a replay

func (*LocalGame) Lost added in v0.2.0

func (g *LocalGame) Lost() bool

Check if Game Over

func (*LocalGame) OutOfBounds added in v0.2.0

func (g *LocalGame) OutOfBounds(p Pos) bool

Check if the given position is out of bounds

func (*LocalGame) Replay added in v0.2.0

func (g *LocalGame) Replay()

Reset the current game to be played again

func (*LocalGame) Status added in v0.2.0

func (g *LocalGame) Status() *Status

Returns the current status of the game. Only contains the knowledge a player should have.

func (*LocalGame) ToSave added in v0.2.3

func (g *LocalGame) ToSave() (*Save, error)

Generate a save from the game

func (*LocalGame) UpdateStatus added in v0.3.0

func (g *LocalGame) UpdateStatus() *Status

Update the status from the current state of the game. Returns status for convenience.

func (*LocalGame) Won added in v0.2.0

func (g *LocalGame) Won() bool

Check if the game is won

type Pos

type Pos struct {
	X, Y int
}

Represent a position in the minefield

func CreateMines

func CreateMines(d Difficulty, safe []Pos) []Pos

Randomly create mines for the given difficulty. Does not create a mine on the given positions.

func NewPos added in v0.2.0

func NewPos(x, y int) Pos

Create a new Position from the given coordinates

func RandomPos

func RandomPos(maxX, maxY int) Pos

Returns a random position inside the provided limits

func (Pos) String

func (p Pos) String() string

Returns a string representation of the position

type Save added in v0.2.3

type Save struct {
	// ID is the hash generated from the Data
	ID string `json:"id"`
	// Data contains everything necessary to create a game
	Data saveData `json:"data"`
}

func LoadSave added in v0.2.3

func LoadSave(path string) (*Save, error)

Load a save file from the given path

func NewSave added in v0.2.3

func NewSave(game *LocalGame) (*Save, error)

Create a new save from the given game

func (*Save) Game added in v0.2.3

func (s *Save) Game() *LocalGame

Creates a LocalGame from the save. The game will be considered a replay

func (*Save) Save added in v0.2.3

func (s *Save) Save(path string) error

Write save file to the given path. Needs to have the correct extension.

type Status

type Status struct {
	Field [][]Field
	// contains filtered or unexported fields
}

Status contains the current state of the game known to the player. As such it will always be a copy and needs to be it's own type, despite the overlapping similarities. It does not support any of the functions that Game does. It is safe to write to Status, as it is merely a copy.

func (*Status) GameOver

func (s *Status) GameOver() bool

Returns if the game is lost

func (*Status) GameWon

func (s *Status) GameWon() bool

Returns if the game is won

func (*Status) ObviousMines added in v0.3.0

func (s *Status) ObviousMines() []Pos

Returns the position of all obvious mines

func (*Status) ObviousSafePos added in v0.3.0

func (s *Status) ObviousSafePos() []Pos

Returns the position of all obvious safe positions

Jump to

Keyboard shortcuts

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