Click here to Skip to main content
14,541,138 members

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

Rate this:
4.42 (14 votes)
Please Sign up or sign in to vote.
4.42 (14 votes)
14 Apr 2009CPOL
A fast version of Conway's Game of Life with thread and DirectX draw


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:

  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."'s_Game_of_Life


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()
    unsigned threadID;

    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 );
    return 0;

Override the OnPaint method CFomView class to implement DirectX drawing:

void CSortAndLifeView::OnPaint() 
    CPaintDC dc(this);

void CSortAndLifeView::OnDraw(CDC* pDC)
    // TODO: add draw code for native data here

    CSortAndLifeDoc* pDoc = GetDocument();


    if(m_bDocumentType == 0)
        m_directXObj.CellDraw((char *)pDoc->m_cCellArray,pDoc->m_cellColor,rectView);
        m_directXObj.BarDraw((int *)pDoc->m_nNumberArray,pDoc->m_barColor,rectView);

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jerry Jiang
Founder SmartTick Software Inc.
Canada Canada

A passionate software developer since 1992

Education:Master of Computer Science.

Comments and Discussions

Generalinteresting Pin
madicursar13-Apr-09 22:54
Membermadicursar13-Apr-09 22:54 
GeneralGreat! Pin
Edwin Song5-Apr-09 11:42
MemberEdwin Song5-Apr-09 11:42 
GeneralRe: Great! Pin
Jerry Jiang7-Apr-09 23:31
MemberJerry Jiang7-Apr-09 23:31 
GeneralBubble and Quicksort Pin
Terence Russell3-Apr-09 5:54
MemberTerence Russell3-Apr-09 5:54 
GeneralRe: Bubble and Quicksort [modified] Pin
Jerry Jiang3-Apr-09 8:19
MemberJerry Jiang3-Apr-09 8:19 
GeneralRe: Bubble and Quicksort Pin
Rick York13-Apr-09 20:05
mveRick York13-Apr-09 20:05 
QuestionCould it be coprocessor-assisted using alpha blends? Pin
supercat92-Apr-09 16:55
Membersupercat92-Apr-09 16:55 
AnswerRe: Could it be coprocessor-assisted using alpha blends? Pin
Jerry Jiang2-Apr-09 20:48
MemberJerry Jiang2-Apr-09 20:48 
GeneralCool, brings back memories Pin
puromtec131-Mar-09 4:48
Memberpuromtec131-Mar-09 4:48 
GeneralRe: Cool, brings back memories Pin
Jerry Jiang2-Apr-09 21:04
MemberJerry Jiang2-Apr-09 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 25 Mar 2009


30 bookmarked