Post by bplus on Oct 15, 2023 8:10:12 GMT -5
Check out qb64.boards.net/thread/224/basic-computer-games?page=2
To see how I got from this post:
To this fully functional coded app with 3 different seeding methods (the best is not commented out):
PS Thankyou zxdunny for a more effective method of counting neighbors.
To see how I got from this post:
Morristown NJ code isn't even QB, it doesn't have If ... Then blocks (more than 1 line of code that ends with End If), it doesn't even have Else (on one line)! It does have a nightmare of Goto stuff!
Best to start from scratch if you know what you want to do and write it in QB.
Just get the jist of what the code is doing and then write up a more updated version.
The jist of Life is to set up an array of on / off cells and display, then the next generation is made by counting neighbors of each cell and according to life rules that cell lives, dies or is born or remains unborn, ie just track what lives! Keep next generation array separate from last until all the new array is filled from last then copy, display and repeat...
Then it is just a matter of how to start the thing off with a "seed" generation. Start with simple patterns like lines or boxes. Morristown is really tedious to input * and spaces! Practice coding skills by making patterned seeds of lines of different lengths or boxes or maybe T shapes... Play with birth and death rules see what happens.
Do you want to be an expert at getting old code to work in newer PL or do you want to be expert in coding what you envision?
Best to start from scratch if you know what you want to do and write it in QB.
Just get the jist of what the code is doing and then write up a more updated version.
The jist of Life is to set up an array of on / off cells and display, then the next generation is made by counting neighbors of each cell and according to life rules that cell lives, dies or is born or remains unborn, ie just track what lives! Keep next generation array separate from last until all the new array is filled from last then copy, display and repeat...
Then it is just a matter of how to start the thing off with a "seed" generation. Start with simple patterns like lines or boxes. Morristown is really tedious to input * and spaces! Practice coding skills by making patterned seeds of lines of different lengths or boxes or maybe T shapes... Play with birth and death rules see what happens.
Do you want to be an expert at getting old code to work in newer PL or do you want to be expert in coding what you envision?
To this fully functional coded app with 3 different seeding methods (the best is not commented out):
Option _Explicit
_Title "Basic Life Proggie spcebar = next generation, r = restart line seed"
' bplus demo start 2023-10-14
Dim As Long XPixels, YPixels
XPixels = 810 ' so we can do perfect symmetry
YPixels = 610 ' ditto
Screen _NewImage(XPixels, YPixels, 12) ' the 12 only allows 16 colors
_ScreenMove 200, 60
Dim As Long CellSquare, XCells, YCells, x, y, xx, yy, nc
CellSquare = 10
XCells = XPixels \ CellSquare
YCells = YPixels \ CellSquare
'Print XCells, YCells, Int(YCells / 2 + .5)
' redim for easy clear, Erase was giving me smeg
ReDim As Long Cells(XCells + 1, YCells + 1)
Dim As Long NextGeneration(XCells + 1, YCells + 1)
' Then it is just a matter of how to start the thing off with a "seed" generation.
' Start with simple patterns like lines or boxes.
' Morristown is really tedious to input * and spaces!
' Practice coding skills by making patterned seeds of lines of different lengths
' or boxes or maybe T shapes... Play with birth and death rules see what happens.
' seed a 3 cell blinker
'For x = 40 To 42
' Cells(x, Int(YCells / 2 + .5)) = 1
'Next
'
restart:
' line seeder for Nice symmetry! use r to restart at different lengths
Dim As Long rl, midx
midx = Int(XCells / 2 + .5)
rl = Int(Rnd * (midx - 2)) + 1
For x = midx - rl To midx + rl
Cells(x, Int(YCells / 2 + .5)) = 1
Next
'For x = 40 To 42
' Cells(x, Int(YCells / 2 + .5)) = 1
'Next
' random seeder
'For x = 1 To 1000
' Cells(Int(Rnd * XCells) + 1, Int(Rnd * YCells) + 1) = 1
'Next
Do
' The jist of Life is to set up an array of on / off cells and display,
Cls
'Display
For y = 1 To YCells
For x = 1 To XCells
If Cells(x, y) Then
Line ((x - 1) * CellSquare, (y - 1) * CellSquare)-Step(CellSquare, CellSquare), 15, BF
Line ((x - 1) * CellSquare, (y - 1) * CellSquare)-Step(CellSquare, CellSquare), 0, B
End If
' We might as well do our neighbor counts while display array
' and we will need a NextGeneration Array, same size as Cells,
'set up to load when we follow Life Rules of Birth, Live Death which are:
' then the next generation is made by counting neighbors of each cell
nc = 0
For yy = y - 1 To y + 1
For xx = x - 1 To x + 1
nc = nc + Cells(xx, yy) ' no ifs
Next
Next
nc = nc - Cells(x, y) ' just remove extra count if not 0
' and according to life rules that cell lives, dies or is born or remains unborn,
'A live cell with 2 or 3 neighbors lives on
'A dead cell with 3 neighbors is born
'The rest die or remain unborn.
If Cells(x, y) And (nc = 2) Then
NextGeneration(x, y) = 1
ElseIf Cells(x, y) And (nc = 3) Then
NextGeneration(x, y) = 1
ElseIf (Cells(x, y) = 0) And (nc = 3) Then
NextGeneration(x, y) = 1
Else
NextGeneration(x, y) = 0
End If
Next
Next
' Keep next generation array separate from last until all the new array is filled
' from last then copy and repeat...
' get cells ready for next generation display
For y = 1 To YCells
For x = 1 To XCells
Cells(x, y) = NextGeneration(x, y)
Next
Next
_Display
_Limit 30
If InKey$ = "r" Then ReDim Cells(XCells + 1, YCells + 1): GoTo restart
Sleep
Loope
PS Thankyou zxdunny for a more effective method of counting neighbors.