Skip to content

Instantly share code, notes, and snippets.

@lpenz lpenz/minesweeper.hs
Created Nov 20, 2017

Embed
What would you like to do?
minesweeper kata in haskell, no output
import qualified Data.Map as Map
import Control.Monad
main :: IO ()
main = processOneField
processOneField :: IO ()
processOneField = do
[n, m] <- (map read . words) <$> getLine :: IO [Int]
when ((n, m) /= (0, 0)) $ doProcessOneField n m
doProcessOneField :: Int -> Int -> IO ()
doProcessOneField n m = do
arena <- replicateM n getLine
let coords = [ (i, j) | i <- [0..n-1], j <- [0..m-1] ]
let mines = filter ( \ c -> (arena !! fst c) !! snd c == '*' ) coords
let zeromap = Map.fromList $ map ( \ c -> (c, 0::Int) ) coords
print $ foldr (f coords) zeromap mines
processOneField
f :: [(Int, Int)] -> (Int, Int) -> Map.Map (Int, Int) Int -> Map.Map (Int, Int) Int
f coords (i, j) r = foldr (Map.adjust (+1)) r neighs
where
neighs = do
ii <- [i-1..i+1]
jj <- [j-1..j+1]
guard $ (ii, jj) `elem` coords
return (ii, jj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.