This is my small, speedy implementation of Conways game of life.
I became obsessed with speed while writing this and this is the result.
The only rule that I set myself was 'no assembler', all PB.
As far as life itself is concerned, the rules can be found in other posts.
I offer this up for your amusement.
By the way, it is quite CPU intensive
Cheers
I became obsessed with speed while writing this and this is the result.
The only rule that I set myself was 'no assembler', all PB.
Code:
' Conway's Game of Life #COMPILE EXE "lifepbw8.exe" #DIM ALL #REGISTER ALL DECLARE FUNCTION IsWindow LIB "USER32.DLL" ALIAS "IsWindow" (BYVAL hWnd AS DWORD) AS LONG FUNCTION PBMAIN() AS LONG LOCAL w,h,ncw,nch,gwin,z,sum,rsx,rsy,x,y,w1,h1,gdc, totpixel AS LONG LOCAL tg(), ng() AS BYTE LOCAL tgptr, ngptr AS BYTE PTR w = 800 h = 600 totpixel = (w-1) * (h-1) DIM tG(totpixel) ' current generation DIM nG(totpixel) ' next generation RANDOMIZE TIMER ' reset the random number generator DESKTOP GET CLIENT TO ncw,nch GRAPHIC WINDOW "Look! - It's alive",(ncw-w)/2,(nch-h)/2,w,h TO gwin GRAPHIC ATTACH gwin,0,REDRAW GRAPHIC COLOR %WHITE,RGB(0,0,64) ' main program loop FOR y = w+1 TO totpixel - w ' seed this generation array randomly z = RND(1,17) ' change from 10 to whatever you like - 2 is too crowded IF z=1 THEN tG(y) = 1 ' between 10 and 50 odd gives a pleasing result NEXT y = totpixel - w ngptr = VARPTR(ng(0)) tgptr = VARPTR(tg(0)) DO IF IsWindow(gwin) = 0 THEN EXIT DO x = y DO IF @tgptr[x] THEN GRAPHIC SET PIXEL (x MOD w , x\w) sum = @tgptr[x-1-w] + @tgptr[x-w] + @tgptr[x+1-w] +_ @tgptr[x-1] + @tgptr[x+1] +_ ' this routine just adds up the number of occupied cells @tgptr[x-1+w] + @tgptr[x+w] + @tgptr[x+1+w] ' around the one of interest - tg(x,y) IF sum = 2 THEN @ngptr[x] = @tgptr[x] IF sum = 3 THEN INCR @ngptr[x] DECR x LOOP UNTIL x = 0 MAT tg()=ng() MAT ng() = ZER GRAPHIC REDRAW ' all done display the new page and return GRAPHIC CLEAR LOOP END FUNCTION
I offer this up for your amusement.
By the way, it is quite CPU intensive
Cheers
Comment