12,456,454 members (56,842 online)
alternative version

40.4K views
30 bookmarked
Posted

# A Fast Version of Conway's Game of Life with Thread and DirectX Draw

, 14 Apr 2009 CPOL
 Rate this:
A fast version of Conway's Game of Life with thread and DirectX draw

## 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:

1. Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation.
2. Any live cell with more than three live neighbours dies, as if by overcrowding.
3. Any live cell with two or three live neighbours lives, unchanged, to the next generation.
4. 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://en.wikipedia.org/wiki/Conway's_Game_of_Life

## Background

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

1. Create a document - To create a new MDI view of Conway's Game of Life or Sorting
2. Open an existing document - To open an existing document of Conway's Game of Life (*.lif) or Sorting (*.sor)
3. Save a document - To save an MDI frame of Conway's Game of Life (*.lif) or Sorting (*.sor)
4. Open a document of Conway's Game of Life
5. Open a document of Sorting
6. Randomly initialize a pattern of the Conway's Game of Life
7. Start the calculation on the current active MDI frame
8. Stop the calculation
9. Clear all cells in the active view and stop the calculation
10. A toggle switch to show grid or hide grid
11. Slow down - To slow down the creation of further generations of Conway's Game of Life
12. Speed up - To speed up the creation of further generations of Conway's Game of Life
13. Color Setting - To set the color of cells in Conway's Game of Life or Sorting Bars

#### Using the Mouse

1. 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".
2. 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.

3. 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:

```void CSortAndLifeView::CreatSortThread()
{

}

{
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();
return 0;
}```

Override the `OnPaint `method `CFomView `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();
}```

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.

## Share

Jerry Jiang(BOLIANG JIANG)

A passionate software developer since 1992

Education:Master of Computer Science.

jerry@smarttick.com

## You may also be interested in...

 Pro Pro

 View All Threads First Prev Next
 Great! Edwin Song5-Apr-09 11:42 Edwin Song 5-Apr-09 11:42
 Re: Great! Jerry Jiang7-Apr-09 23:31 Jerry Jiang 7-Apr-09 23:31
 Last Visit: 31-Dec-99 18:00     Last Update: 30-Aug-16 17:35 Refresh 1