Created
December 7, 2009 06:52
-
-
Save kevinclark/250673 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data CellState = Alive | Dead deriving (Eq, Show) | |
type Time = Integer | |
-- Status of a cell at a time t | |
cell :: Int -> Int -> Time -> CellState | |
-- Initial Board setup | |
cell 0 (-1) 0 = Alive | |
cell 0 0 0 = Alive | |
cell 0 1 0 = Alive | |
cell _ _ 0 = Dead | |
-- General rule | |
cell x y t = state (cell x y (t - 1)) (neighbors x y (t - 1)) | |
-- Survival Rules | |
state :: CellState -> Int -> CellState | |
state Alive n | |
| n < 2 = Dead | |
| n > 3 = Dead | |
| otherwise = Alive | |
state Dead n | |
| n == 3 = Alive | |
| otherwise = Dead | |
-- Count my neighbors | |
neighbors :: Int -> Int -> Time -> Int | |
neighbors x y t = length $ filter (\(x', y') -> cell x' y' t == Alive) border | |
where border = [ (x + dx, y + dy) | dx <- [-1..1], dy <- [-1..1], not (dx == 0 && dy == 0) ] | |
asBoardTile :: CellState -> Char | |
asBoardTile cellstate | |
| cellstate == Alive = 'O' | |
| cellstate == Dead = '-' | |
board :: Int -> Int -> Int -> Int -> Time -> [[CellState]] | |
board top right bottom left time = [[ cell x y time | x <- [left..right] ] | y <- [bottom..top] ] | |
bigBoard :: Time -> [[CellState]] | |
bigBoard = board 5 5 (-5) (-5) | |
bigTextyBoard :: Time -> String | |
bigTextyBoard t = foldr (\val acc -> val ++ "\n" ++ acc) "" [map asBoardTile row | row <- bigBoard t] | |
main = putStr $ foldr (\val acc -> val ++ "\027[2J\n" ++ acc) "" [ bigTextyBoard t | t <- [0..10] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clio:death kev$ ./death | |
----------- | |
----------- | |
----------- | |
----------- | |
-----O----- | |
-----O----- | |
-----O----- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----OOO---- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
-----O----- | |
-----O----- | |
-----O----- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----OOO---- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
-----O----- | |
-----O----- | |
-----O----- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----OOO---- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
----------- | |
-----O----- | |
-----O----- | |
-----O----- | |
----------- | |
----------- | |
----------- | |
----------- | |
..... # really slow here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment