One-dimensional cellular automata: Difference between revisions
Content added Content deleted
No edit summary |
(add language: Retro) |
||
Line 1,450: | Line 1,450: | ||
</pre> |
</pre> |
||
=={{header|Retro}}== |
|||
<lang Retro>: $, dup getLength 0 swap for 2dup + @ , 1+ next 2drop 0 , ; |
|||
create world ".###.##.#.#.#.#..#.." $, |
|||
create world2 21 allot |
|||
create group 4 allot |
|||
variable neighbors |
|||
: middle ( - ) |
|||
0 !neighbors |
|||
group @ '# =if neighbors ++ then |
|||
group 2 + @ '# =if neighbors ++ then |
|||
; |
|||
: left.edge ( - ) @world '# =if 1 else 0 then !neighbors ; |
|||
: right.edge ( - ) world 19 + @ '# =if 1 else 0 then !neighbors ; |
|||
: flip ( n-n ) dup world + @ '# =if '. ;then '# ; |
|||
: check ( n-n ) |
|||
dup 0 =if left.edge ;then |
|||
dup 19 =if right.edge ;then |
|||
dup world + 1- group 3 copy middle ; |
|||
: gen ( - ) |
|||
0 20 for |
|||
0 !neighbors |
|||
check |
|||
@neighbors 0 =if dup world2 + '. swap ! then |
|||
@neighbors 1 =if dup world + @ over world2 + ! then |
|||
@neighbors 2 =if flip over world2 + ! then |
|||
1+ |
|||
next drop |
|||
world2 world 20 copy ; |
|||
: generations ( n- ) |
|||
clear 0 swap for dup putn space world puts cr gen 1+ next drop ;</lang> |
|||
Sample Output: |
|||
<pre>10 generations |
|||
0 .###.##.#.#.#.#..#.. |
|||
1 .#.#####.#.#.#...... |
|||
2 ..##...##.#.#....... |
|||
3 ..##...###.#........ |
|||
4 ..##...#.##......... |
|||
5 ..##....###......... |
|||
6 ..##....#.#......... |
|||
7 ..##.....#.......... |
|||
8 ..##................ |
|||
9 ..##................</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |