康威的人生游戏-规则框架和实现(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/conways-game-of-life-a-rule-framework-and-implemen-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3672 个词 阅读量 0康威的人生游戏-规则框架和实现(译文)
原文地址:https://www.codeproject.com/Articles/576572/Conways-Game-of-Life-A-Rule-Framework-and-Implemen
原文作者:tumbledDown2earth
译文由本站 robot-v1.0 翻译
前言
A rule engine based approach to add and remove rules to play Conway’s Game of Life
基于规则引擎的方法,可以添加和删除规则来玩Conway的"人生游戏"
介绍(Introduction)
生活游戏不是您典型的计算机游戏.它是一种"细胞自动机",由剑桥数学家约翰康威(John Conway)发明. "游戏"实际上是一个零玩家游戏,这意味着它的演变由其初始状态决定,不需要人类玩家的输入.通过创建初始配置并观察其演变,它可以与生命游戏进行交互.(*The Game of Life is not your typical computer game. It is a 'cellular automaton', and was invented by Cambridge mathematician John Conway. The "game" is actually a zero-player game, meaning that its evolution is determined by its initial state, needing no input from human players. One interacts with the Game of Life by creating an initial configuration and observing how it evolves.*) 该游戏在某游戏中被提及时就广为人知.(*This game became widely known when it was mentioned in an*) [文章(*article*)](http://ddi.cs.uni-potsdam.de/HyFISCH/Produzieren/lis_projekt/proj_gamelife/ConwayScientificAmerican.htm) 由<科学美国人>于1970年出版.它由一组细胞组成,这些细胞根据一些数学规则可以存活,死亡或繁殖.根据初始条件,在整个游戏过程中,单元会形成各种模式.有关介绍,您可以观看以下视频片段(*published by Scientific American in 1970. It consists of a collection of cells which, based on a few mathematical rules, can live, die or multiply. Depending on the initial conditions, the cells form various patterns throughout the course of the game. For an introduction, you can watch the video fragment from*) [斯蒂芬
霍金斯(Stephen Hawkings)生命的意义(Stephen Hawkings The Meaning of Life)](http://www.youtube.com/watch?v=CgOcEZinQ2I&feature=share&list=FLwikA_t8e6TSJW-L-lAHkKw) .(.)
背景(Background)
约翰康威(John Conway)扩展了约翰
冯`诺伊曼(John von Neumann)的工作,他创造了一种可以在板上操作的机器,该板上可以创建自己的副本. Neumann的机器所依据的规则比Conway的<生命游戏>中的规则复杂得多.(John Conway extended the work of John von Neumann who had created a machine that operated on a board that could create copies of itself. The rules that Neumann’s machine operated under were much more complicated than the rules in Conway’s Game of Life.)
生命游戏诞生后不久就变得非常著名.许多人着迷于这样一个事实,即细胞在其下运行的非常简单的规则可以创造出混乱的秩序,并且可以演化出如此复杂的模式.当家用电脑在游戏发布后不久变得流行时,许多实现方式都可以使用,并且该游戏也成为了流行的屏幕服务器.(The game of life became very famous soon after its creation. Many become fascinated by the fact that the very simple rules that the cells operate under could create order out of chaos and that so complicated patterns could evolve. When home computers become popular soon after the game was published, a lot of implementations become available and the game becomes a popular screen server.)
然而,Conway的生活游戏不仅令人着迷,而且在数学物理,哲学,经济和许多其他科学领域都具有理论意义.例如.它是细胞自动机最著名的例子之一,已成为可计算性理论研究的热门话题.看到(Conway’s game of life is however not only fascinating to look at but is of theoretic interest for mathematics physics, philosophy, economy and many other scientific fields. E.g. it is one of the most famous examples of cellular automata which has become a popular topic to study in computability theory. See) 维基百科(Wikipedia) 有关Conway生活游戏的更深入的文章.(for a more in depth article about Conway’s game of life.)
“生活游戏"的问题(The Problem of ‘Game of life’)
生命游戏的宇宙是一个由正方形单元组成的无限二维正交网格,每个网格都处于两种可能的状态,即活着的或死亡的.每个单元都与其八个邻居进行交互,这八个邻居是直接在水平,垂直或对角线相邻的单元.在每个时间步上,都会发生以下转换:(The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead. Every cell interacts with its eight neighbours, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:)
- 任何少于两个活邻居的活细胞都会死亡,就好像是寂寞一样.(Any live cell with fewer than two live neighbors dies, as if by loneliness.)
- 任何具有三个以上活邻居的活细胞都会死亡,就像人满为患一样.(Any live cell with more than three live neighbours dies, as if by overcrowding.)
- 任何有两个或三个活邻居的活细胞,都可以保持不变,直到下一代.(Any live cell with two or three live neighbours lives, unchanged, to the next generation.)
- 任何有三个活着邻居的死细胞都可以活着.(Any dead cell with exactly three live neighbors comes to life.) -(–)扩展规则(不是原始定义的一部分)以显示可插入规则引擎(Extended rule (not part of original definition) to show pluggable rule engines)
- 年龄大于3的任何死细胞死亡(附加规则).每一代都为细胞创造了时代(Any dead cell with age > 3 dies (additional rule). Every generation creates an age for the cells)
初始模式构成系统的"种子”.第一代是通过将上述规则同时应用于种子中的每个单元而创建的-生与死同时发生,有时将其发生的离散时刻称为壁虱. (换句话说,每一代都是以前的一代的纯函数.)继续重复应用这些规则以创建更多的一代.(The initial pattern constitutes the ‘seed’ of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed — births and deaths happen simultaneously, and the discrete moment at which this happens is sometimes called a tick. (In other words, each generation is a pure function of the one before.) The rules continue to be applied repeatedly to create further generations.)
以下输入将宇宙中的像元表示为X或-. X是一个活细胞. -是死细胞还是无细胞.以下输入提供宇宙中的提供模式或初始单元.输出是下一滴答(所有规则的应用程序的一次运行)中系统的状态,以相同格式表示.(The inputs below represent the cells in the universe as X or - . X is a alive cell. - is a dead cell or no cell . The below inputs provide the provide pattern or initial cells in the universe . The output is the state of the system in the next tick (one run of the application of all the rules), represented in the same format.)
这是一些插图(Here are a few illustrations from) http://www.conwaylife.com/wiki/Conway’s_Game_of_Life(http://www.conwaylife.com/wiki/Conway’s_Game_of_Life) .(.)
|
|
|
|
块(静物)(Block(still life)) Blinker(oscillator) Glider(spaceship) 脉冲星(周期3振荡器)(Pulsar(period 3 oscillator))
规则说明(Illustration of Rules)
在单元格的m×m正方形数组中,单元格C的存活状态为1或死亡状态为0.我们计算N-C的八个位置附近的活细胞的总和,然后根据下表,在下一代中,活细胞C是活的还是死的:(A cell C is represented by a 1 when alive or 0 when dead, in an m-by-m square array of cells. We calculate N - the sum of live cells in C’s eight-location neighbourhood, then cell C is alive or dead in the next generation based on the following table:)
细胞数量(Cell Number) | 新手机号码(New Cell Number) |
---|---|
1 0.1(1 0,1) | 0寂寞(0 # Lonely) |
1 4\5\6\7\8(1 4,5,6,7,8) | 0#人满为患(0 # Overcrowded) |
1 2,3(1 2,3) | 1生活(1 # Lives) |
0 3(0 3) | 1#要生三个孩子!(1 # It takes three to give birth!) |
0 1,2,4,5,6,7,8(0 0,1,2,4,5,6,7,8) | 0#贫瘠(0 # Barren) |
从这里(From here) http://rosettacode.org/wiki/Conway’s_Game_of_Life(http://rosettacode.org/wiki/Conway’s_Game_of_Life) .(.)
假设边界之外的单元总是死掉的.(Assume cells beyond the boundary are always dead.)
“游戏"实际上是一个零玩家游戏,这意味着它的演变由其初始状态决定,不需要人类玩家的输入.通过创建初始配置并观察其演变,它可以与生命游戏进行交互.(The “game” is actually a zero-player game, meaning that its evolution is determined by its initial state, needing no input from human players. One interacts with the Game of Life by creating an initial configuration and observing how it evolves.)
尽管您应该在更复杂的示例(例如更大的宇宙中的滑翔机)上测试实现,但请在3乘3格的网格中显示3代中的信号灯(连续三个相邻的单元都活着)的动作.(Although you should test your implementation on more complex examples such as the glider in a larger universe, show the action of the blinker (three adjoining cells in a row all alive), over three generations, in a 3 by 3 grid.)
这个(This) 是网络上有关如何制作游戏的一组准则.(is a set of guidelines on the web about how to make the game.)
本文(和代码)部分遵循上述准则,并且重点关注为什么以及如何创建通用规则处理接口和/或管道.(This article (and code) partially follows the above guidelines and also focuses on why and how should we create generic rule processing interfaces and/or pipelines.)
上面提到的5条规则中的最后一条也可以在代码执行中看到.(The last of the 5 rules mentioned above can be seen in execution in code also.)
除了框架之外,WPF中还有一个实现从最终用户UI角度演示算法.外观如下:(Along with the framework, there is an implementation in WPF to demonstrate the algorithm from an end-user UI perspective. Here is how it looks:)
使用代码(Using the Code)
现在深入研究代码…(Now a little deep dive into the code…)
规则引擎很常见,取决于(The rule engine is common and depends on) ITransition
看起来像:(which looks like:)
namespace gameOfLife.Framework
{
public interface ITransition
{
void Apply(ITransitive cellContainer);
}
public interface ITransitive
{
void ApplyTransitions(params ITransition[] transitions);
IList<Cell> Cells { get; set; }
}
}
在此基础上,将实施规则集并将其链接到执行顺序.这是链条的规则1.(Based on this, the set of rules are implemented and chained to the order of implementation. Here is the rule 1 of the chain.)
using gameOfLife.Framework;
namespace gameOfLife.Transition
{
public class Loneliness : ITransition
{
public void Apply(ITransitive cellContainer)
{
//cellContainer.Cells = cellContainer.Cells.Where
//((cell) => CanBeAlive(cellContainer.Cells, cell)).ToList();
foreach (var cell in cellContainer.Cells)
{
if (!CanBeAlive(cellContainer.Cells, cell))
{
cell.Kill();
}
}
}
private bool CanBeAlive(IList<Cell> otherCells, Cell cell)
{
return cell.NeighbouringLocations.Match(otherCells).Count > 1;
}
}
}
作为董事会(As the board the) PatternOfCells
类:(class:)
public class PatternOfCells : ITransitive
{
private IList<Cell> cells = new List<Cell>();
// threadsafe initialization
private static ITransitive instance = new PatternOfCells();
private PatternOfCells()
{
}
public static ITransitive Instance
{
get
{
// do not need double lock here
return instance;
}
}
public void ApplyTransitions(params ITransition[] transitions)
{
foreach (ITransition transition in transitions)
{
transition.Apply(this);
}
ClearDeadCells();
}
}
兴趣点(Points of Interest)
该框架对于尝试使用Conway的"人生游戏"实现解决方案的应用程序很有用.(The framework is useful for applications trying to implement solutions using Conway’s Game of Life.) 还有一个自定义规则也已实现.这需要链接到执行集.默认情况下,它没有连接(符合经典的"生命游戏"规则).附加规则是使游戏变得更有趣的一步.您可以在此处附加规则:(There is a custom rule also implemented. This needs to be chained to the set of execution. By default, it is not connected (in consistence to the classic Game of Life rules). The additional rule is a step beyond to make the game more interesting. You can attach the rule here:)
private void InitializeTransitionIndexes()
{
...
else
{
knownTransitions = new List<string>()
{ "Loneliness", "OverCrowding", "NextGeneration", "Spawn", "Die" }; // "Die" has
// to be appended
}
}
历史(History)
- 16(16)日(th)2013年4月:初始版本(April, 2013: Initial version)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET4 .NET WPF Dev Design Architect 新闻 翻译