带有线程和DirectX Draw的Conway生活游戏的快速版本(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/a-fast-version-of-conway-s-game-of-life-with-threa-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2965 个词 阅读量 0带有线程和DirectX Draw的Conway生活游戏的快速版本(译文)
原文地址:https://www.codeproject.com/Articles/34387/A-Fast-Version-of-Conway-s-Game-of-Life-with-Threa
原文作者:Jerry Jiang
译文由本站 robot-v1.0 翻译
前言
A fast version of Conway’s Game of Life with thread and DirectX draw
带有线程和DirectX绘图功能的Conway生命游戏的快速版本
介绍(Introduction)
本文是有关使用MFC,线程和DirectX的快速Conway的"生命游戏"版本以及用于排序算法的可视演示.它也是"气泡"和"快速排序"算法的可视演示.(This article is about a fast Conway’s Game of Life version using MFC, thread and DirectX together with a visual demo for sorting algorithms. It is also a visual demo for “Bubble” and “Quick Sort” algorithms.)
康威的人生游戏(Conway’s Game of Life)
的(The)生活游戏(Game of Life),也简称为(, also known simply as)生活(Life),是(, is a) 元胞自动机(cellular automaton) 由…设计(devised by the) 英式(British) 数学家(mathematician) 约翰霍顿
康威(John Horton Conway) 在1970年,它是细胞自动机的最著名例子.“游戏"实际上根本不是游戏. Conway的系统更像是生活模拟,这意味着其演化取决于其初始状态,不需要来自人类玩家的进一步投入.通常,通过创建初始配置并观察其演化与生命游戏进行交互,但是使用此应用程序,您可以通过单击鼠标添加或删除相邻单元格来干扰进化.(in 1970. It is the best-known example of a cellular automaton.The “game” really isn’t a game at all. Conway’s system is more like a life simulation, meaning that its evolution is determined by its initial state, requiring no further input from human players. Usually one interacts with the Game of Life by creating an initial configuration and observing how it evolves, but with this application you can interfere with the evolvement by adding or removing neighbour cells with the mouse click.)
康威生活游戏规则(Rules of Conway’s 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 neighbours dies, as if by needs caused by underpopulation.)
- 任何具有三个以上活邻居的活细胞都会死亡,就像人满为患一样.(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 neighbours becomes a live cell.) 初始模式构成系统的"种子”.第一代是通过将上述规则同时应用于种子中的每个单元而创建的-生与死同时发生,有时将其发生的离散时间称为(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.") http://zh.wikipedia.org/wiki/康威的_Game_of_Life(http://en.wikipedia.org/wiki/Conway’s_Game_of_Life)
背景(Background)
我构建了这个项目,以使用DirectX Draw测试Conway的<人生游戏>的算法,我发现它非常有趣.(I built this project to test the algorithm of Conway’s Game of Life with DirectX Draw and I found that it is very interesting.)
使用应用程序(Using the Application)
使用工具栏(Using the Toolbar)
- 创建文档-为Conway的"生命游戏"或"排序"创建新的MDI视图(Create a document - To create a new MDI view of Conway’s Game of Life or Sorting)
- 打开现有文档-要打开Conway的<人生游戏>的现有文档((Open an existing document - To open an existing document of Conway’s Game of Life ()*** .lif(**.lif*)**)或排序((*) or Sorting (*)*** .sor(**.sor*)**)(*)*)
- 保存文档-要保存Conway的<人生游戏>的MDI框架((Save a document - To save an MDI frame of Conway’s Game of Life ()*** .lif(**.lif*)**)或排序((*) or Sorting (*)*** .sor(**.sor*)**)(*)*)
- 打开Conway的<人生游戏>文件(Open a document of Conway’s Game of Life)
- 打开排序文件(Open a document of Sorting)
- 随机初始化Conway的人生游戏模式(Randomly initialize a pattern of the Conway’s Game of Life)
- 在当前活动的MDI框架上开始计算(Start the calculation on the current active MDI frame)
- 停止计算(Stop the calculation)
- 清除活动视图中的所有单元格并停止计算(Clear all cells in the active view and stop the calculation)
- 切换开关以显示网格或隐藏网格(A toggle switch to show grid or hide grid)
- 放慢速度-减慢Conway生命游戏的再一代的创造(Slow down - To slow down the creation of further generations of Conway’s Game of Life)
- 加快速度-加快创建Conway生命游戏的后代(Speed up - To speed up the creation of further generations of Conway’s Game of Life)
- 颜色设置-在Conway的"生命游戏"或"排序栏"中设置单元格的颜色(Color Setting - To set the color of cells in Conway’s Game of Life or Sorting Bars)
使用鼠标(Using the Mouse)
-
鼠标左键单击以添加新单元格或删除单元格.手动创建初始模式或通过添加新的相邻单元来将稳定模式更改为活动状态非常有用.另外,您可以通过单击鼠标来添加或删除邻居单元格,从而干扰进化.用户创建的初始模式可以通过"保存文档”/“打开文档"进行保存和加载.(Mouse Left Click to add a new cell or remove a cell. It is useful to create an initial pattern manually or to change a stable pattern to be active by adding new neighbour cells. In addition, you can interfere with the evolvement by adding or removing neighbour cells with the mouse click. The initial pattern created by the user can be saved and loaded by “Save a document”/“Open a document”.)
-
通过向左移动鼠标光标来连续添加/删除单元格.您可以轻松创建任何初始图案.(To add/remove cells in a row by moving the mouse cursor with left button down. You can create any initial pattern you want easily.)
-
在"排序"视图中右键单击"快速排序”.(Right click to “Quick Sort” in Sorting view.)
在代码中(In the Code)
创建一个独立的线程来实现Conways的生命游戏或排序算法:(Create an independent thread to implement the algorithm of Conways' Game of Life or Sorting:)
void CSortAndLifeView::CreatSortThread()
{
unsigned threadID;
if(hThread)
CloseHandle(hThread);
hThread = (HANDLE)_beginthreadex(NULL, 0, thread_proc, this, CREATE_SUSPENDED,
&threadID);
ResumeThread(hThread);
}
unsigned int __stdcall CSortAndLifeView::thread_proc(void* pv)
{
CSortAndLifeView *this_ = reinterpret_cast<CSortAndLifeView*>(pv);
if(this_->m_bDocumentType == DOCTYPE_LIFE)
this_->OnLifeGame();
else if(this_->m_bDocumentType == DOCTYPE_BUBBLESORT)
this_->OnBubbleSort();
else if(this_->m_bDocumentType == DOCTYPE_QUICKSORT)
this_->OnQuickSort();
_endthreadex( 0 );
return 0;
}
覆盖(Override the) OnPaint
方法(method) CFomView
实现DirectX绘图的类:(class to implement DirectX drawing:)
void CSortAndLifeView::OnPaint()
{
CPaintDC dc(this);
OnDraw(&dc);
}
void CSortAndLifeView::OnDraw(CDC* pDC)
{
// TODO: add draw code for native data here
CSortAndLifeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
GetClientRect(&rectView);
if(m_bDocumentType == 0)
m_directXObj.CellDraw((char *)pDoc->m_cCellArray,pDoc->m_cellColor,rectView);
else
m_directXObj.BarDraw((int *)pDoc->m_nNumberArray,pDoc->m_barColor,rectView);
m_directXObj.Display();
}
可以通过在中设置网格号来更改Conway的"生命游戏"的像元大小(The cell size of the Conway’s Game of Life can be changed by setting the grid number in)标准库(StdAfx.h).(.)
const int CONST_INT_GRIDNUMBER = 50;
兴趣点(Points of Interest)
借助线程和分布式网格计算,该应用程序可以快速完成Conway的生活游戏.(With threads and distributed grid calculation, the application makes a fast Conway’s Game of Life.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VC7.0 VC7.1 VC8.0 C++ VC6 Windows Dev 新闻 翻译