1
1
module GameOfLife where
2
2
3
3
import Control.Concurrent (threadDelay )
4
- import System.Process (system )
5
4
import GHC.IO.Exception (ExitCode )
5
+ import System.Process (system )
6
6
7
7
height :: Int
8
8
height = 30
@@ -21,43 +21,53 @@ type Grid = [Pos]
21
21
-- PRE-BUILT GRIDS --
22
22
23
23
gliderCol :: Grid
24
- gliderCol = [(0 ,2 ),(1 ,0 ),(1 ,2 ),(2 ,1 ),(2 ,2 ),(3 ,11 ),(4 ,9 ),(4 ,10 ),(5 ,10 ),(5 ,11 )]
24
+ gliderCol = [(0 ,2 ),(1 ,0 ),(1 ,2 ),(2 ,1 ),(2 ,2 ),(3 ,11 ),(4 ,9 ),(4 ,10 ),(5 ,10 ),(5 ,11 )]
25
25
26
26
glider :: Grid
27
- glider = [(0 ,2 ),(1 ,0 ),(1 ,2 ),(2 ,1 ),(2 ,2 )]
27
+ glider = [(0 ,2 ),(1 ,0 ),(1 ,2 ),(2 ,1 ),(2 ,2 )]
28
28
29
29
ship :: Grid
30
- ship = [(0 ,0 ),(0 ,3 ),(1 ,4 ),(2 ,0 ),(2 ,4 ),(3 ,1 ),(3 ,2 ),(3 ,3 ),(3 ,4 )]
30
+ ship = [(0 ,0 ),(0 ,3 ),(1 ,4 ),(2 ,0 ),(2 ,4 ),(3 ,1 ),(3 ,2 ),(3 ,3 ),(3 ,4 )]
31
31
32
32
-- PRE-BUILT GRIDS --
33
33
34
34
showPos :: Pos -> Grid -> Char
35
- showPos pos grid | pos `elem` grid = ' 0'
36
- | otherwise = ' '
35
+ showPos pos grid
36
+ | pos `elem` grid = ' 0'
37
+ | otherwise = ' '
37
38
38
39
rowLines :: String -> String
39
- rowLines " " = " "
40
+ rowLines " " = " "
40
41
rowLines gridStr = take width gridStr ++ " \n " ++ rowLines (drop width gridStr)
41
42
42
43
showGrid :: Grid -> String
43
- showGrid grid = rowLines [showPos (r,c) grid | r <- [0 .. (height - 1 )],
44
- c <- [0 .. (width - 1 )]]
44
+ showGrid grid =
45
+ rowLines
46
+ [ showPos (r, c) grid | r <- [0 .. (height - 1 )], c <- [0 .. (width - 1 )]
47
+ ]
45
48
46
49
neighbors :: Pos -> [Pos ]
47
- neighbors (r,c) = [(r- 1 ,c- 1 ),(r- 1 ,c),(r- 1 ,c+ 1 ),
48
- (r,c- 1 ) ,(r,c+ 1 ),
49
- (r+ 1 ,c- 1 ),(r+ 1 ,c),(r+ 1 ,c+ 1 )]
50
+ neighbors (r, c) =
51
+ [ (r - 1 , c - 1 ),
52
+ (r - 1 , c),
53
+ (r - 1 , c + 1 ),
54
+ (r, c - 1 ),
55
+ (r, c + 1 ),
56
+ (r + 1 , c - 1 ),
57
+ (r + 1 , c),
58
+ (r + 1 , c + 1 )
59
+ ]
50
60
51
61
livingNeighs :: Pos -> Grid -> Int
52
62
livingNeighs pos grid = length $ filter (`elem` grid) (neighbors pos)
53
63
54
64
survivors :: Grid -> Grid
55
- survivors grid = filter (\ x -> livingNeighs x grid `elem` [2 ,3 ]) grid
65
+ survivors grid = filter (\ x -> livingNeighs x grid `elem` [2 ,3 ]) grid
56
66
57
67
births :: Grid -> Grid
58
- births grid = [(r,c) | r <- [ 0 .. (height - 1 )],
59
- c <- [0 .. (width - 1 )],
60
- livingNeighs (r,c) grid == 3 && (r,c) `notElem` grid ]
68
+ births grid =
69
+ [ (r, c) | r <- [ 0 .. (height - 1 )], c <- [0 .. (width - 1 )], livingNeighs (r, c) grid == 3 && (r, c) `notElem` grid
70
+ ]
61
71
62
72
nextGen :: Grid -> Grid
63
73
nextGen grid = survivors grid ++ births grid
@@ -67,10 +77,10 @@ cls = system "cls"
67
77
68
78
play :: Grid -> IO ()
69
79
play grid = do
70
- cls
71
- putStr $ showGrid grid
72
- threadDelay $ speed* 10 ^ 3
73
- play $ nextGen grid
80
+ cls
81
+ putStr $ showGrid grid
82
+ threadDelay $ speed* 10 ^ 3
83
+ play $ nextGen grid
74
84
75
85
-- Choose any pre-built grid or customize your own!
76
86
main :: IO ()
0 commit comments