⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortandlifedoc.cpp

📁 Conway s Game of life 算法程序
💻 CPP
字号:
// SortAndLifeDoc.cpp : implementation of the CSortAndLifeDoc class
//

#include "stdafx.h"
#include "SortAndLife.h"

#include "SortAndLifeDoc.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int CSortAndLifeDoc::m_bDocNumber = 0;
/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc

IMPLEMENT_DYNCREATE(CSortAndLifeDoc, CDocument)

BEGIN_MESSAGE_MAP(CSortAndLifeDoc, CDocument)
	//{{AFX_MSG_MAP(CSortAndLifeDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc construction/destruction

CSortAndLifeDoc::CSortAndLifeDoc()
{
	// TODO: add one-time construction code here
	m_barColor = RGB(255,10,80);
	m_cellColor = RGB(10,255,80);
	m_nSleep = 20;
	m_bGrid = FALSE;
	InitializeCriticalSection(&m_cr);

	GetRandomNumberArray(&m_nNumberArray,CONST_INT_BARNUMBER);
	initializeCellLife();
	if(m_bDocNumber==0)
	{
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SPEEDUP,FALSE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SLOWDOWN,FALSE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_COLOR,FALSE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_START,FALSE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SEED,FALSE);
	
	}
	m_bDocNumber++;
}

CSortAndLifeDoc::~CSortAndLifeDoc()
{
	m_bDocNumber--;
	if(m_bDocNumber <= 0)
	{
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_START,TRUE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SEED,TRUE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SPEEDUP,TRUE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SLOWDOWN,TRUE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_COLOR,TRUE);
	
	}
	DeleteCriticalSection(&m_cr);
}

BOOL CSortAndLifeDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc serialization

void CSortAndLifeDoc::Serialize(CArchive& ar)
{
	int i,j;

	if (ar.IsStoring())
	{
		ar <<  m_nSleep;
		ar <<  m_cellColor;
		ar <<  m_barColor;
		for( i=0; i<CONST_INT_BARNUMBER; i++)
			ar << m_nNumberArray[i];
		for(i=0; i<CONST_INT_GRIDNUMBER; i++)
			for( j=0; j<CONST_INT_GRIDNUMBER; j++)
			ar << m_cCellArray[i][j];
		ar <<  m_bGrid;
		// TODO: add storing code here
	}
	else
	{
		ar >>  m_nSleep;
		ar >>  m_cellColor;
		ar >>  m_barColor;
		for(i=0; i<CONST_INT_BARNUMBER; i++)
			ar >> m_nNumberArray[i];
		for( i=0; i<CONST_INT_GRIDNUMBER; i++)
			for(j=0; j<CONST_INT_GRIDNUMBER; j++)
			ar >> m_cCellArray[i][j];
		ar >>  m_bGrid;
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc diagnostics

#ifdef _DEBUG
void CSortAndLifeDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CSortAndLifeDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc commands

void CSortAndLifeDoc::SetNewRandom()
{
	GetRandomNumberArray(&m_nNumberArray,CONST_INT_BARNUMBER);
	initializeCellLife();
}

void CSortAndLifeDoc::GetRandomNumberArray(void *m_nNumberArray,int nSize)
{
    int *pArray = (int *)m_nNumberArray;
	for(int i = 0; i < nSize;i++)
	{
		pArray[i] = rand()%CONST_INT_BARNUMBER;
		if(pArray[i] <= 0)
			pArray[i] = 1;
	}
}
void CSortAndLifeDoc::Clear()
{
	LockData();
	memset(m_cCellArray,0,sizeof(m_cCellArray));
	UnLockData();
}

void CSortAndLifeDoc::CreateCell(int x, int y)
{
	if((x >= CONST_INT_GRIDNUMBER) || (y >= CONST_INT_GRIDNUMBER))
		return;
	LockData();
	m_cCellArray[x][y] = !m_cCellArray[x][y];
	UnLockData();
}

void CSortAndLifeDoc::initializeCellLife()
{
	LockData();
	int z;
	for(int y=0; y<CONST_INT_GRIDNUMBER;y++)
	{
		for(int x=0;x<CONST_INT_GRIDNUMBER;x++)
		{
			z = (int) (10.0*rand()/RAND_MAX+1.0);
			if(z < 10)
					m_cCellArray[y][x] = 0;
			else
					m_cCellArray[y][x] = 1;
		}
	}
	UnLockData();
}

void CSortAndLifeDoc::CopyArray(char *doubleArray)
{
	LockData();
	memcpy(&m_cCellArray[0][0],doubleArray,CONST_INT_GRIDNUMBER*CONST_INT_GRIDNUMBER);
	UnLockData();
}

int CSortAndLifeDoc::Neighbors(int y,int x,char &bCell)
{
	LockData();
	int sum=0,i;
	if((x-1) >= 0)
	{
		for(i=y-1;i<=y+1;i++)
		{
			if((i >= 0) && (i < CONST_INT_GRIDNUMBER))
			{
				if(m_cCellArray[i][x-1])
					sum++;
			}
		}
	}

	if((x+1)<(CONST_INT_GRIDNUMBER))
	{
		for(i=y-1;i <=y+1;i++)
		{
			if((i>=0) && (i < CONST_INT_GRIDNUMBER))
			{
				if(m_cCellArray[i][x+1])
					sum++;
			}
		}
	}

	if((y-1) >= 0)
	{
		if(m_cCellArray[y-1][x])
			sum++;
	}

	if((y+1) < CONST_INT_GRIDNUMBER)
	{
		if(m_cCellArray[y+1][x])
			sum++;
	}
	bCell = m_cCellArray[y][x];
	UnLockData();
	return sum;
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -