2014-12-19 20:09:30 +01:00
|
|
|
import Matrix
|
2014-12-19 20:02:47 +01:00
|
|
|
|
|
|
|
type Cell = Int
|
2014-12-19 20:09:30 +01:00
|
|
|
type Grid = Mat Cell
|
2014-12-19 20:02:47 +01:00
|
|
|
|
2014-12-19 20:24:16 +01:00
|
|
|
-- | Print a grid
|
|
|
|
gprint :: Grid -> IO ()
|
|
|
|
gprint = putStrLn . map replace . show
|
|
|
|
where
|
|
|
|
replace '1' = '■'
|
|
|
|
replace '0' = '.'
|
|
|
|
replace a = a
|
2014-12-19 20:02:47 +01:00
|
|
|
|
|
|
|
-- | Get the state of a cell
|
|
|
|
-- 0 when out of the grid
|
|
|
|
(!) :: Grid -> Pos -> Cell
|
|
|
|
(Mat g) ! (x, y) = case g ?? y >>= (?? x) of
|
|
|
|
Nothing -> 0
|
|
|
|
Just v -> v
|
|
|
|
|
2014-12-19 20:09:30 +01:00
|
|
|
-- | List of neighbours cells
|
2014-12-19 20:02:47 +01:00
|
|
|
near :: Grid -> Pos -> [Cell]
|
|
|
|
near g (x, y) = [g ! (x+x', y+y') | x' <- [-1..1],
|
|
|
|
y' <- [-1..1],
|
|
|
|
(x',y') /= (0,0)]
|
|
|
|
|
|
|
|
-- | Find if a cell will be alive in the next generation
|
|
|
|
alive :: Grid -> Pos -> Cell
|
|
|
|
alive g p
|
|
|
|
| v == 0 && n == 3 = 1
|
|
|
|
| v == 1 && (n == 2 || n == 3) = 1
|
|
|
|
| otherwise = 0
|
|
|
|
where
|
|
|
|
(n, v) = (sum (near g p), g ! p)
|
|
|
|
|
|
|
|
-- | Compute next generation
|
|
|
|
next :: Grid -> Grid
|
|
|
|
next g = fmap (alive g) (indeces g)
|
|
|
|
|
|
|
|
main :: IO ()
|
2014-12-19 20:24:16 +01:00
|
|
|
main = mapM_ gprint (iterate next grid)
|
2014-12-19 20:02:47 +01:00
|
|
|
|
|
|
|
grid = Mat [
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
]
|