samples icon indicating copy to clipboard operation
samples copied to clipboard

Conway's game of life: remove duplicate code

Open thomasw-mitutoyo-ctl opened this issue 4 years ago • 0 comments

Conway's game of life: remove duplicate code

Animation.vb, surrounding() and grow() have duplicate code. How about loops?

The code below could be further simplified by just storing 0 and 1 in the grid. However, I have not yet figured out why you store different integers in there.

    Private Function surrounding(r As Integer, c As Integer) As Integer
        Dim count As Integer = 0
        If _cellValues(r)(c) > 0 Then
            count = -1 ' Do not consider the cell itself
        End If 

        For dr As Integer = -1 To 1 ' surrounding rows
            For dc As Integer = -1 To 1 ' surrounding columns
                If r + dr > 0 And r + dr < 99 Then ' bounds check rows
                    If c + dc > 0 And c + dc < 99 Then ' bounds check column
                        If _cellValues(r + dr)(c + dc) > 0 then
                            count += 1
                        End If
                    End If
                End If
            Next
        Next
        Return count
    End Function

Issue metadata

  • Issue type: sample-update

thomasw-mitutoyo-ctl avatar Mar 31 '21 14:03 thomasw-mitutoyo-ctl