Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System.Random
- data CellState = Dead | Alive deriving (Eq)
- data Cell = Cell CellState deriving (Eq)
- data Row = Row [Cell]
- data Map = Map [Row]
- instance Show CellState where
- show Dead = "_"
- show Alive = "#"
- instance Show Cell where
- show (Cell cellState) = show cellState
- instance Show Row where
- show (Row cells) = unwords $ map show cells
- instance Show Map where
- show (Map rows) = unlines $ map show rows
- getElem :: Int -> [a] -> a
- getElem n xs
- | n >= length xs = xs!!(n - length xs)
- | n < 0 = xs!!(length xs + n)
- | otherwise = xs!!n
- getNeighbours :: Int -> Int -> [[Bool]] -> [Bool]
- getNeighbours x y xs = [ getElem (x-1) (getElem (y-1) xs), getElem x (getElem (y-1) xs) , getElem (x+1) (getElem (y-1) xs)
- , getElem (x-1) (getElem y xs) , getElem (x+1) (getElem y xs)
- , getElem (x-1) (getElem (y+1) xs), getElem x (getElem (y+1) xs) , getElem (x+1) (getElem (y+1) xs) ]
- countLivingNeighbours :: Int -> Int -> [[Bool]] -> Int
- countLivingNeighbours x y xs = length [x | x <- getNeighbours x y xs, x == True]
- intToCellState :: Int -> CellState -- TODO probably change to Maybe CellState one day
- intToCellState n
- | n == 0 = Dead
- | n == 1 = Alive
- | otherwise = Dead
- returnFixedRow :: Row
- returnFixedRow = Row $ map Cell $ map intToCellState [1,0,1,0,1]
- returnFixedMap :: Map
- returnFixedMap = Map $ replicate 5 returnFixedRow
Advertisement