Click here to Skip to main content
Click here to Skip to main content

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

By , 14 Apr 2009
Rate this:
Please Sign up or sign in to vote.
Conway_s_Game_of_Life6.JPG

Introduction

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

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

Toolbar3.JPG

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.

    Conway_s_Game_of_Life7.JPG

  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;

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

License

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 Maple Technology Software
Canada Canada
Jerry Jiang(BOLIANG JIANG)
 
Education:Master of Computer Science.
 
A passionate software developer since 1992,now lives in Vancouver.
 
jerry@mptsoft.com

Comments and Discussions

 
Generalinteresting Pinmembermadicursar13-Apr-09 22:54 
GeneralGreat! PinmemberEdwin Song5-Apr-09 11:42 
GeneralRe: Great! PinmemberJerry Jiang7-Apr-09 23:31 
GeneralBubble and Quicksort PinmemberTerence Russell3-Apr-09 5:54 
GeneralRe: Bubble and Quicksort [modified] PinmemberJerry Jiang3-Apr-09 8:19 
GeneralRe: Bubble and Quicksort PinmemberRick York13-Apr-09 20:05 
QuestionCould it be coprocessor-assisted using alpha blends? Pinmembersupercat92-Apr-09 16:55 
AnswerRe: Could it be coprocessor-assisted using alpha blends? PinmemberJerry Jiang2-Apr-09 20:48 
GeneralCool, brings back memories Pinmemberpuromtec131-Mar-09 4:48 
Nice! This subject has been studied heavily by Wolfram (mathematica guy). He thinks CA can be a predictive area, hence a science. I don't know about that, myself.
 
Nevertheless, here is game of life in hexagons. I dev'd the majority of this java app back in college as my capstone project. This one is fast too!
 
http://www.cse.sc.edu/~bays/h6h6h6/[^]
GeneralRe: Cool, brings back memories PinmemberJerry Jiang2-Apr-09 21:04 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 15 Apr 2009
Article Copyright 2009 by Jerry Jiang
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid