Anonymous user
One-dimensional cellular automata: Difference between revisions
→{{header|COBOL}}
(→{{header|Racket}}: Added.) |
|||
Line 674:
<lang cobol>
Identification division.
Program-id. rc-1d-cell.
Data division.
Working-storage section.
* "Constants."
01 max-gens pic 999 value 9.
01 state-width pic 99 value 20.
01 state-table-init pic x(20) value ".@@@.@@.@.@.@.@..@..".
01 alive pic x value "@".
01 dead pic x value ".".
* The current state.
01 state-gen pic 999 value 0.
01 state-row.
05 state-row-gen pic zz9.
05 filler pic xx value ": ".
05 state-table.
10 state-cells pic x occurs 20 times.
* The new state.
01 new-state-table.
05 new-state-cells pic x occurs 20 times.
* Pointer into cell table during generational production.
01 cell-index pic 99.
88 at-beginning value 1.
88 is-inside values 2 thru 19.
88 at-end value 20.
* The cell's neighborhood.
01 neighbor-count-table.
88 is-
88 is-ripe value 2.
Procedure division.
Perform
Perform max-gens times
perform
perform Next-state
end-perform.
Perform Display-row.
Stop run.
Display-row.
Display state-row.
* Determine who lives and who dies.
Next-state.
Move state-table to new-state-table.
Perform with test after
varying cell-index from 1 by 1
until at-end
perform
end-perform
Perform with test after
varying cell-index from 1 by 1
until at-end
perform Die-off
end-perform
Perform with test after
varying cell-index from 1 by 1
until at-end
perform New-births
end-perform
move new-state-table to state-table.
* Living cell with wrong number of neighbors...
Die-off.
alive and not is-comfy (cell-index)
end-if
.
* Empty cell with exactly two neighbors are...
New-births.
if state-cells(cell-index) = dead and is-ripe (cell-index)
end-if
.
* How many living neighbors does a cell have?
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>
Sample output:
0: .@@@.@@.@.@.@.@..@..
1: .@.@@@@@.@.@.@......
2: ..@@...@@.@.@.......
3: ..@@...@@@.@........
4: ..@@...@.@@........
5: ..@@....@@@.........
6: ..@@....@.@.........
7: ..@@.....@..........
8: ..@@..............
9: ..@@................
=={{header|CoffeeScript}}==
|