One-dimensional cellular automata: Difference between revisions

Content added Content deleted
Line 674: Line 674:


<lang cobol>
<lang cobol>
Identification division.
Identification division.
Program-id. rc-1d-cell.
Program-id. rc-1d-cell.

Data division.
Data division.
Working-storage section.
Working-storage section.

* "Constants."
* "Constants."
01 max-gens pic 999 value 9.
01 max-gens pic 999 value 9.
01 state-width pic 99 value 20.
01 state-width pic 99 value 20.
01 state-table-init pic x(20) value ".@@@.@@.@.@.@..@..".
01 state-table-init pic x(20) value ".@@@.@@.@.@.@.@..@..".
01 alive pic x value "@".
01 alive pic x value "@".
01 dead pic x value ".".
01 dead pic x value ".".

* The current state.
* The current state.
01 state-gen pic 999 value 0.
01 state-gen pic 999 value 0.
01 state-row.
01 state-row.
05 state-row-gen pic zz9.
05 state-row-gen pic zz9.
05 filler pic xx value ": ".
05 filler pic xx value ": ".
05 state-table.
05 state-table.
10 state-cells pic x occurs 20 times.
10 state-cells pic x occurs 20 times.

* The new state.
* The new state.
01 new-state-table.
01 new-state-table.
05 new-state-cells pic x occurs 20 times.
05 new-state-cells pic x occurs 20 times.

* Pointer into cell table during generational production.
* Pointer into cell table during generational production.
01 cell-index pic 99.
01 cell-index pic 99.
88 at-beginning value 1.
88 at-beginning value 1.
88 is-inside values 2 thru 19.
88 is-inside values 2 thru 19.
88 at-end value 20.
88 at-end value 20.

* The cell's neighborhood.
* The cell's neighborhood.
01 neighbor-count pic 9.
01 neighbor-count-table.
88 is-comfy value 1.
03 neighbor-count pic 9 occurs 20 times.
88 is-ripe value 2.
88 is-comfy value 1.
88 is-ripe value 2.

Procedure division.
Procedure division.
Perform Init-state-table.
Perform max-gens times
Perform Init-state-table.
Perform max-gens times
perform Display-row
perform Next-state
perform Display-row
perform Next-state
end-perform.
end-perform.
Perform Display-row.
Perform Display-row.
Stop run.
Stop run.

Display-row.
Display-row.
Move state-gen to state-row-gen.
Display state-row.
Move state-gen to state-row-gen.
Display state-row.

* Determine who lives and who dies.
* Determine who lives and who dies.
Next-state.
Next-state.
Add 1 to state-gen.
Move state-table to new-state-table.
Add 1 to state-gen.
Move state-table to new-state-table.
Perform with test after
varying cell-index from 1 by 1
Perform with test after
until at-end
varying cell-index from 1 by 1
perform Count-neighbors
until at-end
perform Die-off
perform New-births
perform Count-neighbors
end-perform.
end-perform
move new-state-table to state-table.
Perform with test after

varying cell-index from 1 by 1
* Living cell with wrong number of neighbors...
until at-end
Die-off.
perform Die-off
if state-cells(cell-index) = alive and not is-comfy
end-perform
then move dead to new-state-cells(cell-index).

Perform with test after
* Empty cell with exactly two neighbors are...
varying cell-index from 1 by 1
New-births.
until at-end
if state-cells(cell-index) = dead and is-ripe
perform New-births
then move alive to new-state-cells(cell-index).
end-perform

* How many living neighbors does a cell have?
move new-state-table to state-table.
Count-neighbors.
Move 0 to neighbor-count.
* Living cell with wrong number of neighbors...
if at-beginning or at-end then
Die-off.
add 1 to neighbor-count.
if is-inside and state-cells(cell-index - 1) = alive then
if state-cells(cell-index) =
alive and not is-comfy (cell-index)
add 1 to neighbor-count.
if is-inside and state-cells(cell-index + 1) = alive then
then move dead to new-state-cells(cell-index)
end-if
add 1 to neighbor-count.
.

* String is easier to enter, but table is easier to work with,
* Empty cell with exactly two neighbors are...
* so move each character of the initialization string to the state-table.
New-births.
Init-state-table.
if state-cells(cell-index) = dead and is-ripe (cell-index)
Perform with test after
varying cell-index from 1 by 1
then move alive to new-state-cells(cell-index)
end-if
until at-end
.
move state-table-init(cell-index:1) to state-cells(cell-index)
* How many living neighbors does a cell have?
end-perform.
Count-neighbors.
Move 0 to neighbor-count(cell-index)
if at-beginning or at-end then
add 1 to neighbor-count(cell-index)
else
if is-inside and state-cells(cell-index - 1) = alive
then
add 1 to neighbor-count(cell-index)
end-if
if is-inside and state-cells(cell-index + 1) = alive
then
add 1 to neighbor-count(cell-index)
end-if
end-if
.
* String is easier to enter, but table is easier to work with,
* so move each character of the initialization string to the
* state table.
Init-state-table.
Perform with test after
varying cell-index from 1 by 1
until at-end
move state-table-init(cell-index:1)
to state-cells(cell-index)
end-perform
.
</lang>
</lang>


Sample output:
Sample output:


0: .@@@.@@.@.@.@..@..
0: .@@@.@@.@.@.@.@..@..
1: .@.@@@@@.@.@......
1: .@.@@@@@.@.@.@......
2: ..@@...@@.@.......
2: ..@@...@@.@.@.......
3: ..@@...@@@........
3: ..@@...@@@.@........
4: ..@@...@.@........
4: ..@@...@.@@.........
5: ..@@....@.........
5: ..@@....@@@.........
6: ..@@..............
6: ..@@....@.@.........
7: ..@@..............
7: ..@@.....@..........
8: ..@@..............
8: ..@@................
9: ..@@..............
9: ..@@................


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==