-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLifegame.hs
75 lines (63 loc) · 1.37 KB
/
Lifegame.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module Lifegame where
import Prelude
import Control.Concurrent (threadDelay)
main :: IO ()
main = loop new
loop :: Array -> IO ()
loop a = do
threadDelay 10000000
putStrLn $ view a
loop (update a)
type Array = Int -> Int -> Bool
-- #
-- #
-- ###
new :: Array
new = \x_ y_ -> let
x = x_ `mod` 20
y = y_ `mod` 20
in
False
|| (x == 0 && y == 1)
|| (x == 1 && y == 2)
|| (x == 2 && y == 0)
|| (x == 2 && y == 1)
|| (x == 2 && y == 2)
update :: Array -> Array
update a = \x_ y_ -> let
x = x_ `mod` 20
y = y_ `mod` 20
b = a x y
n = 0
+ bti (a (x - 1) (y - 1))
+ bti (a (x - 1) (y + 0))
+ bti (a (x - 1) (y + 1))
+ bti (a (x + 0) (y - 1))
-- + bti (a (x + 0) (y + 0))
+ bti (a (x + 0) (y + 1))
+ bti (a (x + 1) (y - 1))
+ bti (a (x + 1) (y + 0))
+ bti (a (x + 1) (y + 1))
in
next b n
next :: Bool -> Int -> Bool
next False 3 = True
next False _ = False
next True 2 = True
next True 3 = True
next True _ = False
bti :: Bool -> Int
bti False = 0
bti True = 1
view :: Array -> String
view a = let
a' = fmap (<$> [ 0 .. 19 ]) $ (<$> [ 0 .. 19 ]) $ a
in
bits a'
bits :: [[Bool]] -> String
bits [] = ""
bits (x : xs) = bit x $ ('\n' :) $ bits xs
bit :: [Bool] -> String -> String
bit [] = id
bit (False : xs) = ('-' :) . bit xs
bit (True : xs) = ('#' :) . bit xs