10-15-2023, 08:15 AM
I am doing a demo of setting up Conways Game of Life at other forum and run into a problem getting proper neighbor counts of a cell with this code:
x and y are just in outer loops scanning the whole array to display on screen.
If I rework that first IF line to this:
All is well! Fine I can get the code to work as expected with 2nd block but I am not understanding what is going wrong with the first code example?
A professional Basic coder (non QB64 fan) said the logic is correct, has QB64 another glitch?
BTW he showed a much better method to do count without IF.
If anyone a connoisseur of methods it was this:
I switched from using -1's to using 1's for live cell (0 for dead)
As I recall my debug checks with the first block of code was messing up neighbor counts of live cells not that it should matter but with either -1 or 1 for live cell signal.
oh wait, I see it now, the logic is wrong because if I break the first IF line into 2 it becomes obvious why the counts were failing:
OK never mind, I got it now
Code: (Select All)
nc = 0
For yy = y - 1 To y + 1
For xx = x - 1 To x + 1
If (xx <> x) And (yy <> y) Then 'dont count cell(x, y) the cell whose neighbors we are counting
If Cells(xx, yy) Then nc = nc + 1 ' : Beep ' debug OK
End If
Next
Next
If I rework that first IF line to this:
Code: (Select All)
nc = 0
For yy = (y - 1) To (y + 1)
For xx = (x - 1) To (x + 1)
If xx = x And yy = y Then
'dont count cell(x, y) the cell whose neighbors we are counting
Else
If Cells(xx, yy) Then nc = nc + 1: Beep ' debug OK
End If
Next
Next
All is well! Fine I can get the code to work as expected with 2nd block but I am not understanding what is going wrong with the first code example?
A professional Basic coder (non QB64 fan) said the logic is correct, has QB64 another glitch?
BTW he showed a much better method to do count without IF.
If anyone a connoisseur of methods it was this:
Code: (Select All)
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)
As I recall my debug checks with the first block of code was messing up neighbor counts of live cells not that it should matter but with either -1 or 1 for live cell signal.
oh wait, I see it now, the logic is wrong because if I break the first IF line into 2 it becomes obvious why the counts were failing:
Code: (Select All)
nc = 0
For yy = y - 1 To y + 1
For xx = x - 1 To x + 1
If (xx <> x) Then
If (yy <> y) Then
'dont count cell(x, y) the cell whose neighbors we are counting
If Cells(xx, yy) Then nc = nc + 1 ' : Beep ' debug OK
End If
End If
Next
Next
OK never mind, I got it now
b = b + ...