Game of Life simulation!

Christopherx

Well-known member
Joined
Jul 4, 2010
Messages
58
Programming Experience
Beginner
VB.NET:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim x, y As Integer

        For y = 1 To DataGridView1.Rows.Count - 2
            For x = 1 To DataGridView1.Columns.Count - 2
                changed = True
                count = 0
                If DataGridView1.Rows(y - 1).Cells(x).Style.BackColor = Color.Green Then
                    count = count + 1
                End If
                If DataGridView1.Rows(y + 1).Cells(x).Style.BackColor = Color.Green Then
                    count = count + 1
                End If
                If DataGridView1.Rows(y).Cells(x - 1).Style.BackColor = Color.Green Then
                    count = count + 1
                End If
                If DataGridView1.Rows(y).Cells(x + 1).Style.BackColor = Color.Green Then
                    count = count + 1
                End If
                If DataGridView1.Rows(y - 1).Cells(x - 1).Style.BackColor = Color.Green Then
                    count = count + 1
                End If
                If DataGridView1.Rows(y + 1).Cells(x + 1).Style.BackColor = Color.Green Then
                    count = count + 1
                End If
                If DataGridView1.Rows(y - 1).Cells(x + 1).Style.BackColor = Color.Green Then
                    count = count + 1
                End If
                If DataGridView1.Rows(y + 1).Cells(x - 1).Style.BackColor = Color.Green Then
                    count = count + 1
                End If



                If count < 2 Then
                    DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.White
                End If

                If count = 2 Or count = 3 And DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.Green Then
                    DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.Green
                End If

                If count > 3 Then
                    DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.White
                End If

                If count = 3 And DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.White Then
                    DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.Green
                End If


            Next
        Next
        gen = gen + 1
        Me.Text = "Generation:" & gen
        population = 0
    End Sub

So basically i've tried to simulate life using Conway's method, and i've decided that using a datagridview would be easy. The idea is I click the cells at the beginning to set the "seed" and then I click this button, and this code is ran. However, objects called still lives are exploding into crazy patterns, and that shouldn't happen, meaning my code is wrong somewhere. Can anyone help me find the problem? thankyou

edit: White represents "dead cells", green represents live.
 
Last edited:
You don't find errors in code just by reading the code. That's the first thing you do but, if that doesn't work, it's time to debug. The Visual Studio IDE gives you lots of tools to help.

First, place a breakpoint at the top of the code section you're having trouble with. Do that using the F9 key. Now run your project and the debugger will break at that point.

Next, step through your code line by line. You do that using the F10 key. To step into a method call you use F11 and to step out of the current method you use Shift+F11.

Before each step, you need to determine exactly what you expect to happen. After each step you need to determine whether what you expected actually did happen. If it did then you go to the next step. If it didn't then you have found an issue. You then need to determine whether it was your expectation that was incorrect or the behaviour of the code. The IDE provides various tools to help you carry this out, including the Autos, Locals, Watch and Immediate windows.
 
I have been using these to check the values of the Count variables, and this helps me determine which square im looking at. This is all in working order, and I cant seem to see why it won't work. I'm truely, very confused!
 
All cells in a generation is to change simultaneously. That means if you change one cell you will be unable to calculate other cells because the premises for that generation is already lost. "Typically" the algorithm for this game uses two arrays, one for the current generation and one for the next. Each cell in next generation is calculated based on the state of neighbors in current generation. You could do it by setting the next state in the cells Tag in one loop over, then change the state of all cells in another loop.

There is also one logical error, this code:
VB.NET:
If count = 2 Or count = 3 And DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.Green Then
reads like this
VB.NET:
If count = 2 Or (count = 3 And DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.Green) Then
when your intention was (or should be :)) this
VB.NET:
If (count = 2 Or count = 3) And DataGridView1.Rows(y).Cells(x).Style.BackColor = Color.Green Then
This is because AND operator has a higher precedence than the OR operator: Operator Precedence in Visual Basic
 
Back
Top