Journal pocra's Journal: Life in Excel
Sub life()
'15 minute programming i
'VBA(Excel) Macro for Conway's "Game of Life" (with non-wrapping board)
'by Pocra 29 Nov 2002
'Declaration of things!
Dim cell, incell, grid, ingrid As Range
Dim tote, CICi, Barry, sane As Integer
'set palette
For sane = 3 To 10
ActiveWorkbook.Colors(sane) = RGB(255 - (sane * 25), Int((Sin(sane / 3) + 1) * 127), 255 - (sane * 25))
Next
'set the size of the grid!
Set grid = Range("b2:z20")
'assign each cell in grid a random unitary binary value
For Each cell In grid
cell.Value = Int(Rnd(1) * 2)
Next
'repeat this bit forever and ever and ever (or until someone presses "break")
While Barry <> sane
'Set the colour of each cell so the colour number matches the number of neighbours (+2)
For Each cell In grid
'clear accumulate value of tote
tote = 0
'set the immediate neighbours grid to a 3x3 square centred on the current cell
Set ingrid = Range(cell.Offset(-1, -1), cell.Offset(1, 1))
'add all the surrounding cells to tote
For Each incell In ingrid
tote = tote + incell.Value
Next
tote = tote - cell.Value
'set the colour of the cell to tote+2
cell.Interior.ColorIndex = tote + 2
Next
'run the Life algorithm on each cell in the grid
'(if cell = 0, then switch it on iff number of neighbours = 3,
' if cell = 1, then keep it on only if number of neighbours = 2 or 3)
For Each cell In grid
'retrieve number of neighbours from the cell's colour index number
CICi = cell.Interior.ColorIndex - 2
'Apply the Life algorithm
Select Case cell.Value
' if cell is 0...
Case 0
'...set value to 1 if number of neighbours is 3
If CICi = 3 Then cell.Value = 1
'if cell is 1...
Case 1
'...switch it off unless number of neighbours is 2 or 3
If CICi < 2 Or CICi > 3 Then cell.Value = o
End Select
Next
Wend
End Sub
'15 minute programming i
'VBA(Excel) Macro for Conway's "Game of Life" (with non-wrapping board)
'by Pocra 29 Nov 2002
'Declaration of things!
Dim cell, incell, grid, ingrid As Range
Dim tote, CICi, Barry, sane As Integer
'set palette
For sane = 3 To 10
ActiveWorkbook.Colors(sane) = RGB(255 - (sane * 25), Int((Sin(sane / 3) + 1) * 127), 255 - (sane * 25))
Next
'set the size of the grid!
Set grid = Range("b2:z20")
'assign each cell in grid a random unitary binary value
For Each cell In grid
cell.Value = Int(Rnd(1) * 2)
Next
'repeat this bit forever and ever and ever (or until someone presses "break")
While Barry <> sane
'Set the colour of each cell so the colour number matches the number of neighbours (+2)
For Each cell In grid
'clear accumulate value of tote
tote = 0
'set the immediate neighbours grid to a 3x3 square centred on the current cell
Set ingrid = Range(cell.Offset(-1, -1), cell.Offset(1, 1))
'add all the surrounding cells to tote
For Each incell In ingrid
tote = tote + incell.Value
Next
tote = tote - cell.Value
'set the colour of the cell to tote+2
cell.Interior.ColorIndex = tote + 2
Next
'run the Life algorithm on each cell in the grid
'(if cell = 0, then switch it on iff number of neighbours = 3,
' if cell = 1, then keep it on only if number of neighbours = 2 or 3)
For Each cell In grid
'retrieve number of neighbours from the cell's colour index number
CICi = cell.Interior.ColorIndex - 2
'Apply the Life algorithm
Select Case cell.Value
' if cell is 0...
Case 0
'...set value to 1 if number of neighbours is 3
If CICi = 3 Then cell.Value = 1
'if cell is 1...
Case 1
'...switch it off unless number of neighbours is 2 or 3
If CICi < 2 Or CICi > 3 Then cell.Value = o
End Select
Next
Wend
End Sub
Life in Excel More Login
Life in Excel
Slashdot Top Deals