This is a 1-dimensional cellular automata implemented in Qbasic. Yes, I know, QBasic is not state-of-the-art, but I wrote this mainly for humans to read. While the code is indeed ready to run, it is very slow. The rule in the code shown is Rule 90, but you can change that easily.
If you change the Screen 11 statement to Screen 12, the output will be colorized based on the rule.

10
REM Initialize

REM Switch this to Screen 12 for color
SCREEN 11

Rem Seed the random number generator based on the time; used for pallete
RANDOMIZE VAL(MID$(TIME$, 4, 2) + RIGHT$(TIME$, 2)) 
GOSUB 1000

15
CLS
GOSUB 4000
DIM rules(8) AS INTEGER
DIM bits(3) AS INTEGER


20 REM Init Main Loop

PrevY = 1
CurrY = 2

30 REM Main Loop

FOR x = 1 TO 640

bits(1) = 0: bits(2) = 0: bits(3) = 0
xless = x
xmore = x
IF xless < 2 THEN xless = 641
IF xmore > 639 THEN xmore = 0
IF POINT(xless - 1, PrevY) > 0 THEN bits(1) = 1
IF POINT(x, PrevY) > 0 THEN bits(2) = 1
IF POINT(xmore + 1, PrevY) > 0 THEN bits(3) = 1

GOSUB 2000

LINE (x, CurrY)-(x, CurrY), pixel

NEXT x
PrevY = CurrY
CurrY = CurrY + 1

IF CurrY = 480 THEN CurrY = 0: IF INKEY$ = " " THEN GOSUB 3000: GOTO 15

GOTO 30

1000
REM Input your rule here. Should be self explanatory
rules(1) = 0
rules(2) = 1
rules(3) = 0
rules(4) = 1
rules(5) = 1
rules(6) = 0
rules(7) = 1
rules(8) = 0
GOSUB 1100
RETURN

1100
REM this subroutine colorizes the output. No heresy here - it's still producing the same thing.
FOR i = 1 TO 8
IF rules(i) > 0 THEN rules(i) = FIX(RND * 14 + 1)
NEXT i
RETURN

2000
IF bits(1) = 1 AND bits(2) = 1 AND bits(3) = 1 THEN pixel = rules(1): RETURN
IF bits(1) = 1 AND bits(2) = 1 AND bits(3) = 0 THEN pixel = rules(2): RETURN
IF bits(1) = 1 AND bits(2) = 0 AND bits(3) = 1 THEN pixel = rules(3): RETURN
IF bits(1) = 1 AND bits(2) = 0 AND bits(3) = 0 THEN pixel = rules(4): RETURN
IF bits(1) = 0 AND bits(2) = 1 AND bits(3) = 1 THEN pixel = rules(5): RETURN
IF bits(1) = 0 AND bits(2) = 1 AND bits(3) = 0 THEN pixel = rules(6): RETURN
IF bits(1) = 0 AND bits(2) = 0 AND bits(3) = 1 THEN pixel = rules(7): RETURN
IF bits(1) = 0 AND bits(2) = 0 AND bits(3) = 0 THEN pixel = rules(8): RETURN
RETURN

3000
FOR i = 1 TO 8
rules(i) = FIX(RND * 2)
NEXT i
GOSUB 1100
RETURN

4000
LINE (320, 1)-(320, 1), 1
RETURN



Yes, I wrote this code. Yes, the coding style stinks. Yes, Qbasic sucks eggs.
Feel free to copy, use, distribute, mangle, or beam into the cosmos.

Log in or register to write something here or to contact authors.