Game of Life.

Basic and Machine Language

Moderator: Moderators

Post Reply
Shaun.Bebbington
Vic 20 Dabbler
Posts: 78
Joined: Fri Jan 20, 2006 3:41 am

Game of Life.

Post by Shaun.Bebbington »

I did a rough Game of Life simulator for the Sinclair ZX81 (last year or maybe the year before) so I decided to convert it from C to CBM BASIC 2.

Here's the resulting listing (I wrote it with CBM prg Studio):

Code: Select all

0 poke 36879,8:print"{clear}{green}";:gosub 63999:gosub 1:goto 5
1 for i=o to a:for j=o to b:g(i,j)=-((rnd(o)*x)<=e):next j,i:return
2 for i=z to h:for j=z to w:if g(i,j) then print c$;:goto 4
3 print s$;
4 next j,i:for i=o to a:for j=o to b:c=g(i,j):n=n+(c and g(i-o,j))+(c and g(i-o,j-o))+(c and g(i-o,j+o))+(c and g(i+o,j))+(c and g(i+o,j+o))+(c and g(i+o,j-o))+(c and g(i,j-o))+(c and g(i,j+o)):g(i,j)=-(n=d or n=e):n=z:next j,i
5 g=g+o:print t$g:get a$:on-(a$="")goto 2:print"{clear}":gosub 1:g=z:goto 5
63999 i=j=z:n=z:o=1:x=11:h=20:w=21:a=h-o:b=w-o:c=z:d=2:e=3:a$="":c$="0":s$=" ":t$="{home}generation:":dim g(h,w):return
If you're not fond of typing, the disk image is here. There is a compiled version which requires more RAM, I think 16K. Press the any key when the cells have stabilized to start again from generation one.

Updates from one generation to the next is quite slow, even with the compiled version, but I expected this. If nothing else it was an interesting challenge to reduce the number of lines etc...

Regards,

Shaun.
Without context, we are only data.
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Game of Life.

Post by wimoos »

Nice one!

I think optimizations below will make it somewhat faster:

Code: Select all

0 poke 36879,8:print"{clear}{green}";:gosub 63999:gosub 1:goto 5
1 for i=o to a:for j=o to b:g(i,j)=-((rnd(o)*x)<=e):next j,i:return
2 for i=z to h:for j=z to w:print chr$(32+16*g(i,j));:next j,i
3 for i=o to a:for j=o to b:if g(i,j) then n=g(i-o,j)+g(i-o,j-o)+g(i-o,j+o)+g(i+o,j)+g(i+o,j+o)+g(i+o,j-o)+g(i,j-o)+g(i,j+o):g(i,j)=-(n=d or n=e)
4 next j,i
5 g=g+o:print t$g:get a$:on-(a$="")goto 2:print"{clear}":gosub 1:g=z:goto 5
63999 i=j=z:n=z:o=1:x=11:h=20:w=21:a=h-o:b=w-o:c=z:d=2:e=3:a$="":t$="{home}generation:":dim g(h,w):return
Regards,

Wim.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Game of Life.

Post by Mike »

Shaun.Bebbington wrote:I did a rough Game of Life simulator for the Sinclair ZX81 (last year or maybe the year before) so I decided to convert it from C to CBM BASIC 2. [...]
I just tried it out.

I'm sorry to say that, but this one really doesn't implement Conway's Game Of Life. The rules governing survival, death and birth of cells need to be applied to all cells at once, not at a mixture of cells of the old generation 'below' the currently calculated cell, and new generation 'above' the currently calculated cell. I.e., you have to keep a matrix of the current generation, build the next generation in a second matrix, and only when it's finished, you transfer the second matrix into the first one, making the next generation the current one. Finally, at least in principle, GOL is played on an infinite plane, so you have to decide what happens when cells touch the border.

I'll have a go at a functioning version in BASIC, till later.
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Game of Life.

Post by Mike »

Here we go: (download)

Code: Select all

10 X=RND(-TI):CLR:DIMC$(23),N$(23):PRINT"{CLR}";
11 :
12 C$(0)="                       ":C$(23)=C$(0):N$(0)=C$(0):N$(23)=C$(0)
13 FORI=1TO22:N$="":FORJ=2TO22
14 R$=" ":IFRND(1)>.5THENR$="*"
15 N$=N$+R$:NEXT:N$(I)=" "+N$+" ":NEXT
16 :
17 PRINT"{HOME}";:FORI=1TO22:PRINTMID$(N$(I),2,21):C$(I)=N$(I):NEXT
18 :
19 FORI=1TO22:N$="":FORJ=2TO22
20 P= -(MID$(C$(I-1),J-1,1)="*")-(MID$(C$(I-1),J,1)="*")-(MID$(C$(I-1),J+1,1)="*")
21 P=P-(MID$(C$(I)  ,J-1,1)="*")                        -(MID$(C$(I)  ,J+1,1)="*")
22 P=P-(MID$(C$(I+1),J-1,1)="*")-(MID$(C$(I+1),J,1)="*")-(MID$(C$(I+1),J+1,1)="*")
23 R$=" ":IFP=3ORP=2ANDMID$(C$(I),J,1)="*"THENR$="*"
24 N$=N$+R$:NEXT:N$(I)=" "+N$+" ":NEXT:GOTO17
It implements GOL on a matrix with 24 rows and 23 columns. Only the middle 22 rows and 21 columns are displayed (so I don't have any hassles with scrolling), the one-cell border around the displayed area always consists of dead cells. Lines 12 to 15 create the initial generation, line 17 displays the matrix and copies the next to the current generation, and lines 19 to 24 calculate the next generation from the current one. When the display becomes static or just oscillatory, restart with STOP/RESTORE and RUN+{RETURN}.

Really no point in doing any 'fancy' optimizations here, like replacing constants with variables for faster access.

If you want to look at a faster version, also with slightly higher resolution (160x192 cells), just try out VIC Life.
Shaun.Bebbington
Vic 20 Dabbler
Posts: 78
Joined: Fri Jan 20, 2006 3:41 am

Re: Game of Life.

Post by Shaun.Bebbington »

Good work, thanks guys.

Regards,

Shaun.
Without context, we are only data.
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Game of Life.

Post by wimoos »

Based on Mikes' algorithm, probably the shortest and tightest Basic implementation. Of course, you can't beat the speed of an implementation in ML...

Code: Select all

1 PRINT"{CLR}":DIMP%(22,22),R%(22,22):FORI=1TO21:FORJ=1TO21:P%(I,J)=RND(1)*2:NEXT:NEXT
2 PRINT"{HOME}":FORI=1TO21:FORJ=1TO21:PRINTCHR$(32+10*P%(I,J));:R%(I,J)=P%(I,J):NEXT:PRINT:NEXT
3 Q=0:I=1:FORR=2TO22:S=0:J=1:FORT=2TO22:P=R%(I,S)+R%(I,T):FORK=STOT:P=P+R%(Q,K)+R%(R,K):NEXT
4 P%(I,J)=ABS(P=3ORP=2ANDR%(I,J)):S=J:J=T:NEXT:Q=I:I=R:NEXT:GOTO2
Lines 2 and 3 are longer than 88 characters. You'll have to fiddle with shorthand entry to get them in.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
Post Reply