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

📄 game.cpp

📁 五子棋源码,非常经典的游戏,也算是初学DELPHI的朋友必须了解的东西,可以加强逻辑思维,提高编程的严密性
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		}
	}
	for( i=0;i<FIVE_MAX_LINE;i++ )
	for( j=0;j<FIVE_MAX_LINE;j++ )
	{
		if( FiveArea[i][j] == 'N' )
		{
			FiveArea[i][j] = Mf;
			if( SreachArea( FiveArea,Mf ) == -1 )
			{
				Step step;
				FiveArea[i][j] = Side;
				mm = i;
				nn = j;
				step.m = mm;
				step.n = nn;
				step.side = Side;
				StepList.AddTail( step );
				return TRUE;
			}
			FiveArea[i][j] = 'N';
		}
	}
return FALSE;
}

void CFive::ResumePlayState()
{
	int i,j;
	POSITION pos;
	Step step_temp;
	for( i=0;i<FIVE_MAX_LINE;i++ )
		for( j=0;j<FIVE_MAX_LINE;j++ )
		FiveArea[i][j] = 'N';
	pos = StepList.GetHeadPosition();
	while(pos)
	{
		step_temp = StepList.GetNext(pos);
		FiveArea[step_temp.m][step_temp.n] = step_temp.side;
	}
}

int CFive::WzqRun( int &mm, int &nn )
{
	int m,n;
	double max_count;
	Count  temp;
	Step   step_temp;
	POSITION pos;
	m = mm;
	n = nn;
    PiShen.count = WZQ_YOU;
	if( m < 0||m >= FIVE_MAX_LINE||n < 0||n >= FIVE_MAX_LINE )
		return WZQ_HAVE;

//	if( FiveArea[m][n] != 'N' )
//		return WZQ_HAVE;

	if( BiTest( mm,nn ) == FALSE )
	{
		OneCountList.RemoveAll();
		CreateThread();
//------------------------------------------------
//		EndEvent.Lock();
//		WaitForSingleObject( m_hThread,INFINITE );
//		if( OneCountList.IsEmpty() )
//			return WZQ_ERROR;
//		
//		temp = OneCountList.GetHead();
//		max_count = temp.count;
//		mm = temp.step.m;
//		nn = temp.step.n;
//		
//		pos = OneCountList.GetHeadPosition();
//		while( pos != NULL )
//		{
//			temp = OneCountList.GetNext( pos );
//			if( temp.count > max_count )
//			{
//				max_count = temp.count;
//				mm = temp.step.m;
//				nn = temp.step.n;
//			}
//		}
//----------------------------------------------------
	}
	else
		return WZQ_NOTHREAD;
//----------------------------------------------------
//	if( mm == m&&nn == n )
//		return WZQ_ERROR;
//	FiveArea[mm][nn] = Side;
//	step_temp.side = Side;
//	step_temp.m    = mm;
//	step_temp.n    = nn;
//	StepList.AddTail( step_temp );
//	PlayIndex = StepList.GetCount();
//-----------------------------------------------------
	return WZQ_RUN;
}

