views:

2373

answers:

6

I know only little how to make macros in Excel.

+1  A: 

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/

alphadogg
That is just terrible. All that time spent writing horrendous VBA to manipulate cell colours. Just because you can do something doesn't mean you should.
danio
It's just for fun. Why get so down on it?
alphadogg
It seems so backwards. Just using the cells of excel as if they are pixels. Could have done something really different with all that time. At least doing game of life makes use of some of the excel characteristics such as functions based on the contents of other cells.
danio
A: 

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.

Georg
I'm really curious if it wasn't possible to implement this using only the cell value resolver of Excel. :> I mean the value of each cell is a function of it's neighbors, right?
Aaron Digulla
I think you're referring to the wrong game of life. I think this is the one he means http://www.math.com/students/wonders/life/life.html
ferrari fan
Georg
+4  A: 

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.

Aaron Digulla
+6  A: 

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.msdn.com/excel/archive/2007/11/02/iteration-conway-s-game-of-life.aspx

jtolle
That was searching on "excel" and "game of life". Impressively, searching on "How can you make Game of life in Excel?" returns this stackoverflow question that is less than an hour old.
jtolle
A: 

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.

+3  A: 

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

You can download my version (with undo, save pattern, and configure logic features) at www.forkandbeard.co.uk.

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.

ForkandBeard