forked from tA/cellularAutomata
added examples of other transition functions
This commit is contained in:
parent
7af444d32f
commit
d69831d2cb
88
README.md
88
README.md
@ -101,3 +101,91 @@ as such, the functions to change our focus within the space assumes an infinite
|
|||||||
██ █ █ ███ █ ███ █ █ ███ █ ███ █ █
|
██ █ █ ███ █ ███ █ █ ███ █ ███ █ █
|
||||||
█ █ ██ █ █ █ █ ██ █ █ █ ██
|
█ █ ██ █ █ █ █ ██ █ █ █ ██
|
||||||
```
|
```
|
||||||
|
|
||||||
|
example using `rule3` and a non-random starting position:
|
||||||
|
|
||||||
|
`./cellularAutomata -w 80 -g 80`
|
||||||
|
|
||||||
|
```
|
||||||
|
█
|
||||||
|
███
|
||||||
|
█ █ █
|
||||||
|
██ █ ██
|
||||||
|
█ █ █
|
||||||
|
███ ███ ███
|
||||||
|
█ █ █ █ █
|
||||||
|
██ ██ ███ ██ ██
|
||||||
|
█ █ █
|
||||||
|
███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █
|
||||||
|
██ █ ██ ██ █ ██ ██ █ ██
|
||||||
|
█ █ █ █ █
|
||||||
|
███ ███ ███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ ██ ██ ██ ██ █ ██ ██ ██ ██ ██
|
||||||
|
█ █ █
|
||||||
|
███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █
|
||||||
|
██ █ ██ ██ █ ██ ██ █ ██
|
||||||
|
█ █ █ █ █ █ █ █ █
|
||||||
|
███ ███ ███ ███ ███ ███ ███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ ██ ███ ██ ██ ██ ██ ███ ██ ██ ██ ██ ███ ██ ██
|
||||||
|
█ █ █ █ █
|
||||||
|
███ ███ ███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █
|
||||||
|
███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
||||||
|
█ █ █
|
||||||
|
███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █
|
||||||
|
██ █ ██ ██ █ ██ ██ █ ██
|
||||||
|
█ █ █ █ █ █ █ █ █
|
||||||
|
███ ███ ███ ███ ███ ███ ███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ ██ ███ ██ ██ ██ ██ ███ ██ ██ ██ ██ ███ ██ ██
|
||||||
|
█ █ █ █ █ █ █ █
|
||||||
|
█ ███ ███ ███ ███ ███ ███ ███ ██
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
█ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ██
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █ ██ ██ █
|
||||||
|
█ █ █
|
||||||
|
███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █
|
||||||
|
██ █ ██ ██ █ ██ ██ █ ██
|
||||||
|
█ █ █ █ █ █ █ █ █
|
||||||
|
███ ███ ███ ███ ███ ███ ███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ ██ ███ ██ ██ ██ ██ ███ ██ ██ ██ ██ ███ ██ ██
|
||||||
|
█ █ █ █ █ █ █
|
||||||
|
███ ███ ███ ███ ███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
||||||
|
█
|
||||||
|
███
|
||||||
|
█ █ █
|
||||||
|
██ █ ██
|
||||||
|
█ █ █
|
||||||
|
███ ███ ███
|
||||||
|
█ █ █ █ █
|
||||||
|
██ ██ ███ ██ ██
|
||||||
|
█ █ █
|
||||||
|
███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █
|
||||||
|
██ █ ██ ██ █ ██ ██ █ ██
|
||||||
|
█ █ █ █ █
|
||||||
|
███ ███ ███ ███ ███
|
||||||
|
█ █ █ █ █ █ █ █ █ █ █
|
||||||
|
██ ██ ██ ██ ██ █ ██ ██ ██ ██ ██
|
||||||
|
█ █ █
|
||||||
|
```
|
||||||
|
22
src/Main.hs
22
src/Main.hs
@ -97,6 +97,22 @@ rule (Space (l:_) _ (r:_))
|
|||||||
| l == r = Dead
|
| l == r = Dead
|
||||||
| otherwise = Alive
|
| otherwise = Alive
|
||||||
|
|
||||||
|
-- a second rule for example
|
||||||
|
rule2 :: Space CellState -> CellState
|
||||||
|
rule2 (Space (l1:l2:_) m (r1:r2:_))
|
||||||
|
| m == Alive && numAlive == 1 = Dead
|
||||||
|
| m == Alive && numAlive == 4 = Dead
|
||||||
|
| m == Dead && numAlive == 3 = Alive
|
||||||
|
| otherwise = m
|
||||||
|
where
|
||||||
|
ns = [l1, l2, r1, r2]
|
||||||
|
numAlive = length $ filter (== Alive) ns
|
||||||
|
|
||||||
|
rule3 :: Space CellState -> CellState
|
||||||
|
rule3 (Space (l:_) m (r:_))
|
||||||
|
| l == r = m
|
||||||
|
| otherwise = if m == Alive then Dead else Alive
|
||||||
|
|
||||||
-- take a space and a rule and
|
-- take a space and a rule and
|
||||||
-- return the next space
|
-- return the next space
|
||||||
step :: (Space t -> t) -> Space t -> Space t
|
step :: (Space t -> t) -> Space t -> Space t
|
||||||
@ -177,7 +193,7 @@ runAutomata :: Space CellState -> Int -> Int -> IO ()
|
|||||||
runAutomata s 0 w = putStrLn $ concat $ map show $ boundw w s
|
runAutomata s 0 w = putStrLn $ concat $ map show $ boundw w s
|
||||||
runAutomata s n w = do
|
runAutomata s n w = do
|
||||||
putStrLn $ concat $ map show $ boundw w s
|
putStrLn $ concat $ map show $ boundw w s
|
||||||
runAutomata (step rule s) (n - 1) w
|
runAutomata (step rule3 s) (n - 1) w
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
@ -190,5 +206,7 @@ main = do
|
|||||||
let m = head cs
|
let m = head cs
|
||||||
let l = take wh $ drop 1 cs
|
let l = take wh $ drop 1 cs
|
||||||
let r = take wh $ drop wh $ drop 1 cs
|
let r = take wh $ drop wh $ drop 1 cs
|
||||||
let s = Space (l ++ (repeat Dead)) m (r ++ (repeat Dead))
|
--let s = Space (l ++ (repeat Dead)) m (r ++ (repeat Dead))
|
||||||
|
-- non-random starting position for rule3 (the serpinski triangle)
|
||||||
|
let s = Space (repeat Dead) Alive (repeat Dead)
|
||||||
runAutomata s h w
|
runAutomata s h w
|
||||||
|
Loading…
Reference in New Issue
Block a user