int CFive::GetCurStep(int &mm,int &nn)
{
	int m,n;
	POSITION pos;
	double   max_count;
	Count  temp;
	Step   step_temp;
    if( PiShen.count == WZQ_I )
	{
		step_temp = PiShen.step;
		mm = PiShen.step.m;
		nn = PiShen.step.n;
		step_temp.side = Side;
		FiveArea[mm][nn] = Side;
		StepList.AddTail( step_temp );
		PlayIndex = StepList.GetCount();
		return WZQ_RUN;
	}
////////////////////////////////////////////////////////////////////////////////
//	if( OneCountList.IsEmpty() )
//		return WZQ_ERROR;
////////////////////////////////////////////////////////////////////////////////
	if( CountList.IsEmpty() )
		return WZQ_ERROR;

//	temp = OneCountList.GetHead();
	temp = CountList.GetHead();

	max_count = temp.count;
	mm = temp.step.m;
	nn = temp.step.n;
///////////////////////////////////////////////////////////////////////////////		
//	pos = OneCountList.GetHeadPosition();
//	while( pos != NULL )
//	{
//		temp = OneCountList.GetNext( pos );
//		if( temp.count > max_count )
//		{
//			max_count = temp.count;
//			mm = temp.step.m;
//			nn = temp.step.n;
//		}
//	}
//////////////////////////////////////////////////////////////////////////////
	pos = CountList.GetHeadPosition();
	while( pos != NULL )
	{
		temp = CountList.GetNext( pos );
		if( temp.count > max_count )
		{
			max_count = temp.count;
			mm = temp.step.m;
			nn = temp.step.n;
		}
	}

	if( mm == m&&nn == n )
		return WZQ_ERROR;
	FiveArea[mm][nn] = Side;
	step_temp.side = Side;
	step_temp.m    = mm;
	step_temp.n    = nn;
	StepList.AddTail( step_temp );
	PlayIndex = StepList.GetCount();

	return WZQ_RUN;
}

