Documentation ¶
Index ¶
- Constants
- func Flag(x int, y int)
- func NewGame(grid ...Grid) (Minesweeper, Event)
- func Play() error
- func SetDifficulty(difficulty Difficulty) error
- func SetGrid(width int, height int) error
- type Block
- type Difficulty
- type Event
- type ExplodedError
- type GameAlreadyStartedError
- type Grid
- type Minesweeper
- type Node
- type UnspecifiedDifficultyError
- type UnspecifiedGridError
Constants ¶
const ( // Win is the game's event. This will trigger whenever all non-mine // cells are visited. Win eventType // Lose is the game's event. This will trigger whenever a cell // containing the mine is visited. Lose )
Variables ¶
This section is empty.
Functions ¶
func Flag ¶
Flag marks the cell, according to the coordinates supplied in the method argument, as flagged. When a particular cell is flagged, the cell in question will prevent from being handled by the game when the Visit(int, int) method with the same coordinate of the cell in question is called.
func NewGame ¶
func NewGame(grid ...Grid) (Minesweeper, Event)
NewGame creates a separate minesweeper instance. Unlike minesweeper.New, this function creates a non-singleton instance. Functions of this package such as Visit will become the methods of this instance.
func Play ¶
func Play() error
Play allows the game to setup all the mines in place randomly. The placement is non-deterministic since the implementation uses the "crypto/rand" package.
An error will return when this method is called twice or more.
More importantly, Grid size and Difficulty must be specified, otherwise, you will encounter an UnspecifiedGridError and UnspecifiedDifficultyError, respectively.
func SetDifficulty ¶
func SetDifficulty(difficulty Difficulty) error
SetDifficulty sets the difficulty of the game as a basis of the number of mines. An error will return if this method is being called when a game is already being played or better yet, the Play() method has already been called.
func SetGrid ¶
SetGrid sets the board's size. You can't change the board's size once the Play() method has been called, otherwise, a GameAlreadyStartedError will return.
Nothing will return if the setting the board's size is successful.
Whenever the game ends, this instance will be garbage collected. Calling any exported methods when the game ended may result in nil pointer dereference. Creating a new setup of the game is ideal.
Types ¶
type Block ¶
Block stores the information of a particular minesweeper cell. It has the value itself, type of the value in the cell, and the location in the grid. It also reports when a cell is visited or flagged.
func Visit ¶
Visit visits a particular cell according to the xy-coordinates of the argument supplied by this method being called. There are three scenarios that depend to the generated configuration of the game:
A warning number: We may expect a number when visiting a particular cell. The returned []Block will return an array of revealed cells. Accordingly, this type of scenario will always return an array of one Block element.
No mine but no warning number: When encountered, a visited cell will recursively visit all neighboring or adjacent cells because if a visited cells contains no warning number, then there are no neighboring mines per se. Thus, visiting all the neighboring cells are safe to be visited and continues to visit any probed blank cell recursively until no blank cell is left to be probed. The returned []Block will return an array of all probed cells with the first element as the original visited cell.
A mine: When encountered, the game ends revealing all mines by the returned []Block array. It will also return an ExplodedError as a second return value. Furthermore, when event handling is supported, a Lose event will enqueue to the game's even channel buffer. You can remember this buffer when you originally called the NewGame(...Grid) function and store the second returned value as your event listener in a separate goroutine.
There is also one trivial scenario. Visiting a block that is flagged (i.e. originally called the Flag(int, int) method as a result) will have no effect on the state of the game. If a flagged cell with a suspected mine is visited, it will prevent it from being visited and the game would likely treat the called method as if nothing was called at all.
The last Visit() method call with the last non-mine cell will trigger the Win event. The game ends eventually.
type Difficulty ¶
type Difficulty uint8
Difficulty is the state of the game's difficulty. Values of this type are minesweeper.Easy, minesweeper.Medium and minesweeper.Hard
const ( // Easy is the difficulty of the game. The amount of mines present // in the game with this difficulty would result to 10% of the // total area of the board's size. Easy Difficulty // Medium is the difficulty of the game. The amount of mines // present in the game with this difficulty would result to 20% // of the total area of the board's size. Medium // Hard is the difficulty of the game. The amount of mines present // in the game with this difficulty would result to 50% of the // total area of the board's size. Hard )
type Event ¶
type Event chan eventType
Event is a channel type used to receive the game's realtime event. Particular values accepted to this channel are minesweeper.Win and minesweeper.Lose
func New ¶
New creates a new minesweeper environment. Note that this only creates the minesweeper instance without the necessary settings such as the game's difficulty and the game's board size and calling this method will not start the game.
This method returns the minesweeper interface and the event handler. The event handler allows you to setup event listeners in such situation when a game seamlessly triggers a win or lose event. The event handler is a buffered channel that, when used, allows you to setup a particular goroutine to independently listen for these events.
As for this method's argument, this method appears to accept an arbitrary number of trailing arguments of type Grid. It can only, however, handle only one Grid instance and the rest of the arguments will be ignored. Although supplying this Grid is also optional, you may encounter an UnspecifiedGridError panic when calling the Play() method if the Grid is not supplied. You may explicitly supply it by calling the SetGrid(int, int) method.
type ExplodedError ¶
type ExplodedError struct {
// contains filtered or unexported fields
}
ExplodedError is the error type used to handle a situation when a mine is visited
func (ExplodedError) Error ¶
func (Exploded ExplodedError) Error() string
type GameAlreadyStartedError ¶
type GameAlreadyStartedError struct{}
GameAlreadyStartedError is the error type used to handle errors when attempting to restart the game, changing the Grid size when the game is started, or changing the difficulty while the game is started.
func (GameAlreadyStartedError) Error ¶
func (GameAlreadyStarted GameAlreadyStartedError) Error() string
type Grid ¶
type Grid struct{ Width, Height int }
Grid is the game's board size defined by its Grid.Width and Grid.Height
type Minesweeper ¶
type Minesweeper interface { SetGrid(int, int) error SetDifficulty(Difficulty) error Play() error Flag(int, int) Visit(int, int) ([]Block, error) }
Minesweeper is the main point of consumption and manipulation of the Minesweeper's game state. Methods provided by this interface are common use cases to solve a minesweeper game.
Any instance derived by this interface is compatible for type casting to the rendering.Tracker and visited.StoryTeller interfaces.
type Node ¶
type Node uint8
Node is the type of the cell's value. Values of this type are minesweeper.Unknown, minesweeper.Bomb and minesweeper.Number
const ( // Unknown is the type of the value contained in a minesweeper // cell. It has no value which means no mines are neighbored in // the cell. Unknown Node = 1 << iota // Bomb is the type of the value contained in a minesweeper cell. // This is the equivalent of a mine in the game. Bomb // Number is the type of the value contained in a minesweeper // cell. The number indicated the number of mines neighbored in // the cell. Number )
type UnspecifiedDifficultyError ¶
type UnspecifiedDifficultyError struct{}
UnspecifiedDifficultyError is the error type used to handle errors when the Play() method is called but the Difficulty is not set in the game.
func (UnspecifiedDifficultyError) Error ¶
func (UnspecifiedDifficulty UnspecifiedDifficultyError) Error() string
type UnspecifiedGridError ¶
type UnspecifiedGridError struct{}
UnspecifiedGridError is the error type used to handle errors when the Play() method is called but the Grid size is not set in the game.
func (UnspecifiedGridError) Error ¶
func (UnspecifiedGrid UnspecifiedGridError) Error() string