This implements in Liberty Basic the 2D cellular automaton invented by Conway and popularised via Scientific American.
In a grid (here I use 100x100) each cell takes values of 0 or 1.
Each cell has a 'neighbourhood' of 8, including up, down, sideways & diagonal neighbours ( Moore).
At each 'tick' of time, the rules are that you count how many neighbours are alive ( =1)
DEATHS 1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation. 2. Any live cell with more than three live neighbours dies, as if by overcrowding. 3. Any live cell with two or three live neighbours lives on to the next generation. BIRTHS 4. Any dead cell with exactly three live neighbours becomes a live cell.To implement this I need a second 2D array to hold the new calculated values, then copy them to the original array.
My only problem is that I am using a dll to convert bmp to gif (it's free for amateur use) and it pops up a nag screen each time it is called.
' Play Conway's LIFE on a 100x100 grid. ' Works in JB or LB ' Uses two 2D arrays for the present generation and calculated new one. ' To-dos include adding initial set-uup editor, pause, etc... ' Enable some of the commented-out lines & LB will convert the saved bmps to gifs ' which can then be assembled as an animated gif ' A good reaason to upgrade to LB! ' However this free dll needs you to click a confirm box for each conversion ' or of course buy the commercial version! ' NB I generally save to RAMdisk- change the file address to what you want, or add a selector. nomainwin WindowWidth = 700 WindowHeight = 630 UpperLeftX = 10 UpperLeftY = 10 graphicbox #w.graphicbox1, 10, 10, 502, 502 textbox #w.tb1, 520, 10, 160, 40 textbox #w.tb2, 10, 530, 500, 40 button #w.exit, "Exit", [quit], LR, 30, 10 dim display( 100, 100), scratch( 100, 100) global i open "Life- 2D cellular automaton" for window as #w #w, "trapclose [quit]" #w.graphicbox1, "down; fill white; flush ; size 1" #w, " font ms_sans_serif 12" #w.tb1, "!font ms_sans_serif 12 bold" #w.tb2, "!font ms_sans_serif 20" for x =0 to 100 #w.graphicbox1, "color darkblue ; line "; 5*x; " 0 "; 5*x; " 500" for y =0 to 100 #w.graphicbox1, "line 0 "; 5*y; " 500 "; 5*y scan display( x, y) =0 next y next x #w.graphicbox1, "size 4 ; flush" x =0 read name$ #w.tb2, " "; name$ while x <>100 read x, y if x <>100 then display( x, y) =1 wend [LWSS] data "Glider (lightweight spaceship LWSS)" data 21, 20 data 24, 20 data 20, 21 data 20, 22 data 24, 22 data 20, 23 data 21, 23 data 22, 23 data 23, 23 data 100, 100 for i =10 to 97 for j =40 to 97 display( i, j) =int( 2 *rnd(1)) next j next i for i =0 to 300 #w.tb1, " Generation "; i scan #w.graphicbox1, "discard" ' so we don't memory leak & have longer & longer redraws. ' SORRY- the gif conversion screen therefore messes it up. ' Display current states as 4x4 box centred in 5x5 grid area, ' so you can see a boundary between cells. for x =0 to 99 for y =0 to 99 if display( x, y) =0 then #w.graphicbox1, "color white" else #w.graphicbox1, "color black" #w.graphicbox1, "set "; 3 +5 *x; " "; 3 +5 *y scan next y next x 'Create new states in scratch for x =0 to 99 for y =0 to 99 scratch( x, y) =0 scan ' look at all immediate neighbours at sides and diagonally. ' Implement toroidal mapping so no 'edge artefacts'. if x= 0 then lx =99 else lx =x-1 ' x coord of cell to left if x=99 then rx = 0 else rx =x+1 ' x coord of cell to right if y= 0 then uy =99 else uy =y-1 ' y coord of cell uppermost above if y=99 then ly = 0 else ly =y+1 ' y coord of cell lowermost below ' find local # lv of neighbour cells lv = display( lx, y) +display( lx, uy) +display( lx, ly) ' three cells to left lv =lv +display( rx, y) +display( rx, uy) +display( rx, ly) ' three cells to right lv =lv +display( x, uy) +display( x, ly) ' two above & below if display( x, y) =1 then ' We're looking at a live cell... if ( lv <2) or ( lv >3) then scratch( x, y) =0 ' which may die of loneliness or overcrowding... else scratch( x, y) =1 ' ....or survive happily. end if end if if display( x, y) =0 then ' We're looking at a dead cell.... if lv =3 then scratch( x, y) =1 ' which comes alive if exactly 3 neighbours.... 'else 'scratch( x, y) =0 ' ....and otherwise stays dead. end if next y next x 'Copy scratch into display. for x =0 to 99 for y =0 to 99 scan display( x, y) =scratch( x, y) next y next x call savegif "" next i wait sub savegif h$ #w.graphicbox1 "getbmp drawing 1, 1, 503, 503" ver$ ="v" +right$( "000" +str$( i), 3) base$ ="R:\" ' (I use "R:\") bmpsave "drawing", base$ +"LifeJB" +ver$ +".bmp" ' a (soon deleted) bmp. a$ =base$ +"LifeJB" +ver$ +".bmp" b$ =base$ +"LifeJB" +ver$ +".gif" ' Enable following lines if you want to ' use this dll (in LB only) to change ' the bmps to gifs. open "re_imageconv" for dll as #d calldll #d, "ConvertImageToGif",_ a$ as ptr,_ b$ as ptr,_ result as long close #d kill a$ end sub [quit] close #w end