你如何使Excel中的生活游戏?

我只知道如何在Excel中制作macros。

应该很久以前这样做了,但这里是我的版本的康威的生活在Excel中。

这是代码的一个黑客。 绝不是一个完美的解决scheme(没有在这个上花上一个年龄),但是你也许可以挑出一些东西。

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 

为了实现这个function,只需使单元格A1的背景为黑色,单元格B1的背景为白色,然后在C2:AM20范围内添加一些黑色背景,然后运行ActionLogic方法。

你可以通过Googlefind很多例子。

第一个结果是来自David Gainer博客的一篇文章,它使用康威的“生命游戏”来教授循环参考公式和迭代(不涉及VBA):

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

你将需要两个macros。 第一个应该格式化游戏表格,使单元格是正方形的。

让用户运行这个macros。 之后,她应该为每个活着的细胞input1。 使用条件格式将单元格完全变黑(如果值!= 0,背景=黑色)

现在有一个第二个macros来计算背景图纸(另一张图纸)中的下一个步骤。 使用相对单元格定位(相对于ActiveCell)和两个嵌套循环。 完成后,将背景表中的所有值复制到游戏表中。

search它,看看他们的代码。 在Excel中制作完整的游戏已经成为很多人的兴趣爱好。

例如: http : //www.geocities.jp/nchikada/pac/

你为什么说Excel是错误的select?

我认为Excel是解决这个问题的最好方法:

1.0(OR(SUM(B2:D4)-C3 = 3,AND(SUM(B2:D4)-C3 = 2,C3 = 1)),1,0)

*以上是返回单元格C3的下一代值的expression式。

以下是演示: https : //docs.google.com/open?id = 0B4FcWULw3iQidlZSdG9GRDh0TXM

如果你处于一个需要从零开始执行这种事情的情况,那么函数式编程是最好的方法。 否则,Excel工作得很好。 为什么? 因为Excel是一个强迫你只input纯函数的系统。 你看,模拟这个生命游戏的关键是要认识到,细胞的每个状态都是前一个状态的纯function。 Excel自然会迫使你这样想。

有关循环引用的另一个教程可以在这里find: http:

这个解释了如何使用循环引用插入时间戳。

Excel绝对是这种问题的错误select。 至于如何可能:首先了解生活的游戏,然后在Excel中使用visual basic 。