Game Of Life 10Liner

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
bola_dor
Posts: 398
Joined: Wed Oct 02, 2019 5:32 am

Game Of Life 10Liner

Post by bola_dor »

I've just submitted a 10 Lines version of the Conway's Game of Life to the BASIC 10Liner Contesthttps://gkanold.wixsite.com/homeputerium/rules2021 (still not released)
The program uses some sort of speed optimization selecting in advance and on the same loop which cells to evaluate or not.
I am not a professional programmer, not even an informatics related professional, sometimes I just code for fun as I did 35 years ago. And this was fun, and stressful at the same time :D ..
I was stuck at 11 lines many times (some times there were 18 ;) ).. and had to rewrite it several times to work around how to perform several tasks on a single line..
I am happy with the results.. deppendin on the initial pattern on line 20 it can take from 30" to over a minute per generation. You can modify it to test different ones as the found at https://en.m.wikipedia.org/wiki/Conway%27s_Game_of_Life
It only uses the upper half of the screen just to speed up the action.
Ofcourse it can be further optimized for speed using more lines.. line 70 for example is not only too long but it calculates the number of neighbors two times when it could have been done once with an extra line before..
I learned a lot with this.
I hope you like it

Code: Select all

  10 DIM A$(1536)
  20 LET A$(99 TO 286)="%   %                                 %                            %    %                             % % % % "
  30 PRINT AT 0,0;A$(1 TO 384)
  40 FOR F=34 TO 351
  50 IF A$(F+64)="% " THEN LET A$(768+F+31 TO 768+F+97)="111"+A$(768+F+34 TO 768+F+62)+"111"+A$(768+F+66 TO 768+F+94)+"111"
  60 IF A$(768+F)<>"1" THEN GOTO 80
  70 IF (A$(F)="% " AND CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=256) OR CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=384 THEN LET A$(384+F)="% "
  80 NEXT F
  90 LET A$=A$(385 TO 768)+A$(1152 TO 1536)+A$(1152 TO 1536)
 100 GOTO 30
Questions, comments & critics are welcome!!
Ernesto
ZX80 USA, ZX81UK, ZX Spectrum, ZX Spectrum+, ZX Spectrum 128+ UK, ZX Spectrum +2/A, Sinclair QL, CZ1000, CZ1500, CZ2000, CZ1000Plus, CZ1500Plus, CZ Spectrum, CZ Spectrum Plus, TK83, TK85, TK90X, TK95. TS2068. And more to come :D
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Game Of Life 10Liner

Post by XavSnap »

Hi Nice code.

Just a speed optimisation:

Code: Select all

  60 IF A$(768+F)<>"1" THEN GOTO 80
  70 IF (A$(F)="% " AND CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=256) OR CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=384 THEN LET A$(384+F)="% "
  80 NEXT F
60 IF A$(768+F)<>"1" THEN NEXT F

or

Code: Select all

60 IF A$(768+F)<>"1" THEN IF (A$(F)="% " AND CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=256) OR CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=384 THEN LET A$(384+F)="% "
I tried to move the D_File to DEST (vas) value, but the variable decays are corrupt… and the program length is too long... we had to reconstruct the d_file in an ASM routine (1 REM and 1 RAND USR), but the program won't be a "pure" basic program.

Code: Select all

10 DIM A$(1536)
11 POKE 16396,PEEK 16402+5
12 POKE 16397,PEEK 16403
13 LET A$(1 TO 8)=CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
bola_dor
Posts: 398
Joined: Wed Oct 02, 2019 5:32 am

Re: Game Of Life 10Liner

Post by bola_dor »

XavSnap wrote: Sat Mar 27, 2021 8:03 am Hi Nice code.
Hi Xavier, thank you, is always good to read you..
Just a speed optimisation:

Code: Select all

  60 IF A$(768+F)<>"1" THEN GOTO 80
  70 IF (A$(F)="% " AND CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=256) OR CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=384 THEN LET A$(384+F)="% "
  80 NEXT F