void CFive::Serialize( CArchive& ar )
{
	int i,j;
	BYTE flags0,flags1;
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar<<(BYTE)0xfa;
		ar<<(BYTE)0x5e;
		ar<<WF0_1;
		ar<<WF0_2;
		ar<<WF0_3;
		ar<<WF0_4;
		ar<<WF1_1;
		ar<<WF1_2;
		ar<<WF1_3;
		ar<<WF1_4;
		ar<<WF2_3;
		ar<<WF2_4;
		ar<<WF5;
		ar<<BreadthMax;
		ar<<DeepMax;
		ar<<ThreadDeepMax;
		ar<<Delta;
		ar<<Side;
		for( i = 0;i<FIVE_MAX_LINE;i++ )
			for( j = 0;j<FIVE_MAX_LINE;j++ )
			   ar<<FiveArea[i][j];
        StepList.Serialize( ar );
		ImpList.Serialize( ar );
		ar<<PlayStateFlags;
		ar<<PlayIndex;
	}
	else
	{
		// TODO: add loading code here
		ar>>flags0;
		ar>>flags1;
		if( flags0!=0xfa||flags1!=0x5e)
		     AfxThrowFileException(CFileException::accessDenied);
		StepList.RemoveAll();
		ar>>WF0_1;
		ar>>WF0_2;
		ar>>WF0_3;
		ar>>WF0_4;
		ar>>WF1_1;
		ar>>WF1_2;
		ar>>WF1_3;
		ar>>WF1_4;
		ar>>WF2_3;
		ar>>WF2_4;
		ar>>WF5;
		ar>>BreadthMax;
		ar>>DeepMax;
		ar>>ThreadDeepMax;
		ar>>Delta;
		ar>>Side;
		for( i = 0;i<FIVE_MAX_LINE;i++ )
			for( j = 0;j<FIVE_MAX_LINE;j++ )
			   ar>>FiveArea[i][j];
        StepList.Serialize( ar );
		ImpList.Serialize( ar );
		ar>>PlayStateFlags;
		ar>>PlayIndex;
		if( PlayStateFlags == TRUE )
			UpdatePlay();
	}
}
////////////////////////////////////////////////////////////////////////////////////////////
// 抽象平分函数
// ~~~~~~~~~~~~~
// Nf ----------对那一方平分'B'或'W'
// level--------方法COUNT_INC,COUNT_SUB,COUNT_MID
///////////////////////////////////////////////////////////////////////////////////////////
void CFive::CalRun( char Nf,LEVE leve )
{
	if( CurBreadth <= 0 )
		return;
	MemoryCount++;
	JingDuCount++;

	char     Mf;
	int      i,j,num;
	double   Ncount,Mcount,TempCount,TempCount1,wf5temp;
	Count    *pCount,temp;
	char     Area[FIVE_MAX_LINE][FIVE_MAX_LINE];
	POSITION pos;
	Step     steptemp;

	Mf = ( Nf == 'B'?'W':'B');
	
	pCount = new Count[CurBreadth];

	for( i = 0;i < CurBreadth;i++ )
	{
		pCount[i].count = -9999999;
		pCount[i].step.side = 'E';
	}

	for( i = 0;i < FIVE_MAX_LINE;i++ )
		for( j = 0;j < FIVE_MAX_LINE;j++ )
		    Area[i][j] = FiveArea[i][j];        //复制FiveArea数组

    pos = DeepList.GetHeadPosition( );
	while(pos != NULL)
	{
		steptemp = DeepList.GetNext(pos);
		Area[steptemp.m][steptemp.n] = steptemp.side;
	}

	switch( leve )
	{
	case COUNT_INC:
		Ncount = SreachArea( Area,Nf );
		for( i = 0;i < FIVE_MAX_LINE;i++ )
		{
			for( j = 0;j < FIVE_MAX_LINE;j++ )
			{
			    if( Area[i][j] == 'N' )
				{
					Area[i][j] = Nf;
					wf5temp = SreachArea( Area,Nf );
					if( wf5temp == -1 )
						wf5temp = 2*Dump( 5,10 ) + Ncount;
					TempCount = wf5temp - Ncount;
					if( TempCount > pCount[0].count )
					{
						pCount[0].count     = TempCount;
						pCount[0].step.side = Nf;
						pCount[0].step.m    = i;
						pCount[0].step.n    = j;
						for( num = 1;num < CurBreadth;num++ )
							if( pCount[0].count > pCount[num].count )
						      {
							     temp = pCount[num];
								 pCount[num] = pCount[0];
								 pCount[0] = temp;
						      }
					}
					Area[i][j] = 'N';
				}
			}
		}
		break;
	case COUNT_SUB:
		Mcount = SreachArea( Area,Mf );
		for( i = 0;i < FIVE_MAX_LINE;i++ )
		{
			for( j = 0;j < FIVE_MAX_LINE;j++ )
			{
			    if( Area[i][j] == 'N' )
				{
					Area[i][j] = Nf;
					TempCount = Mcount - SreachArea( Area,Mf );
					if( TempCount > pCount[0].count )
					{
						pCount[0].count     = TempCount;
						pCount[0].step.side = Nf;
						pCount[0].step.m    = i;
						pCount[0].step.n    = j;
						for( num = 1;num < CurBreadth;num++ )
							if( pCount[0].count > pCount[num].count )
						      {
							     temp = pCount[num];
								 pCount[num] = pCount[0];
								 pCount[0] = temp;
						      }
					}
					Area[i][j] = 'N';
				}
			}
		}
		break;
	case COUNT_MID:
		Mcount = SreachArea( Area,Mf );
		Ncount = SreachArea( Area,Nf );
		for( i = 0;i < FIVE_MAX_LINE;i++ )
		{
			for( j = 0;j < FIVE_MAX_LINE;j++ )
			{
			    if( Area[i][j] == 'N' )
				{
					Area[i][j] = Nf;
					wf5temp = SreachArea( Area,Nf );
					if( wf5temp == -1 )
						wf5temp = Ncount + 2*Dump( 5,10 );
					TempCount1 = wf5temp - Ncount;
					TempCount = Mcount - SreachArea( Area,Mf );
					TempCount = TempCount + TempCount1;
					if( TempCount > pCount[0].count )
					{
						pCount[0].count     = TempCount;
						pCount[0].step.side = Nf;
						pCount[0].step.m    = i;
						pCount[0].step.n    = j;
						for( num = 1;num < CurBreadth;num++ )
							if( pCount[0].count > pCount[num].count )
						      {
							     temp = pCount[num];
								 pCount[num] = pCount[0];
								 pCount[0] = temp;
						      }
					}
					Area[i][j] = 'N';
				}
			}
		}
		break;
	case COUNT_ALPHA:
		Mcount = SreachArea( Area,Mf );
		Ncount = SreachArea( Area,Nf );
		for( i = 0;i<FIVE_MAX_LINE;i++ )
			for( j = 0;j<FIVE_MAX_LINE;j++ )
		{
			if( Area[i][j] == 'N' )
			{
				Area[i][j] = Mf;
				wf5temp = SreachArea( Area,Mf );
				if( wf5temp == -1 )
				{
					wf5temp = Mcount + 2*Dump( 5,10 );
					if( Mf == Side )
					{
						Step le;
						if( !DeepList.IsEmpty() )
						{
							le = DeepList.GetHead();
						    PiShen.step = le;
						    PiShen.count = WZQ_I;
						}
					}
				}
                TempCount = wf5temp - Mcount;
				Area[i][j] = Nf;
				wf5temp = SreachArea( Area,Nf );
				if( wf5temp == -1 )
				{
					wf5temp = Ncount + 2*Dump( 5,10 );
					if( Nf == Side )
					{
						Step le;
						if( !DeepList.IsEmpty() )
						{
							le = DeepList.GetHead();
						    PiShen.step = le;
						    PiShen.count = WZQ_I;
						}
					}
				}
				TempCount += wf5temp - Ncount;
				if( TempCount > pCount[0].count )
				{
					pCount[0].count     = TempCount;
					pCount[0].step.side = Nf;
					pCount[0].step.m    = i;
					pCount[0].step.n    = j;
					for( num = 1;num < CurBreadth;num++ )
						if( pCount[0].count > pCount[num].count )
					{
						temp = pCount[num];
						pCount[num] = pCount[0];
						pCount[0] = temp;
					}
				}
				Area[i][j] = 'N';
			}
		}
		break;
	case COUNT_DELTA:
		Mcount = SreachArea( Area,Mf );
		Ncount = SreachArea( Area,Nf );
		for( i = 0;i<FIVE_MAX_LINE;i++ )
			for( j = 0;j<FIVE_MAX_LINE;j++ )
		{
			if( Area[i][j] == 'N' )
			{
				Area[i][j] = Mf;
				wf5temp = SreachArea( Area,Mf );
				if( wf5temp == -1 )
					wf5temp = Mcount + 2*Dump( 5,10 );
                TempCount = wf5temp - Mcount;
				Area[i][j] = Nf;
				wf5temp = SreachArea( Area,Nf );
				if( wf5temp == -1 )
					wf5temp = Ncount + 2*Dump( 5,10 );
				TempCount1 = wf5temp - Ncount;
				if( TempCount1 > TempCount )
					TempCount = TempCount1;
				if( TempCount > pCount[0].count )
				{
					pCount[0].count     = TempCount;
					pCount[0].step.side = Nf;
					pCount[0].step.m    = i;
					pCount[0].step.n    = j;
					for( num = 1;num < CurBreadth;num++ )
						if( pCount[0].count > pCount[num].count )
					{
						temp = pCount[num];
						pCount[num] = pCount[0];
						pCount[0] = temp;
					}
				}
				Area[i][j] = 'N';
			}
		}
		break;
	}

	for( num = 0;num < CurBreadth;num++ )
		if( pCount[num].step.side != 'E' )
		    CountList.AddTail( pCount[num] );
	delete pCount;
	MemoryCount--;
}

/////////////////////////////////////////////////////////////////////////////////////////////
// 给下一层次加入深度列表
// ~~~~~~~~~~~~~~~~~~~~~~~
void CFive::AddDeepList( Step step )  
{
	DeepList.AddTail( step );
}

Step CFive::GetLastDeepList()
{
	return DeepList.GetTail();
}

double CFive::GetStepCount()
{
	POSITION pos;
	double   temp;
	Count    steptemp;
	temp = 0;
	pos = CountList.GetHeadPosition();
	while(pos != NULL)
	{
		steptemp = CountList.GetNext(pos);
        temp = steptemp.count + temp;
	}
	if( CurCount == Side )
		return (temp+CurCount);
	else
		return (-temp+CurCount);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// 搜索安排函数
// ~~~~~~~~~~~~~

void CFive::ThreadRun()
{
	if( WaitForSingleObject( KillWzqRun,0 ) == WAIT_OBJECT_0 )
		return;

	if( CurDeep <= 0 || CurBreadth <= 0 )
	{
		return;
	}
/////////////////////////////////////////////////////////////////////////////////
// 显示信息
// ~~~~~~~~~
//	{
//		TCHAR Buf[50];
//		wsprintf( Buf,"%d",ThreadCount );
//		pInfo->SetPaneText(2,Buf );
//		wsprintf( Buf,"%2fMb",MemoryCount*600/1024/1024 );
//		pInfo->SetPaneText(3,Buf );
//	}
/////////////////////////////////////////////////////////////////////////////////
	if( CurThreadDeep > 0 )
	{
		POSITION pos,postemp;
		CFive **pFive;
		int i,num;
		i = 0;
//...........................................................................................
		CalRun( CurSide, COUNT_ALPHA );
		num   = CountList.GetCount();
		pFive = (CFive**)new BYTE[sizeof(CFive*)*num];
		pos = CountList.GetHeadPosition();
		while(pos)
		{
			pFive[i] = new CFive( CurSide,CurDeep,CurBreadth,CurThreadDeep );
			postemp = DeepList.GetHeadPosition();
			while( postemp )
			{
				pFive[i] -> AddDeepList( DeepList.GetNext( postemp ));
			}
			pFive[i] -> AddDeepList( CountList.GetNext( pos ).step );
			pFive[i] -> CreateThread();  //Create thread
			i++;
		}

		for( i=0;i < num;i++ )
		{
			pFive[i]->EndEvent.Lock();
			WaitForSingleObject( pFive[i] -> m_hThread,INFINITE ); 
			CurCount += pFive[i] -> GetStepCount();
			if( CurDeep == DeepMax )
			{
				Count count_temp;
			    count_temp.step  = pFive[i] -> GetLastDeepList();
				count_temp.count = pFive[i] -> GetStepCount();
				OneCountList.AddTail( count_temp );
			}
			delete pFive[i];
		}
		delete pFive;
	}
	else
	{
		int i,num;
		POSITION pos,postemp;
//...........................................................................................
		CalRun( CurSide,COUNT_ALPHA );
		num = CountList.GetCount();
		pos = CountList.GetHeadPosition();
		while( pos )
		{
			if( WaitForSingleObject( KillWzqRun,0 ) == WAIT_OBJECT_0 )
				return;
			{
			postemp = DeepList.GetHeadPosition();
	        if( CurSide == Side )
			{
				CFive five( CurSide,CurDeep,CurBreadth,CurThreadDeep );
				while( postemp )
				{
					five.AddDeepList( DeepList.GetNext( postemp ));
				}
				five.AddDeepList( CountList.GetNext( pos ).step );
			    i++;
			    five.ThreadRun();
			    CurCount += five.GetStepCount();
			    if( CurDeep == DeepMax )
			    {
					Count count_temp;
			        count_temp.step = five.GetLastDeepList();
			 	    count_temp.count = five.GetStepCount();
				    OneCountList.AddTail( count_temp );
			    }
			}else
			{
				CFive five( CurSide,CurDeep,CurBreadth,CurThreadDeep );
				while( postemp )
				{
					five.AddDeepList( DeepList.GetNext( postemp ));
				}
				five.AddDeepList( CountList.GetNext( pos ).step );
			    i++;
			    five.ThreadRun();
			    CurCount += five.GetStepCount();
			    if( CurDeep == DeepMax )
			    {
					Count count_temp;
			        count_temp.step = five.GetLastDeepList();
			 	    count_temp.count = five.GetStepCount();
				    OneCountList.AddTail( count_temp );
			    }
			}
			}
		}
	}
}

BOOL CFive::InitInstance()
{
	ThreadCount++;
	ThreadRun();
	return FALSE;
}

int CFive::ExitInstance()
{
	ThreadCount--;
	EndEvent.SetEvent();
	return CWinThread::ExitInstance();
}

⌨️ 快捷键说明

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