main.hs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. -- Mine Sweeper Lab
  2. -- Import section
  3. import Data.Char
  4. import Data.Set ( Set )
  5. import qualified Data.Set as S
  6. -- Types
  7. data Cell = Covered Int Bool Bool -- number of bombs around ; has bomb ; has flag
  8. | Uncovered Int -- number of bombs around
  9. | Selected
  10. data Grid = Grid [[Cell]]
  11. data StdGen = SG -- TODO : replace
  12. -- Methods
  13. instance Show Cell where
  14. show ( Covered _ _ hasFlag ) = if hasFlag then "[F]" else "[ ]"
  15. show ( Uncovered n ) = ['(', intToDigit n, ')']
  16. show Selected = ">x<"
  17. instance Show Grid where
  18. show (Grid a) = unlines $ map (unwords . map show) a
  19. -- Functions
  20. randSet::Int -> StdGen -> StdGen -> Int -> Int -> Set (Int, Int)
  21. randSet n sg1 sg2 h w =
  22. let byl = randomRs (0, h - 1) sg1 in
  23. let bxl = randomRs (0, w - 1) sg2 in
  24. let bl = zip byl bxl in
  25. let biggerSets = scanl (flip S.insert) S.empty bl in
  26. head (dropWhile (\s -> S.size s < n) biggerSets)
  27. grid::Int -> Int -> Set (Int, Int) -> Grid
  28. grid h w s = Grid [ [Covered 0 (S.member (y, x) s) False | x <- [0..w-1] ] | y <- [0..h-1] ]
  29. randomRs (x, y) sg = [x + mod k (y - x) | k <- [1..y] ] -- TODO : replace
  30. -- Testing data
  31. tinyGrid = Grid [
  32. [Covered 2 True False, Uncovered 2, Covered 2 True False],
  33. [Selected, Uncovered 0, Covered 2 True True]
  34. ]