minesweeper

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: Apache-2.0 Imports: 5 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
)

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

Types

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
)

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
	// Recursive function to reveal all neighbouring fields that can be safely reveald.
	// Stops when a field has not exactly zero neighbouring mines
	RevealField(p Pos)
	// 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
}

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 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

Check a given field and recursevly reveal all neighboring fields that should be revield. Returns the resulting new status of the game

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) RevealField added in v0.2.0

func (g *LocalGame) RevealField(p Pos)

Recursive function to reveal all neighbouring fields that can be safely reveald. Stops when a field has not exactly zero neighbouring mines

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) 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, p Pos) []Pos

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

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 Status

type Status struct {
	Field    [][]Field
	GameOver bool
	GameWon  bool
}

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.

Jump to

Keyboard shortcuts

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