Question

I know only little how to make macros in Excel.

Was it helpful?

Solution

Should have done this a long time ago but here's my version of Conway's Life in excel.

Here's a hack of the code. By no means a perfect solution (didn't spend an age on this) but you might be able to pick some bits out.

Private arrGrid(100, 100) As Boolean
Private arrGridNextGeneration(100, 100) As Boolean

Private Sub PopulateParentArrayData()

For k = 1 To Sheet1.Range("C2:AM20").Cells.Count

If Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("A1").Interior.Color Then

    arrGrid(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) = True
Else
    arrGrid(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) = False
End If
DoEvents
Next
End Sub

Private Sub ApplyParentArrayData()

For k = 1 To Sheet1.Range("C2:AM20").Cells.Count

If arrGrid(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) Then

    Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("A1").Interior.Color
Else
    Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("B1").Interior.Color
End If
    DoEvents
Next

End Sub

Private Sub ApplyNextGenerationArrayData()

For k = 1 To Sheet1.Range("C2:AM20").Cells.Count

    If arrGridNextGeneration(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) Then

        Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("A1").Interior.Color
    Else
        Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("B1").Interior.Color
    End If
    DoEvents
Next

End Sub

Private Function GetNeighbourCount(ByVal pintRow As Integer, ByVal pintColumn As Integer) As Integer

Dim intCount As Integer

intCount = 0

For r = pintRow - 1 To pintRow + 1

For c = pintColumn - 1 To pintColumn + 1

If r <> pintRow Or c <> pintColumn Then
If arrGrid(r, c) Then

    intCount = intCount + 1
End If
End If
Next c
Next r

GetNeighbourCount = intCount

End Function

Private Sub PopulateNextGenerationArray()

Dim intNeighbours As Integer

For r = 0 To 100
For c = 0 To 100

If r > Sheet1.Range("C2:AM20").Rows(0).Row Then
If r <= Sheet1.Range("C2:AM20").Rows(Sheet1.Range("C2:AM20").Rows.Count).Row Then
If c > Sheet1.Range("C2:AM20").Columns(0).Column Then
If c <= Sheet1.Range("C2:AM20").Columns(Sheet1.Range("C2:AM20").Columns.Count).Column Then



 intNeighbours = GetNeighbourCount(r, c)
If arrGrid(r, c) Then
    'A1 cell
    If intNeighbours < 2 Or intNeighbours > 3 Then
        arrGridNextGeneration(r, c) = False
    Else
        arrGridNextGeneration(r, c) = True
    End If

Else
    'B1 cell
    If intNeighbours = 3 Then
        arrGridNextGeneration(r, c) = True
    Else
        arrGridNextGeneration(r, c) = False
    End If
End If
End If
End If
End If
End If
DoEvents
Next c
Next r

End Sub

Private Sub ActionLogic()


'Application.ScreenUpdating = False

PopulateParentArrayData
PopulateNextGenerationArray
ApplyNextGenerationArrayData

'Application.ScreenUpdating = True
End Sub

To get this to work just make the background of cell A1 black, the background of cell B1 white and then add some black backgrounds in the range C2:AM20 and run the ActionLogic method.

OTHER TIPS

You can find many examples through Google.

The very first result is a post from David Gainer's blog that uses Conway’s Game of Life to teach about circular reference formulas and iteration (no VBA involved):

http://blogs.office.com/2007/11/02/iteration-conways-game-of-life

You will need two macros. The first one should format the game sheet so the cells are square.

Have the user run this macro. After that she should enter a 1 for each cell that is alive. Use conditional formatting to turn the cell completely black (background = black if value != 0)

Now have a second macro which calculates the next step in a background sheet (another sheet). Use relative cell positioning (relative to ActiveCell) and two nested loops. When this is done, copy all values from the background sheet to the game sheet.

Search for it and look at their code. Plenty of people have made it a hobby to make full games in Excel.

Ex: http://www.geocities.jp/nchikada/pac/

Why do you say Excel is the wrong choice?

I think Excel is the best way to solve this:

Excel solves this with 1 line: IF(OR(SUM(B2:D4)-C3=3,AND(SUM(B2:D4)-C3=2,C3=1)),1,0)

*where the above is an expression that returns the next generation value for the cell C3.

Here's the demo: https://docs.google.com/open?id=0B4FcWULw3iQidlZSdG9GRDh0TXM

If you're in a situation where you have to implement this sort of things from scratch, then functional programming is the best way to go. Otherwise, Excel works really well. Why? Because Excel is a system that forces you to enter only pure functions. You see, the key to simulating this Game of Life is to realize that each state of the cells is a PURE FUNCTION of the previous state. Excel naturally forces you to think this way.

another tutorial on circular references in excel can be found here: http://chandoo.org/wp/2009/01/08/timestamps-excel-formula-help/

this one explains how you can insert timestamps using circular references.

Excel is definitely the wrong choice for this kind of a problem. As to how it would be possible: First learn about the game of life and then visual basic to use in Excel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top