Web page: http://www.geocities.ws/alvalongo/c64/basic/Life_in_C64.html
A small Conway's Game of Life implementation
written for Commodore 64 using built-in BASIC
V2
in 10 lines of code
BASIC 10 liner contest 2019 by
Homeputerium
Category: "EXTREME-256"
Entry · #70
Tweeter:
@Basic10L on Twitter
Other web pages:
2019 BASIC 10-Liner Contest in On!
Emulator:
VICE emulator v2.2 for Windows
The simple
rules of Game of life are:
·
1- If the cell is alive, then it stays
alive if it has either 2 or 3 live neighbors.
·
2- If the cell is dead, then it springs
to life only in the case that it has 3 live neighbors.
This
simple rules lead to intense computational and long runs as board is bigger, as
every cell on the board has to look its sorrounding 8 cells.
A 10
x 10 board has 100 cells and 100 x 8 = 800 cells to look up.
A 15
x 15 board has 225 cells and 225 x 8 = 1800 cell to look up.
Commodore
BASIC isn’t fast enough to calculate next generation and display a smooth
animation , so I decided first calculate all generations until (control by TX
variable) saving the result on a bidimensional array (a$) and then play every
generation in sequence..
Every
row in the array is a string of CX+2 length at tick T
The program is named “life64.prg”.
If the unit 9 is configure as a directory, load
program typing the command:
load”life64.prg”,9
and press RETURN
And then type
List 10
and press RETURN
The program will run with this parameters:
CX is set 15, fro 15 rows
RX is set 15, for 15 columns
TX is set 50 for 50 ticks
Then type
Run
and press RETURN
The program starts at tick 0 (zero) and calculate next
tick row by row column by colum
Every star character is a alive cell
Tick is 0
At tick one is:
At tick seven
At tick 31:
After all ticks are calculate the program play endless
every tick in a smooth animation.
Line
90 has the initial generation, on row and column pairs.
End
is signaling by two or more consecutive “0” (zero) values:
About design
Commodore
BASIC V2 logical operacions give numeric values:
-1
(minus one) for TRUE values
0
(cero) for FALSE values
It
can compute algebraic expresions with logical operations like
L=(B=1)
So if
varible B is 1 (one) then L gets -1
If
varible B is another number then L gets 0
You
can write algebraic expression mixed with logical expressions, as:
K-3*(B=2)
If K is 10, and B is 2 then the result is 13
If K is 10 and B is 10 then the result is 10
With this feature I was able to skip a lot of IF-THEN
sentences and write down a long algebraic expression evaluating all 8 neighbors
around a cell.
If a cell has coordinate Y (for row) and X
(for column):
Its 8 neighbors have the following coordinates:
It was assume that all cells around the board are
always in “dead” state
Programming tools
Development and testing was on a Windows 2000
Professional SP4, spanish language
1-Editor
Crimson Editor
Version: 3.70 Release
2-Convertion from text to tokenized Commodore BASIC V2
tool: C64List
Version: v3.03 by Jeff Hoag
https://www.commodoreserver.com/BlogEntryView.asp?EID=DB1EC7391247402997A20810B1ACED71
3.Emulator
VICE, Versatil Commodore Emulator
Version: 2.2
Description of program:
Source: life64.bas
10 print "{clear}{white}life on
64":cx=12:rx=12:tx=10:d2=-2:d3=-3:u=1:e=2:z=0:t=z
Clear
screen and set white color.
Initialize
variables:
CX maximun number of columns
RX maximum number of rows
TX maximim ticks generations
D2 numeric value minus 2, for rule 1
D3 numeric value minus 3 for rule 1 and 2
U numeric value 1 (one)
E numeric value 2 (two)
Z numeric value 0 (zero)
T tick value for generations, init with
zero
220 v$="*":c$(z)=" ":c$(u)=v$:dim
a$(rx+u,tx):for i=z to rx+u:a$(i,t)="["+
left$("
",cx)+"]":next i
V$ aliVe state, using a
star character
C$(z) Dead, using a space charater
on position 0 of array c$
C$(u) Alive, using a star
character on position 1 of array C$
A$ Bidimensional array for
board of RX+U rows and TX generations
Initializa every row of array A$ at time 0, with dead cells. every row
is a string of CX+2 characters, because the board has around all dead cell
30 read r,c:if r>=u and c>=u then
c=c+u:a$(r,t)=mid$(a$(r,t),u,c)+v$+mid$(a$(r,t),c+e): goto 30
Read tuples
of row and column coordinates.
If they are
greater o equal than one then modify string on this row R so the column C has a
alive cell.
It’s added
one because the most left cell and the most right cell are border cell outside
main board,
40 40 for t=z to
tx-u:gosub 90:for y=u to rx:l$="[":for x=e to cx+u
Start of
main loop, time is from 0 to max time minus one
GOSUB 900,
for draw the board
Y is for
rows
L$ is for
the new line o row, initialize a “[“ character because is the cell outside main
board.
X is for
columns, from 2 to CX plus 2
50 print
"{home}t=";str$(t);">r=";str$(y);",c=";str$(x);" ":p=x+u:q=y+u:r=x-u:s=y-u
Write number of T generation,
and Y row and X column
Initialize variables for neighbors cells.
60
k=(mid$(a$(s,t),x,u)=v$)+(mid$(a$(s,t),p,u)=v$)+(mid$(a$(y,t),p,u)=v$)
+(mid$(a$(q,t),p,u)=v$)+(mid$(a$(q,t),x,u)=v$)+(mid$(a$(q,t),r,u)=v$)
+(mid$(a$(y,t),r,u)=v$)+(mid$(a$(s,t),r,u)=v$)
Every neighbor cell, total of 8, is compare agaings the
alive value in variable V$
If is alive then the value es –1 (minus one)
At then end variable K has a
value between 0 (zero) and –8 (minus eight)
70
w$=mid$(a$(y,t),x,u):l$=l$+c$((w$=v$)*((k=d2)+(k=d3))+(w$<>v$)*(k=d3)):next
x:a$(y,t+u)=l$+"]":next y:next t:print"{clear}";
Varible W$ gets the current
state at time T of cell at Y row and X column
(w$=v$)*((k=d2)+(k=d3), this is for rule 1
(w$=v$), the cell is alive?
(k=d2)+(k=d3), the count is –2 or –3
if both are TRUE then the net
value es 1 positive
(w$<>v$)*(k=d3), this is for rule 2
(w$<>v$), the cell is dead?
(k=d3), the total of alive cell is –3?
if both are TRUE then the net
value es 1 posotive
From the array C$, get the
next cell state, if 0 then space character, if 1 then star character-
next x,
go to next column
a$(y,t+u)=l$+" ", write new row in the next time T+1
next y, next row
next t, next generation
80 print
"{clear}":for t=z to tx:gosub 90:next t:for b=z to 300:next b:goto 80
A loop to display the board
at time T
for b=z to 300:next b, is a
delay
90 print
"{home}t=";str$(t);"
":for i=u to rx:print "{rvrs on}";a$(i,t):next i:return
A subroutine to display a
board at tick T, printing every row
It prints the left and right
borders of board.
100 data
1,2,1,3,2,2,2,3,3,4,3,5,3,11,4,4,4,5,4,10,4,11,5,11,9,12,9,15,10,11,
11,11,11,15,12,11,12,12,12,13,12,14,13,2,13,3,13,4,14,4,15,3,0,0
Data for generation zero, row
and column coordinates
Signal end of data with two
or more zero values.
To calculate this coordinates
of ticke zeto I write a amall program lifegrid.bas
This is for a board of 15
rows and 15 columns
Data on line 300 is a ruler
Data for roe 1 is line 301
and so on
An star is a alive cell
2019-04-29, Life_in_c64_v101-c104.doc, by alvalongo