12,070,030 members (62,527 online)
Add your own
alternative version

39.1K views
673 downloads
30 bookmarked
Posted

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

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

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

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

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

 Founder SmartTick Software Inc. Canada
Jerry Jiang(BOLIANG JIANG)

Education:Master of Computer Science.

jerry@smarttick.com

## Comments and Discussions

 First Prev Next
 interesting madicursar13-Apr-09 23:54 madicursar 13-Apr-09 23:54
 Great! Edwin Song5-Apr-09 12:42 Edwin Song 5-Apr-09 12:42
 Re: Great! Jerry Jiang8-Apr-09 0:31 Jerry Jiang 8-Apr-09 0:31
 Bubble and Quicksort Terence Russell3-Apr-09 6:54 Terence Russell 3-Apr-09 6:54
 Re: Bubble and Quicksort [modified] Jerry Jiang3-Apr-09 9:19 Jerry Jiang 3-Apr-09 9:19
 Re: Bubble and Quicksort Rick York13-Apr-09 21:05 Rick York 13-Apr-09 21:05
 Could it be coprocessor-assisted using alpha blends? supercat92-Apr-09 17:55 supercat9 2-Apr-09 17:55
 On a system which implements alpha blending, I would expect that a "generation" could be accomplished by using alpha-blended block transfers to effectively count the number of life cells at each position, and then using a color-map transform that would turn every live pixel full white and every dead pixel full black. That would be a horribly slow approach without hardware assistance, but with hardware assistance it should be very fast. On older hardware like an Amiga, one should be able to use a sequence of planar BLTs to achieve similar effect. Two three-way BLTs will can be used to show which cells have an odd number of cells in their horizontal vicinity and which have 2 or more. With a few more BLTs one can identify all cells which have a total of 3 or 4 live cells in the immediate vicinity (including the cell itself). Those which don't have 3 or 4 should die; those which have exactly three should become alive. Those with four should be alive only if alive already. I don't think the original Amiga could run full-screen Life at a full 60fps, but it might have been able to manage 20-30fps. A modern graphics card, properly exploited, should be able to go much faster.
 Re: Could it be coprocessor-assisted using alpha blends? Jerry Jiang2-Apr-09 21:48 Jerry Jiang 2-Apr-09 21:48
 Cool, brings back memories puromtec131-Mar-09 5:48 puromtec1 31-Mar-09 5:48
 Re: Cool, brings back memories Jerry Jiang2-Apr-09 22:04 Jerry Jiang 2-Apr-09 22:04
 Last Visit: 31-Dec-99 19:00     Last Update: 9-Feb-16 7:57 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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

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