One-dimensional cellular automata: Difference between revisions

Content added Content deleted
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(add FreeBASIC)
Line 540: Line 540:
Generation 8 : __##________________
Generation 8 : __##________________
Generation 9 : __##________________</pre>
Generation 9 : __##________________</pre>

==={{header|FreeBASIC}}===
<lang freebasic>#define SIZE 640

randomize timer

dim as ubyte arr(0 to SIZE-1, 0 to 1)
dim as uinteger i
for i = 0 to SIZE - 1 'initialise array with zeroes and ones
arr(i, 0)=int(rnd+0.5)
next i

screen 12 'display graphically

dim as string ch=" "
dim as uinteger j = 0, cur = 0, nxt, prv, neigh
while not ch = "q" or ch = "Q"
for i = 0 to SIZE - 1
pset(i, j), 8+7*arr(i,cur) 'print off cells as grey, on cells as bright white
nxt = (i + 1) mod SIZE
prv = (i - 1)
if prv < 0 then prv = SIZE - 1 'let's have a wrap-around array for fun
neigh = arr(prv, cur) + arr(nxt, cur)
if arr(i, cur) = 0 then 'evolution rules
if neigh = 2 then
arr(i, 1-cur) = 1
else
arr(i, 1-cur) = 0
end if
else
if neigh = 0 or neigh = 2 then
arr(i, 1-cur) = 0
else
arr(i, 1-cur) = 1
end if
end if
next i
j = j + 1
cur = 1 - cur
do
ch = inkey
if ch <> "" then exit do 'press any key to advance the sim
'or Q to exit
loop
wend</lang>


==={{header|Sinclair ZX81 BASIC}}===
==={{header|Sinclair ZX81 BASIC}}===