December 03, 2020, 07:40:16 PM

### Author Topic: [bb] 1D Cellular Automaton Explorer by zoqfotpik [ 1+ years ago ]  (Read 510 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] 1D Cellular Automaton Explorer by zoqfotpik [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : 1D Cellular Automaton Explorer
Author : zoqfotpik
Posted : 1+ years ago

Description : This produces images by showing the evolution of a one-dimensional cellular automaton over time (y coordinate = elapsed time.)

Hit R to randomly choose a new ruleset, C to change the color palette.

Cellular automata are computational.  There are many bizarre and interesting images possible from this, don't just run it once or twice and assume that the output is ugly or simple-- it's not.  Hit R a bunch of times.

Code :
Code: BlitzBasic
1. ' 2d ca
2.
3. Global states[100000]
4. Global array[1000]
5. Global nextarray[1000]
7. Global r%[50]
8. Global g%[50]
9. Global b%[50]
10. Global low%
11. Global high%
12. Global cellsize = 2
13. Global celltypes = 10
14. Global screenarray[1000,1000]
15. Global drawstripheight%=10
16. Global scrollwindow% = 1000
17. Function initcolors()
18. For i = 1 To 49
19.         r[i]=Rand(255)
20.         g[i]=Rand(255)
21.         b[i]=Rand(255)
22.         Next
23. End Function
24.
25. Function initarray()
26.         'For i = 0 To 999
27.         'array[i]=Rand(49)
28.         'Next
29.
30.         'For i = 450 To 550
31.                 'array[i]=Rand(49)
32.         'Next
33.
34.         For i = 0 To 999
35.                 array[i]=Rand(celltypes)
36.         Next
37.
38.         'array[500]=Rand(49)
39. End Function
40.
41. Function initstates()
42.         For i = 0 To 99999
43.         states[i]=Rand(celltypes)-1
44.         Next
45.         states[0]=0
47.         high = low + Rand(neighborradius*50)
48. End Function
49.
50. Function doarray()
51.         For j = 0 To 1000/cellsize
52.                 For i = 0 To 999/cellsize
53.                         Local neighbors = 0
55.                                 absx = x
56.                                 If absx<0 absx = absx+1000/cellsize
57.                                 If absx>999/cellsize absx = absx-1000/cellsize
58.                                 neighbors = neighbors + array[absx]
59.                         Next
60.                         nextcell = states[neighbors]
61.                         If neighbors < low Or neighbors > high nextcell = 0
62.                         nextarray[i]=states[neighbors]
63.                         'SetColor r[states[neighbors]],g[states[neighbors]],b[states[neighbors]]
64.                         'DrawRect i*cellsize,j*cellsize,cellsize,cellsize
65.
66.                 Next
67.                 For i = 0 To 999/cellsize
68.                         array[i]=nextarray[i]
69.                         screenarray[i,j]=nextarray[i]
70.                 Next
71.                 'If j Mod 40 = 1 Flip
72.                 'states[Rand(celltypes)]=Rand(celltypes)
73.         Next
74.         'drawscreen()
75.
76. End Function
77.
78. Graphics 1000,1000
79. Cls
80. initcolors()
81. initarray()
82. initstates()
83. doarray()
84. Flip
85. While Not KeyDown(KEY_ESCAPE)
86.         If KeyHit(KEY_SPACE)
87.                 Cls
88.                 initarray()
89.                 doarray()
90.                 Flip
91.         EndIf
92.
93.         If KeyHit(KEY_R)
94.                 initstates()
95.                 initarray()
96.                 doarray()
97.                 Flip
98.         EndIf
99.
100.         If KeyHit(KEY_C)
101.                 initcolors()
102.                 drawscreen()
103.                 Flip
104.         EndIf
105.
106.         drawscreen
107.         Flip
108.         scrollwindow:+1
109.         If scrollwindow > 999 scrollwindow = 0
110. Wend
111.
112. Function drawscrolledscreen()
113.         Local y = 1000/cellsize
114.         For j = scrollwindow To 0 Step -1
115.         drawcellline(j,y*cellsize)
116.         y=y-1
117.         Flip
118.         Next
119. End Function
120.
121.
122. Function drawscreen()
123.         For j = 1 To 999
124.         'For i = 0 To 999/cellsize
125.         '       SetColor r[screenarray[i,j]],g[screenarray[i,j]],b[screenarray[i,j]]
126.         '       DrawRect i*cellsize,(j*cellsize)-cellsize,cellsize,cellsize
127.         'Next
128.         DrawcellLine(j,j*cellsize)
129.         Next
130. End Function
131.
132.
133.
134. Function DrawcellLine(j,screeny)
135.         For i = 0 To 999/cellsize
136.                 SetColor r[screenarray[i,j]],g[screenarray[i,j]],b[screenarray[i,j]]
137.                 DrawRect i*cellsize,screeny,cellsize,cellsize
138.         Next
139. End Function