Skip to content

Instantly share code, notes, and snippets.

@benjamintanweihao
Created January 18, 2017 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjamintanweihao/16c46e60e379218e9af2d3fc6b6cf425 to your computer and use it in GitHub Desktop.
Save benjamintanweihao/16c46e60e379218e9af2d3fc6b6cf425 to your computer and use it in GitHub Desktop.
module Main where
main :: IO ()
main =
showcells glider
cls :: IO ()
cls =
putStr "\ESC[2J]"
type Pos = (Int,Int)
writeat :: Pos -> String -> IO ()
writeat p xs = do goto p
putStr xs
goto :: Pos -> IO ()
goto (x,y) =
putStr ("\ESC[" ++ show y ++ ";" ++ show x ++ "H")
width :: Int
width = 10
height :: Int
height = 10
type Board = [Pos]
glider :: [Pos]
glider = [(4,2),(2,3),(4,3),(3,4),(4,4)]
showcells :: Board -> IO ()
showcells b = sequence_ [writeat p "0" | p <- b]
isAlive :: Board -> Pos -> Bool
isAlive b p = elem p b
isEmpty :: Board -> Pos -> Bool
isEmpty b p = not (isAlive b p)
neighbs :: Pos -> [Pos]
neighbs (x,y) = map wrap [(x-1,y-1), (x,y-1),
(x+1,y-1), (x-1,y),
(x+1,y), (x-1,1),
(x,y+1), (x+1,y+1)]
wrap :: Pos -> Pos
wrap (x,y) = (((x-1) `mod` width) + 1,
((y-1) `mod` height) + 1)
liveneighs :: Board -> Pos -> Int
liveneighs b = length . filter (isAlive b) . neighbs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment