Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main where
- import qualified Data.Set as S
- import Graphics.Gloss
- type Cell = (Int, Int)
- type Plane = S.Set Cell
- main :: IO ()
- main = simulate (InWindow "Game of Life" (500, 500) (0, 0)) white 10 seed (draw) (step)
- where step view delta alive = live alive
- draw alive = pictures $ map (drawCell) (S.toList alive)
- where drawCell (x, y) = translate (fromIntegral x * 5 :: Float) (fromIntegral y * 5 :: Float) $ rectangleSolid 5 5
- -- Returns a set of cells surrounding a cell
- offset :: Cell -> Plane
- offset (x, y) = S.fromList [(x + dx, y + dy) | dx <- [-1..1], dy <- [-1..1], not (dx == 0 && dy == 0)]
- -- Returns the amount of cells surrounding a cell
- neighbors :: Plane -> Cell -> Int
- neighbors alive cell = S.size $ S.filter (`S.member` alive) (offset cell)
- -- Takes a generation and produces the next
- live :: Plane -> Plane
- live alive = S.filter (willlive alive) (more alive)
- where more alive = S.union alive $ S.unions [offset cell | cell <- S.toList alive]
- willlive alive cell
- | S.member cell alive = neighbors alive cell `elem` [2, 3]
- | otherwise = neighbors alive cell == 3
- -- Gospers Glider Gun
- seed :: Plane
- seed = S.fromList [(5, 1), (5, 2), (6, 1), (6, 2), (5, 11), (6, 11), (7, 11), (4, 12), (3, 13), (3, 14), (8, 12), (9, 13), (9, 14), (6, 15), (4, 16), (5, 17), (6, 17), (7, 17), (6, 18), (8, 16), (3, 21), (4, 21), (5, 21), (3, 22), (4, 22), (5, 22), (2, 23), (6, 23), (1, 25), (2, 25), (6, 25), (7, 25), (3, 35), (4, 35), (3, 36), (4, 36)]
Add Comment
Please, Sign In to add comment