One-dimensional cellular automata: Difference between revisions

Content added Content deleted
Line 1,984: Line 1,984:
const
const
num_turns = 20
num_turns = 20
symbols:TSymbols = ('#',' ')
symbols:TSymbols = ('#','_')


proc `==` (x:TBoolArray,y:TBoolArray): bool =
proc `==` (x:TBoolArray,y:TBoolArray): bool =
Line 2,015: Line 2,015:
for i in 0..(len(map)-1):
for i in 0..(len(map)-1):
map[i] = bool(random(2))
map[i] = bool(random(2))
return map

proc fixed_map(): TBoolArray =
var map = [False,True,True,True,False,True,True,False,True,False,True,
False,True,False,True,False,False,True,False,False,False,False,
False,False,False,False,False,False,False,False,False]
return map
return map


#make the map
#make the map
var map:TBoolArray
var map:TBoolArray
map = random_map()
#map = random_map() # uncomment for random start
map = fixed_map()
print_map(map,symbols)
print_map(map,symbols)
for i in 0..num_turns:
for i in 0..num_turns:
Line 2,038: Line 2,045:
</lang>
</lang>
Example output:
Example output:
<pre>_###_##_#_#_#_#__#_____________
<pre>
_#_#####_#_#_#_________________
# # ### ## ##### ### ####
__##___##_#_#__________________
## #### # ### ### #
__##___###_#___________________
#### # # ### #
__##___#_##____________________
# # ## ##
__##____###____________________
#####
__##____#_#____________________
# #
__##_____#_____________________
__##___________________________
__##___________________________</pre>


'''Using a string character counting method''':
<lang nimrod>const
s_init: string = "_###_##_#_#_#_#__#__"
arrLen: int = 20
var q0: string = s_init & repeatChar(arrLen-20,'_')
var q1: string = q0

proc life(s:string): char =
var str: string = s
if len(normalize(str)) == 2: # normalize eliminates underscores
return '#'
return '_'
proc evolve(q: string): string =
result = repeatChar(arrLen,'_')
#result[0] = '_'
for i in 1 .. q.len-1:
result[i] = life(substr(q & '_',i-1,i+1))

echo(q1)
q1 = evolve(q0)
echo(q1)
while q1 != q0:
q0 = q1
q1 = evolve(q0)
echo(q1)</lang>
Example output:
<pre>_###_##_#_#_#_#__#__
_#_#####_#_#_#______
__##___##_#_#_______
__##___###_#________
__##___#_##_________
__##____###_________
__##____#_#_________
__##_____#__________
__##________________
__##________________</pre>


</pre>
=={{header|OCaml}}==
=={{header|OCaml}}==