60 IF A$(768+F)<>"1" THEN NEXT F
I tried that but it can lead to a error ant the end of the loop executing line 70.. its faster I thought leaving it as the occurrence is really rare but... finally I opted for the academic way..
or

Code: Select all

60 IF A$(768+F)<>"1" THEN IF (A$(F)="% " AND CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=256) OR CODE A$(F-33)+CODE A$(F-32)+CODE A$(F-31)+CODE A$(F-1)+CODE A$(F+1)+CODE A$(F+31)+CODE A$(F+32)+CODE A$(F+33)=384 THEN LET A$(384+F)="% "
Oh!!! That's a good one!!! I think that when code at last was simplified I didn't realize that I was jumping a single line... that line was made in two separated ones, the first performing the addition and assigning to a variable and the second using that variable.. 😊🙏
I tried to move the D_File to DEST (vas) value, but the variable decays are corrupt… and the program length is too long... we had to reconstruct the d_file in an ASM routine (1 REM and 1 RAND USR), but the program won't be a "pure" basic program.

Code: Select all

10 DIM A$(1536)
11 POKE 16396,PEEK 16402+5
12 POKE 16397,PEEK 16403
13 LET A$(1 TO 8)=CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118
well I am lost here... what's the goal of moving the D_File? Can you please give me some explanation? And how does that routine works.. my skills in MC are really scarce
Thank you!!
Ernesto
ZX80 USA, ZX81UK, ZX Spectrum, ZX Spectrum+, ZX Spectrum 128+ UK, ZX Spectrum +2/A, Sinclair QL, CZ1000, CZ1500, CZ2000, CZ1000Plus, CZ1500Plus, CZ Spectrum, CZ Spectrum Plus, TK83, TK85, TK90X, TK95. TS2068. And more to come :D
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Game Of Life 10Liner

Post by XavSnap »

I ever code under the DEST/D_file system…

The A$ variable IS the screen mirror, and you dont had to Print the value on the screen !
a LET A$(55)="*" directly print a "*" on the screen at the Vars A$ address.
It speed up the screen display, and avoid all PRINT AT... commands.
I will code a demo to show you it…
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Game Of Life 10Liner

Post by XavSnap »

The D_File moved to the vars… (demo)

Code: Select all

1 DIM A$(60)
3 LET DF1=PEEK 16396
4 LET DF2=PEEK 16397
5 GOSUB 100
6 PRINT AT 10,5;"hello\::world...";AT 15,15;"AGAIN ?";AT 0,SIN SIN SIN SIN SIN SIN SIN SIN 0
10 GOTO 5
100 LET C$=CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118+CHR$ 118
105 LET A$=C$+C$+C$+C$+C$+C$+C$+C$+C$+C$+C$+C$
106 LET A$(1)=A$(1)
110 POKE 16396,PEEK 16402
120 POKE 16397,PEEK 16403
130 LET B$="** THIS IS A DEMO TO DISPLAY A STRING VARIABLE ON THE SCREEN, WITHOUT A PRINT COMMAND **\::press\::a\::key\::"
200 LET B$=B$(2 TO )+B$(1)
300 LET A$(3 TO 36)=B$
400 IF INKEY$="" THEN GOTO 200
450 POKE 16396,DF1
480 POKE 16397,DF2
500 RETURN
Cap0000.jpg
Cap0000.jpg (8.04 KiB) Viewed 1561 times
:shock:
No PRINTs and 34 columns !

106 LET A$(1)=A$(1) > fixe the LAST_k address @ the fist character (must have 2 $76(128) chars to start the display)
110 POKE 16396,PEEK 16402 > Move the D_file to the A$ variable
120 POKE 16397,PEEK 16403 >

DISPLAY.P
(1.82 KiB) Downloaded 126 times
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Post Reply