Created
January 18, 2017 23:59
-
-
Save benjamintanweihao/16c46e60e379218e9af2d3fc6b6cf425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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