-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lifegame.hs
203 lines (188 loc) · 4.38 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
module Lifegame
(
World
, display
, surround
, willBeBorn
, willBeAlive
, willBeDead
) where
import System.Posix
import Data.List
-- $setup
-- >>> let d22 = display [0..2] [0..2] :: AliveList -> IO()
-- | 座標を表すデータ
-- >>> Point 1 2
-- Point 1 2
data Point = Point Int Int deriving(Show, Eq, Ord)
-- | ある座標の周りにある座標のリスト
-- >>> surround (Point 1 2)
-- [Point 0 1,Point 1 1,Point 2 1,Point 0 2,Point 2 2,Point 0 3,Point 1 3,Point 2 3]
--
-- >>> surround (Point 2 2)
-- [Point 1 1,Point 2 1,Point 3 1,Point 1 2,Point 3 2,Point 1 3,Point 2 3,Point 3 3]
--
surround :: Point -> [Point]
surround (Point x y) = [ Point a b |
b <- [y-1, y, y+1],
a <- [x-1, x, x+1],
a/=x || b/=y ]
-- | 生存しているセルのリストで、世界全体を表現する
-- (とりあえずの仮実装)
-- >>> AliveList [Point 1 3,Point 2 2]
-- AliveList [Point 1 3,Point 2 2]
data AliveList = AliveList [Point] deriving(Show)
class World a where
alive :: a -> Point -> Bool
next :: a -> a
-- | あるPointが生きているか返す
-- >>> let world = AliveList [Point 1 2]
-- >>> alive world (Point 1 2)
-- True
--
-- >>> alive world (Point 1 3)
-- False
--
-- >>> let w = AliveList [Point 0 1,Point 0 2,Point 1 2]
-- >>> display [0..2] [0..2] w
-- oo_
-- o__
-- ___
--
-- >>> display [0..2] [0..2] (next w)
-- oo_
-- oo_
-- ___
--
-- >>> let w = AliveList [Point 1 1,Point 0 2,Point 1 2]
-- >>> display [0..2] [0..2] w
-- oo_
-- _o_
-- ___
--
-- >>> d22 (next w)
-- oo_
-- oo_
-- ___
--
-- >>> let w3 = AliveList [Point 0 1,Point 1 1,Point 0 2,Point 1 2,Point 2 2]
-- >>> display [0..2] [0..3] w3
-- ___
-- ooo
-- oo_
-- ___
--
-- >>> display [0..2] [0..3] $ next w3
-- _o_
-- o_o
-- o_o
-- ___
--
-- >>> next w3
-- AliveList [Point 0 1,Point 0 2,Point 1 3,Point 2 1,Point 2 2]
--
-- >>> let w4 = AliveList [Point 1 1,Point 2 1]
-- >>> d22 w4
-- ___
-- _oo
-- ___
--
-- >>> d22 $ next w4
-- ___
-- ___
-- ___
instance World AliveList where
alive (AliveList ls) pt = pt `elem` ls
next w@(AliveList ls) = AliveList nextList where
baby = filter (willBeBorn w) $
filter (not . alive w) $
concatMap surround ls
stay = filter (willBeAlive w) ls
uniq = map head . group . sort
nextList = uniq (baby ++ stay)
run :: World a => [Int] -> [Int] -> a -> Int -> IO ()
run _ _ _ 0 = return ()
run xs ys world num = do
putStr "\ESC[2J"
display xs ys world
usleep 1000000
run xs ys (next world) (num-1)
-- | 盤面を表示する
-- >>> let world = AliveList [Point 1 2]
-- >>> display [0..3] [0..2] world
-- _o__
-- ____
-- ____
--
-- >>> let world2 = AliveList [Point 1 2, Point 2 2]
-- >>> display [0..3] [0..2] world2
-- _oo_
-- ____
-- ____
--
-- >>> display [0..5] [0..2] world2
-- _oo___
-- ______
-- ______
display :: World a => [Int] -> [Int] -> a -> IO ()
display xs ys w =
mapM_ (putStrLn . lineAt) ys'
where
ys' = reverse ys
lineAt y = map ptToChar [Point a y | a <- xs ]
ptToChar pt
| alive w pt = 'o'
| otherwise = '_'
-- FIXME 適切な名前を考える。Bornって形容詞じゃないか?
-- | ある座標が、次回「誕生」するか
-- >>> let w = AliveList [Point 0 1,Point 0 2,Point 1 2]
-- >>> display [0..2] [0..2] w
-- oo_
-- o__
-- ___
--
-- >>> willBeBorn w (Point 1 1)
-- True
--
-- >>> willBeBorn w (Point 2 2)
-- False
willBeBorn :: World a => a -> Point -> Bool
willBeBorn w pt = num == 3 where
num = length $ filter (alive w) $ surround pt
-- | 次の状態で、あるポイントが生存するかどうか
-- >>> let w = AliveList [Point 1 1,Point 0 2,Point 1 2]
-- >>> display [0..2] [0..2] w
-- oo_
-- _o_
-- ___
--
-- >>> willBeAlive w (Point 1 1)
-- True
--
-- >>> let w2 = AliveList []
-- >>> display [0..3] [0..3] w2
-- ____
-- ____
-- ____
-- ____
--
-- >>> willBeAlive w2 (Point 1 1)
-- False
willBeAlive :: World a => a -> Point -> Bool
willBeAlive world pt = num == 2 || num == 3 where
num = length $ filter (alive world) $ surround pt
-- | 死ぬケース
-- >>> let w = AliveList [Point 1 1, Point 2 1]
-- >>> willBeDead w (Point 1 1)
-- True
--
-- >>> let w2 = AliveList [Point 0 1,Point 1 1,Point 0 2,Point 1 2,Point 2 2]
-- >>> display [0..2] [0..2] w2
-- ooo
-- oo_
-- ___
--
-- >>> willBeDead w2 (Point 1 1)
-- True
willBeDead :: World a => a -> Point -> Bool
willBeDead w pt = not $ willBeAlive w pt