game-of-life 0.1.0.2 → 0.1.0.3
raw patch · 2 files changed
+69/−3 lines, 2 files
Files
- Simulation.hs +66/−0
- game-of-life.cabal +3/−3
+ Simulation.hs view
@@ -0,0 +1,66 @@+module Simulation where++import Data.Array++data State = Alive | Dead deriving (Eq)+type Row = Array Int State+type Grid = Array Int Row++data Board = Board {bGrid :: Grid, bRows :: Int, bCols :: Int} deriving (Show)+data Coord = Coord Int Int deriving (Show)++instance Num Coord where+ Coord a b + Coord c d = Coord (a+c) (b+d)+ Coord a b - Coord c d = Coord (a-c) (b-d)++instance Show State where+ show Alive = "■"+ show Dead = " "++directions :: [Coord]+directions = [Coord y x | y <- [-1..1], x <- [-1..1], not (x==0 && y==0)]++neighbors :: Board -> Coord -> [Coord]+neighbors board coord = filter (\c -> state board c == Alive) validCoords+ where+ validCoords = filter (validCoord board) coordsAround+ coordsAround = map (+ coord) directions++validCoord :: Board -> Coord -> Bool+validCoord board (Coord y x) = xValid && yValid+ where + xValid = x >= 0 && x < bCols board+ yValid = y >= 0 && y < bRows board++numNeighbors :: Board -> Coord -> Int+numNeighbors board coord = length $ neighbors board coord++state :: Board -> Coord -> State+state board (Coord y x) = (bGrid board) ! y ! x++singleStep :: Board -> Coord -> State+singleStep board coord = stepCell currentState bors+ where + currentState = state board coord+ bors = numNeighbors board coord++stepCell :: State -> Int -> State+stepCell Dead 3 = Alive+stepCell Dead _ = Dead+stepCell Alive n+ | n < 2 = Dead+ | n > 3 = Dead+ | otherwise = Alive++step :: Board -> Board+step board = board {bGrid = newGrid}+ where+ newGrid :: Grid+ newGrid = listArray (0, rs-1) $ map newRow [0..rs-1]++ newRow :: Int -> Row+ newRow y = listArray (0,cs-1) $+ [singleStep board (Coord y x) | x <- [0..(cs)-1]]++ cs = bCols board+ rs = bRows board
game-of-life.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: game-of-life-version: 0.1.0.2+version: 0.1.0.3 synopsis: Conway's Game of Life -- description: license: MIT@@ -18,7 +18,7 @@ executable game-of-life main-is: Main.hs- -- other-modules: + other-modules: Simulation -- other-extensions: build-depends: base >=4.7 && <4.8, random,@@ -27,4 +27,4 @@ text -- hs-source-dirs: default-language: Haskell2010- ghc-options: -O2+ -- ghc-options: -O2