Minesweeper game: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: add comment to fix single quote formatting issue)
Line 3,328: Line 3,328:
Game Module
Game Module


<lang haskell>{-# LANGUAGE TemplateHaskell #-}
Note: Couldn't use the lang tag to format the code. It has issues with single quotes which makes the viewing a mess. Using "pre" for now.
<pre>{-# LANGUAGE TemplateHaskell #-}


module MineSweeper
module MineSweeper
Line 3,336: Line 3,335:
, CellState(..)
, CellState(..)
, Pos
, Pos
,
-- lenses / prisms
-- lenses / prisms
pos
, pos
, coveredLens
, coveredLens
, coveredFlaggedLens
, coveredFlaggedLens
Line 3,344: Line 3,342:
, xCoordLens
, xCoordLens
, yCoordLens
, yCoordLens
,
-- Functions
-- Functions
emptyBoard
, emptyBoard
, groupedByRows
, groupedByRows
, displayCell
, displayCell
Line 3,356: Line 3,353:
, mineBoard
, mineBoard
, totalRows
, totalRows
, totalCols
, totalCols )
)
where
where


Line 3,386: Line 3,382:


-- Re-useable lens.
-- Re-useable lens.
coveredLens :: Traversal' Cell (Bool, Bool)
coveredLens :: Traversal' Cell (Bool, Bool) --'
coveredLens = state . _Covered
coveredLens = state . _Covered


coveredMinedLens, coveredFlaggedLens, unCoveredLens :: Traversal' Cell Bool
coveredMinedLens, coveredFlaggedLens, unCoveredLens :: Traversal' Cell Bool --'
coveredMinedLens = coveredLens . _1
coveredMinedLens = coveredLens . _1
coveredFlaggedLens = coveredLens . _2
coveredFlaggedLens = coveredLens . _2
unCoveredLens = state . _UnCovered
unCoveredLens = state . _UnCovered


xCoordLens, yCoordLens :: Lens' Cell Int
xCoordLens, yCoordLens :: Lens' Cell Int --'
xCoordLens = pos . _1
xCoordLens = pos . _1
yCoordLens = pos . _2
yCoordLens = pos . _2
Line 3,407: Line 3,403:
, _state = Covered False False
, _state = Covered False False
, _adjacentMines = 0
, _adjacentMines = 0
, _cellId = n}) <$> zip [1..] positions
, _cellId = n }) <$> zip [1..] positions
where
where
positions = (,) <$> [1..totalCols] <*> [1..totalRows]
positions = (,) <$> [1..totalCols] <*> [1..totalRows]
Line 3,515: Line 3,511:
where
where
mines n = take n <$> randomCellIds
mines n = take n <$> randomCellIds
minedBoard n =
minedBoard n = (\m ->
fmap (\c -> if c ^. cellId `elem` m then c & state . mined .~ True else c)
(\m -> fmap
(\c -> if c ^. cellId `elem` m then c & state . mined .~ True else c)
board) . filter (\c -> openedCell ^. cellId /= c)
board) . filter (\c -> openedCell ^. cellId /= c)
<$> mines n
<$> mines n
Line 3,529: Line 3,524:
where
where
maxMinedCells = floor $ realToFrac (totalCols * totalRows) * 0.2
maxMinedCells = floor $ realToFrac (totalCols * totalRows) * 0.2
minMinedCells = floor $ realToFrac (totalCols * totalRows) * 0.1</pre>
minMinedCells = floor $ realToFrac (totalCols * totalRows) * 0.1</lang>


Text Play
Text Play