Guest User

Untitled

a guest
Jul 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. module Main where
  2.  
  3. import qualified Data.Set as S
  4. import Graphics.Gloss
  5.  
  6. type Cell = (Int, Int)
  7. type Plane = S.Set Cell
  8.  
  9. main :: IO ()
  10. main = simulate (InWindow "Game of Life" (500, 500) (0, 0)) white 10 seed (draw) (step)
  11. where step view delta alive = live alive
  12. draw alive = pictures $ map (drawCell) (S.toList alive)
  13. where drawCell (x, y) = translate (fromIntegral x * 5 :: Float) (fromIntegral y * 5 :: Float) $ rectangleSolid 5 5
  14.  
  15. -- Returns a set of cells surrounding a cell
  16. offset :: Cell -> Plane
  17. offset (x, y) = S.fromList [(x + dx, y + dy) | dx <- [-1..1], dy <- [-1..1], not (dx == 0 && dy == 0)]
  18.  
  19. -- Returns the amount of cells surrounding a cell
  20. neighbors :: Plane -> Cell -> Int
  21. neighbors alive cell = S.size $ S.filter (`S.member` alive) (offset cell)
  22.  
  23. -- Takes a generation and produces the next
  24. live :: Plane -> Plane
  25. live alive = S.filter (willlive alive) (more alive)
  26. where more alive = S.union alive $ S.unions [offset cell | cell <- S.toList alive]
  27. willlive alive cell
  28. | S.member cell alive = neighbors alive cell `elem` [2, 3]
  29. | otherwise = neighbors alive cell == 3
  30.  
  31. -- Gospers Glider Gun
  32. seed :: Plane
  33. 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