One-dimensional cellular automata: Difference between revisions

No edit summary
Line 1,036:
Generation 9: __##________________
</pre>
=={{header|Lua}}==
<lang lua>num_iterations = 9
f = { 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 }
 
function Output( f, l )
io.write( l, ": " )
for i = 1, #f do
local c
if f[i] == 1 then c = '#' else c = '_' end
io.write( c )
end
print ""
end
 
Output( f, 0 )
 
for l = 1, num_iterations do
local g = {}
for i = 2, #f-1 do
if f[i-1] + f[i+1] == 1 then
g[i] = f[i]
elseif f[i] == 0 and f[i-1] + f[i+1] == 2 then
g[i] = 1
else
g[i] = 0
end
end
if f[1] == 1 and f[2] == 1 then g[1] = 1 else g[1] = 0 end
if f[#f] == 1 and f[#f-1] == 1 then g[#f] = 1 else g[#f] = 0 end
f, g = g, f
 
Output( f, l )
end </lang>
<pre>0: _###_##_#_#_#_#__#__
1: _#_#####_#_#_#______
2: __##___##_#_#_______
3: __##___###_#________
4: __##___#_##_________
5: __##____###_________
6: __##____#_#_________
7: __##_____#__________
8: __##________________
9: __##________________</pre>
 
=={{header|M4}}==
Anonymous user