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 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."
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
- Create a document - To create a new MDI view of Conway's Game of Life or Sorting
- Open an existing document - To open an existing document of Conway's Game of Life (*.lif) or Sorting (*.sor)
- Save a document - To save an MDI frame of Conway's Game of Life (*.lif) or Sorting (*.sor)
- Open a document of Conway's Game of Life
- Open a document of Sorting
- Randomly initialize a pattern of the Conway's Game of Life
- 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
- Slow down - To slow down the creation of further generations of Conway's Game of Life
- Speed up - To speed up the creation of further generations of Conway's Game of Life
- 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
Create an independent thread to implement the algorithm of Conways' Game of Life or Sorting:
hThread = (HANDLE)_beginthreadex(NULL, 0, thread_proc, this, CREATE_SUSPENDED,
unsigned int __stdcall CSortAndLifeView::thread_proc(void* pv)
CSortAndLifeView *this_ = reinterpret_cast<CSortAndLifeView*>(pv);
if(this_->m_bDocumentType == DOCTYPE_LIFE)
else if(this_->m_bDocumentType == DOCTYPE_BUBBLESORT)
else if(this_->m_bDocumentType == DOCTYPE_QUICKSORT)
_endthreadex( 0 );
CFomView class to implement DirectX drawing:
void CSortAndLifeView::OnDraw(CDC* pDC)
CSortAndLifeDoc* pDoc = GetDocument();
if(m_bDocumentType == 0)
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
With threads and distributed grid calculation, the application makes a fast Conway's Game of Life.