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

Introduction

A fast conways's Game of Life Version using MFC,thread and DirectX together with a visual demo for sorting algorithms. It is also a good demo for "Bubble" and "Quick Sort" algorithms.

Conway's Game of Life

For information on Conway's Game of Life, check out the Wikipedia entry: Conway's Game of Life

Backgroud

I built this project just to test the algorithm of Conway's Game of Life,because I found 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 creating of further generations of Conway's Game of Life.
  12. Speed up - To speed up the creating 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,also you can interfere 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

To Create an independent thread to implement algorithum 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;
}

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



About the Author

Jerry Jiang

Jerry Jiang is a software developer, a passionate software developer since 1992. Website: Maple Technology Software

Downloads

Comments

  • wheloltabotly PumeSonee Phobereurce 2850917

    Posted by TizefaTaNaday on 04/16/2013 08:25am

    ShiptSishDype louis vuitton diaper bag cropypompural louis vuitton faux handbags Daugtulkclart

    Reply
  • wheloltabotly PumeSonee Phobereurce 9284020

    Posted by TizefaTaNaday on 03/29/2013 04:22pm

    RedsBexaste christian louboutin shoes hignaranine christian louboutin shoes CreeteSteby

    Reply
  • wheloltabotly PumeSonee Phobereurce 6768209

    Posted by TizefaTaNaday on 03/28/2013 01:46pm

    Infundado outlet Gucci hignaranine Why Not Try These Out CreeteSteby

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds