Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is my Logic wrong or QB64's ?
#1
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:
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
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:
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)
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:
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 Smile
b = b + ...
Reply


Messages In This Thread
Is my Logic wrong or QB64's ? - by bplus - 10-15-2023, 08:15 AM
RE: Is my Logic wrong or QB64's ? - by SMcNeill - 10-15-2023, 09:20 AM
RE: Is my Logic wrong or QB64's ? - by bplus - 10-15-2023, 10:38 AM
RE: Is my Logic wrong or QB64's ? - by mnrvovrfc - 10-15-2023, 11:24 AM
RE: Is my Logic wrong or QB64's ? - by SMcNeill - 10-15-2023, 11:39 AM
RE: Is my Logic wrong or QB64's ? - by bplus - 10-15-2023, 11:50 AM
RE: Is my Logic wrong or QB64's ? - by SMcNeill - 10-15-2023, 12:13 PM
RE: Is my Logic wrong or QB64's ? - by bplus - 10-15-2023, 12:21 PM
RE: Is my Logic wrong or QB64's ? - by bplus - 10-15-2023, 04:56 PM



Users browsing this thread: 6 Guest(s)