dotnet / dotnet/samples

Conway's game of life: remove duplicate code

Open
#4,489 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
5.1k
Avg merge
9h 11m
Merged PRs (30d)
1

Description

## 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.