hs-pgms-0.1.0.1: Programmer's Mine Sweeper in Haskell

Portabilityportable
Stabilityexperimental
MaintainerBertram Felgenhauer <int-e@gmx.de>

Mine

Contents

Description

This module is part of Haskell PGMS.

It provides types and a monad for implementing and running Minesweeper strategies. It's the core of PGMS.

Synopsis

Minesweeper configurations

data Config Source

Description of a mine sweeper configuration (or difficulty).

Constructors

Config 

Fields

cSize :: Pos

the board size

cMines :: Int

the number of mines placed on the board

Instances

validConfig :: Config -> BoolSource

Check validity of a config.

The width and height must be at least 2, and the number of mines must be between 1 and the number of cells on the board, minus 1.

beginner :: ConfigSource

Default config: 9x9 with 10 mines

intermediate :: ConfigSource

Default config: 16x16 with 40 mines

expert :: ConfigSource

Default config: 30x16 with 99 mines

Minesweeper boards

data Pos Source

A point in 2D space with integer coordinates.

Used to adress cells on a Minesweeper board, and also to describe board sizes.

Constructors

Pos 

Fields

pX :: Int
 
pY :: Int
 

Instances

data Cell Source

A cell on a Minesweeper board.

Constructors

Hidden

a hidden cell

Marked

a marked cell

Exploded

oops, you stepped on a mine here!

Exposed Int

an exposed cell with a count of neighbours

Instances

data Board Source

A complete Minesweeper board, including hidden state.

Constructors

Board 

Fields

bConfig :: Config

board size etc.

bMines :: Array Pos Bool

array indicating the position of the mines

bView :: View

current view

bTodo :: Int

number of mines left to find

Instances

type View = Array Pos CellSource

A view of the Minesweeper board.

neighbours :: Config -> Pos -> [Pos]Source

Find the neighbouring cells of a given cell.

The Config parameter is used to find the boundaries of the board.

Minesweeper strategies

data StrategyM a Source

The monad for implementing Minesweeper strategies.

Instances

data Strategy Source

A strategy with some meta-information.

It's advisable to define your own strategies in terms of defaultStrategy so that future additions to that record don't break your code.

Constructors

Strategy 

Fields

sName :: String

The strategy's name. It should be ASCII and not contain spaces.

sAuthor :: String

The strategy's author.

sDescription :: String

A description of the strategy.

sRun :: StdGen -> StrategyM String

The strategy's implementation.

defaultStrategy :: StrategySource

Default values for Strategy.

 myStrategy :: Strategy
 myStrategy = defaultStrategy {
     sName = "Hiho",
     sRun  = \_ -> return "I don't want to play anymore, see you!"
 }

move :: Pos -> StrategyM IntSource

Reveal a cell. Returns the number of mines in the neighbourhood.

Note: Revealing a cell with a mine beneath will lose the game.

move_ :: Pos -> StrategyM ()Source

Like move, but with no return value.

mark :: Pos -> StrategyM ()Source

Mark a cell.

Note: Marking a cell without a mine beneath will lose the game. This is a deviation from standard Minesweeper.

getView :: StrategyM ViewSource

Get a view of the current board.

getConfig :: StrategyM ConfigSource

Get the current board's config.

Note: the config will never change throughout a game.

traceMine :: String -> StrategyM ()Source

Provide a debug message.

These will be displayed in the status line in the GUI or on the terminal when running the command line version in verbose mode.

Running Minesweeper games

data Result a Source

A game result.

Constructors

Won

The game was won.

Unfinished a

The strategy implementation finished before the game was over.

Lost

The game was lost.

Instances

Eq a => Eq (Result a) 
Show a => Show (Result a) 

playGame :: Config -> StdGen -> StrategyM a -> (Result a, Board)Source

A pure version of playGameP.

data Play a whereSource

UI interface

These are actions for the MonadPrompt monad.

  • Start - A new game just started.
  • Update - A move was made, and the indicated cell changed
  • Trace - The strategy provided a trace message.

Constructors

Start :: Board -> Play () 
Update :: Pos -> Board -> Play () 
Trace :: String -> Board -> Play () 

playGameP :: Config -> StdGen -> StrategyM a -> Prompt Play (Result a, Board)Source

Play a game.

The result is a Prompt action, which is suitable for implementing a UI that displays the game's progress.