Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import qualified Data.Set as Set
- import System.Random
- import System.Console.ANSI
- import System.IO (stdin, stdout, hSetEcho, hSetBuffering, BufferMode(..))
- type Coord = (Int, Int)
- data Cell = Mine Bool | Empty Bool | Flag deriving (Show)
- data Level = Level {
- getCursor :: Coord,
- getField :: [[Cell]]
- }
- instance Show Level where
- show level = makeBorder $ unlines $ map (map $ toChr) (getField level)
- where
- toChr :: Cell -> Char
- toChr (Mine True) = 'X'
- -- cheat mode
- toChr (Mine False) = 'X'
- toChr (Empty True) = '.'
- toChr (Flag) = '|'
- toChr _ = ' '
- makeBorder :: String -> String
- makeBorder str = unlines $ horizontal : (map addVertical rows) ++ [horizontal]
- where
- rows = lines str
- horizontal = '+' : take (length $ rows !! 0) (repeat '-') ++ "+"
- addVertical row = '|' : row ++ "|"
- --generateLevel :: Int -> Int -> StdGen -> StdGen -> Int -> Level
- generateLevel width height gen nrMines =
- Level { getCursor = (0, 0), getField = field }
- where
- xs = randoms gen
- mines = uniqueCoords xs nrMines width height
- getCell :: Int -> Int -> Cell
- getCell x y
- | Set.member (x, y) mines = Mine False
- | otherwise = Empty False
- field = [ [ getCell x y | y <- [0..height] ] | x <- [0..width] ]
- uniqueCoords :: [Int] -> Int -> Int -> Int -> Set.Set Coord
- uniqueCoords randoms count limitX limitY =
- recurse Set.empty randoms
- where
- recurse :: Set.Set Coord -> [Int] -> Set.Set Coord
- recurse coords (randomX:randomY:randoms)
- | Set.size coords == count = coords
- | otherwise = recurse (Set.insert (mod randomX limitX, mod randomY limitY) coords) randoms
- main :: IO ()
- main = do
- hSetEcho stdin False
- hSetBuffering stdin NoBuffering
- hSetBuffering stdout NoBuffering
- gen <- getStdGen
- let level = generateLevel 12 18 gen 10
- print level
- setTitle "FOO"
- setCursorPosition 0 0
- t <- getChar
- print t
- --gameLoop level
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement