Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conway's game of life: remove duplicate code #4489

Open
thomasw-mitutoyo-ctl opened this issue Mar 31, 2021 · 0 comments
Open

Conway's game of life: remove duplicate code #4489

thomasw-mitutoyo-ctl opened this issue Mar 31, 2021 · 0 comments

Comments

@thomasw-mitutoyo-ctl
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant