1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- -- Mine Sweeper Lab
- -- Import section
- import Data.Char
- import Data.Set ( Set )
- import qualified Data.Set as S
- -- Types
- data Cell = Covered Int Bool Bool -- number of bombs around ; has bomb ; has flag
- | Uncovered Int -- number of bombs around
- | Selected
- data Grid = Grid [[Cell]]
- data StdGen = SG -- TODO : replace
- -- Methods
- instance Show Cell where
- show ( Covered _ _ hasFlag ) = if hasFlag then "[F]" else "[ ]"
- show ( Uncovered n ) = ['(', intToDigit n, ')']
- show Selected = ">x<"
- instance Show Grid where
- show (Grid a) = unlines $ map (unwords . map show) a
- -- Functions
- randSet::Int -> StdGen -> StdGen -> Int -> Int -> Set (Int, Int)
- randSet n sg1 sg2 h w =
- let byl = randomRs (0, h - 1) sg1 in
- let bxl = randomRs (0, w - 1) sg2 in
- let bl = zip byl bxl in
- let biggerSets = scanl (flip S.insert) S.empty bl in
- head (dropWhile (\s -> S.size s < n) biggerSets)
- grid::Int -> Int -> Set (Int, Int) -> Grid
- grid h w s = Grid [ [Covered 0 (S.member (y, x) s) False | x <- [0..w-1] ] | y <- [0..h-1] ]
- randomRs (x, y) sg = [x + mod k (y - x) | k <- [1..y] ] -- TODO : replace
- -- Testing data
- tinyGrid = Grid [
- [Covered 2 True False, Uncovered 2, Covered 2 True False],
- [Selected, Uncovered 0, Covered 2 True True]
- ]
|