QB64: No Frills Conway’s Game of Life

Posted on at


Decided to try my hand at QB64, so I started with a very simple Conway’s Game of Life program.

When I say no frills that’s because its text based, you can’t edit the starting cells (without editing the code) and is not exactly the nicest looking or most efficient code. But it does work! And that is what makes me happy.

’ Game of Life.

’ Expiriment in cellular automatons.

’ Version 001

’ By Matthew Macomber

PRINT “Game of Life - Simple Text Version”

DIM SHARED grid1(20, 20)

DIM SHARED grid2(20, 20)

FOR y = 0 TO 19

   FOR x = 0 TO 19

       grid1(y, x) = 0

   NEXT x

NEXT y

’ Set some test cells, basic alternating 2 step

grid1(5, 5) = 1

grid1(6, 4) = 1

grid1(6, 5) = 1

grid1(6, 6) = 1

grid1(7, 5) = 1

INPUT “How many ticks do you want the sim to run?”; runTime

FOR z = 1 TO runTime

   IF current = 1 THEN

       current = 2

   ELSE

       current = 1

   END IF

   IF cellTick(current) THEN

       CLS

       PRINT “Running…”

   END IF

NEXT z

CLS

PRINT (1 MOD 2) + 1

PRINT (2 MOD 2) + 1

PRINT (3 MOD 2) + 1

PRINT (4 MOD 2) + 1

INPUT “Waiting”, fake

IF printGrid((runTime MOD 2) + 1) THEN

   PRINT “Run completed”

END IF

FUNCTION cellTick (a)

IF a = 1 THEN

   FOR y = 0 TO 19

       FOR x = 0 TO 19

           ’ Test if cell is alive or dead. grid1 to grid2

           cellCount = 0

           IF y - 1 >= 0 AND x - 1 >= 0 THEN

               IF grid1(y - 1, x) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid1(y - 1, x - 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid1(y, x - 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid1(y + 1, x - 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid1(y + 1, x) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid1(y + 1, x + 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid1(y, x + 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid1(y - 1, x + 1) THEN

                   cellCount = cellCount + 1

               END IF

           END IF

           IF grid1(y, x) = 1 THEN

               IF cellCount < 2 THEN

                   grid2(y, x) = 0

               END IF

               IF cellCount > 3 THEN

                   grid2(y, x) = 0

               END IF

               IF cellCount = 2 OR cellCount = 3 THEN

                   grid2(y, x) = 1

               END IF

           ELSE

               IF cellCount = 3 THEN

                   grid2(y, x) = 1

               END IF

           END IF

       NEXT x

   NEXT y

   ERASE grid1

ELSE

   FOR y = 0 TO 19

       FOR x = 0 TO 19

           ’ Test if cell is alive or dead. grid2 to grid1

           cellCount = 0

           IF y - 1 >= 0 AND x - 1 >= 0 THEN

               IF grid2(y - 1, x) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid2(y - 1, x - 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid2(y, x - 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid2(y + 1, x - 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid2(y + 1, x) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid2(y + 1, x + 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid2(y, x + 1) = 1 THEN

                   cellCount = cellCount + 1

               END IF

               IF grid2(y - 1, x + 1) THEN

                   cellCount = cellCount + 1

               END IF

           END IF

           IF grid2(y, x) = 1 THEN

               IF cellCount < 2 THEN

                   grid1(y, x) = 0

               END IF

               IF cellCount > 3 THEN

                   grid1(y, x) = 0

               END IF

               IF cellCount = 2 OR cellCount = 3 THEN

                   grid1(y, x) = 1

               END IF

           ELSE

               IF cellCount = 3 THEN

                   grid1(y, x) = 1

               END IF

           END IF

       NEXT x

   NEXT y

   ERASE grid2

END IF

END FUNCTION

FUNCTION printGrid (a)

IF a = 1 THEN

   FOR y = 0 TO 19

       FOR x = 0 TO 19

           PRINT grid1(y, x);

       NEXT x

       PRINT “”

   NEXT y

ELSE

   FOR y = 0 TO 19

       FOR x = 0 TO 19

           PRINT grid2(y, x);

       NEXT x

       PRINT “”

   NEXT y

END IF

END FUNCTION

I enjoyed playing with QB64, I tested it and works on Linux and Windows versions of QB64.



About the author

160