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

📄 intelligence.cpp

📁 用VC编制的集成的野人和八数码演示程序。其中野人程序用动态的效果演示
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	m_pRecordSet->Open( i_strSql, "AI", adOpenDynamic, adLockReadOnly, adCmdUnknown);
	if ( m_pRecordSet->AdoEof )
	{
		m_pRecordSet->Close();
		return false;
	}
	m_pRecordSet->Close();
	return true;
}

//--------------------------------判断当前状态是不是目标状态---------------------------------------
bool CIntelligence::isGoal(CString strCurrent)
{
	if ( strCurrent == m_strDestination )
		return true;
	return false;
}



//-----------------------------------------新建结点------------------------------------------------
void CIntelligence::newIntelligence(CString strParent)
{
	FieldsPtr i_pFields;
	FieldPtr i_pField;
    _variant_t i_strSql = "select * from Source where Source = '" + strParent + "'";
	bool i_bIsMinValue = true;     int i_nOrder = 1;
	TIntelligencePtr i_pHead, i_pTail, i_pCurrent;    int i_nFlag = 0;       // 为了向目标数据库中添加
	m_pRecordSet->Open( i_strSql, "AI", adOpenDynamic, adLockReadOnly, adCmdUnknown );
	i_pFields = m_pRecordSet->Fields;
	if ( !m_pRecordSet->AdoEof )
		m_pRecordSet->MoveFirst();
	else
	{
		m_pRecordSet->Close();
		return;
	}
	while ( !m_pRecordSet->AdoEof )
	{
		i_pCurrent = new TIntelligence;
		i_pField = i_pFields->Item["Destination"];
		i_pCurrent->strNode = i_pField->Value;
        i_pField = i_pFields->Item["Weight"];
		i_pCurrent->nWeight = i_pField->Value;
		i_pCurrent->pNext = NULL;       // 添加结点完毕
		i_nFlag++;                
		if ( i_nFlag == 1 )
		{
			i_pHead = i_pCurrent;    // 添加到目标数据库中的头结点
			i_pTail = i_pCurrent;
		}
		else
		{
			i_pTail->pNext = i_pCurrent;
			i_pTail = i_pCurrent;
		}
		m_pRecordSet->MoveNext();
	}
	m_pRecordSet->Close();          // 从源数据库中搜索数据完毕
	i_pCurrent = i_pHead;
	while ( i_pCurrent )
	{
		i_strSql = "select * from Destination where IsMinValue = true and Destination = '" + strParent + "'";
		m_pRecordSet->Open( i_strSql, "AI", adOpenDynamic, adLockReadOnly, adCmdUnknown );
		i_pFields = m_pRecordSet->Fields;
		i_pField = i_pFields->Item["Weight"];
		i_pCurrent->nWeight += ( int )i_pField->Value;
        i_pField = i_pFields->Item["Depth"];
		i_pCurrent->nDepth = ( int )i_pField->Value + 1;
		m_pRecordSet->Close();
		i_pCurrent = i_pCurrent->pNext;
	}
	while ( i_pHead )
	{
		i_strSql = "select * from Destination where Destination = '" + i_pHead->strNode + "'";
		m_pRecordSet->Open( i_strSql, "AI", adOpenDynamic, adLockOptimistic, adCmdUnknown );
		if ( !m_pRecordSet->AdoEof )
			m_pRecordSet->MoveFirst();
		while ( !m_pRecordSet->AdoEof )
		{
			i_nOrder++;
			i_pFields = m_pRecordSet->Fields;
			i_pField = i_pFields->Item["IsMinValue"];
			if ( i_pField->Value )
			{
			    i_pField = i_pFields->Item["Weight"];
			    if ( i_pHead->nWeight < ( int )i_pField->Value )
			    {
				    i_pField = i_pFields->Item["IsMinValue"];
			        i_pField->PutValue( false );
			        m_pRecordSet->Update();
			    }
			    else
				    i_bIsMinValue = false;
			}
			m_pRecordSet->MoveNext();
		}
		m_pRecordSet->Close();
	    i_strSql = "select * from Destination";
	    m_pRecordSet->Open( i_strSql, "AI", adOpenDynamic, adLockOptimistic, adCmdUnknown );
	    i_pFields = m_pRecordSet->Fields;
		m_pRecordSet->AddNew();
		i_pField = i_pFields->Item["Source"];       i_pField->PutValue( (_bstr_t)strParent  );
		i_pField = i_pFields->Item["Destination"];  i_pField->PutValue( (_bstr_t)i_pHead->strNode );
		i_pField = i_pFields->Item["Weight"];       i_pField->PutValue( i_pHead->nWeight );
        i_pField = i_pFields->Item["Depth"];        i_pField->PutValue( i_pHead->nDepth );
		i_pField = i_pFields->Item["IsMinValue"];   i_pField->PutValue( i_bIsMinValue );
        i_pField = i_pFields->Item["IsVisited"];    i_pField->PutValue( false );
		i_pField = i_pFields->Item["IsSolution"];   i_pField->PutValue( false );
		i_pField = i_pFields->Item["Sort"];        i_pField->PutValue( i_nOrder );
		m_pRecordSet->Update();
		m_pRecordSet->Close();
		i_pCurrent = i_pHead;
		i_pHead = i_pHead->pNext;
		delete( i_pCurrent );
		i_bIsMinValue = true;
		i_nOrder = 1;
	} 
}


//---------------------------------找出A*算法中代价最小的结点--------------------------------------
CString CIntelligence::findMinCost(void)
{
	CString i_strReturn = "";       int i_nReturn = -1, i_nTemp = -1;
	int i_nMinCost = 1000;
	FieldsPtr i_pFileds;    FieldPtr i_pFiled;
	_bstr_t i_strSql = "select * from Destination where IsVisited = false and IsMinValue = true";
	m_pRecordSet->Open( i_strSql, "AI", adOpenDynamic, adLockOptimistic, adCmdUnknown );
	i_pFileds = m_pRecordSet->Fields;
	if ( !m_pRecordSet->AdoEof )
		m_pRecordSet->MoveFirst();
	while ( !m_pRecordSet->AdoEof )
	{
		i_pFiled = i_pFileds->Item["Weight"];
		i_nTemp++;
		if ( ( int )i_pFiled->Value < i_nMinCost )
		{
			i_pFiled = i_pFileds->Item["Destination"];    i_strReturn = i_pFiled->Value;
			i_pFiled = i_pFileds->Item["Weight"];         i_nMinCost = i_pFiled->Value;
			i_nReturn = i_nTemp;
		}
		m_pRecordSet->MoveNext();
	}    
	if ( i_nReturn>=0 )
	{
		m_pRecordSet->MoveFirst();
		m_pRecordSet->Move( i_nReturn );
		i_pFiled = i_pFileds->Item["IsVisited"];
		i_pFiled->PutValue( true );
		m_pRecordSet->Update();
	}
	m_pRecordSet->Close();
	return i_strReturn;
}

//----------------------------------------找出A*算法的解---------------------------------------------
void CIntelligence::findAnswer(void)
{
	_bstr_t i_strSql;
	CString i_strDestination = m_strDestination;
	FieldsPtr i_pFields;    FieldPtr i_pField; 
	while ( i_strDestination != "")
	{
        i_strSql="select * from Destination where IsMinValue=true and Destination='"+i_strDestination+"'";
		m_pRecordSet->Open( i_strSql, "AI", adOpenDynamic, adLockOptimistic, adCmdUnknown );
		i_pFields = m_pRecordSet->Fields; 
		i_pField = i_pFields->Item["Source"];
		i_strDestination = i_pField->Value;
		i_pField = i_pFields->Item["IsSolution"];
		i_pField->PutValue( true );
		m_pRecordSet->Update();
		m_pRecordSet->Close();
	}
}

//------------------------------------生成A*算法的结果链表------------------------------------------
void CIntelligence::generateList(void)
{
	TIntelligencePtr i_pNew, i_pCurrent, i_pTail;
	TAiLevelPtr i_pAiHead, i_pAiNew, i_pAiTail;
	FieldsPtr i_pFields;    FieldPtr i_pField;
	_bstr_t i_strSql = "select * from Destination where IsMinValue=true and Destination='"+m_strSource+"'";
	int i_nNumber;
	while ( m_pResult )
	{
		i_pCurrent = m_pResult;
		m_pResult = m_pResult->pNext;
		delete( i_pCurrent );
	}
	m_pRecordSet->Open( i_strSql, "AI", adOpenDynamic, adLockReadOnly, adCmdUnknown );
    i_pFields = m_pRecordSet->Fields;
	i_pField = i_pFields->Item["Destination"];
	i_pNew = new TIntelligence;        // 新建头结点
	i_pNew->strNode = m_strSource;
	i_pNew->nWeight = 0;
	i_pNew->nDepth = 1;
	i_pNew->nBreadth = 1;
	i_pField = i_pFields->Item["IsVisited"];       i_pNew->bIsVisited= i_pField->Value;
	i_pField = i_pFields->Item["IsSolution"];      i_pNew->bIsSolution = i_pField->Value;
	i_pNew->pParent = NULL;
	i_pNew->pNext = NULL; 
	m_pRecordSet->Close();
	m_pResult = i_pNew;       // 结果链表的头指针
	i_pTail = i_pNew;         // 结果链表的尾指针 
	i_pCurrent = i_pNew;   
	i_pAiNew = new TAiLevel;
	i_pAiNew->nNumber = 1;
	i_pAiNew->pNext = NULL;
	i_pAiHead = i_pAiNew;
	i_pAiTail = i_pAiNew;
	while ( i_pCurrent )
	{
		i_strSql = "select * from Destination where IsMinValue=true and Source='"+i_pCurrent->strNode+"'";
		m_pRecordSet->Open( i_strSql, "AI", adOpenDynamic, adLockOptimistic, adCmdUnknown );
		i_pFields = m_pRecordSet->Fields;
		if ( !m_pRecordSet->AdoEof )
			m_pRecordSet->MoveFirst();
		while ( !m_pRecordSet->AdoEof )
		{
			i_pNew = new TIntelligence;        // 新建头结点
			i_pField = i_pFields->Item["Destination"];    i_pNew->strNode = i_pField->Value;
			i_pField = i_pFields->Item["Weight"];         i_pNew->nWeight = i_pField->Value;
            i_pField = i_pFields->Item["Depth"];          i_pNew->nDepth = i_pField->Value;
			if ( i_pNew->nDepth > m_nDepth )
			{
				i_pAiNew = new TAiLevel;
				i_pAiNew->nNumber = 1;
				i_pAiNew->pNext = NULL;
				i_pAiTail->pNext = i_pAiNew;
				i_pAiTail = i_pAiNew;
				i_pField = i_pFields->Item["Breadth"];    i_pField->PutValue( 1 );
				i_pNew->nBreadth = 1;
				m_nDepth++;
			}
			else
			{
				i_pAiNew = i_pAiHead;
				for ( i_nNumber=1; i_nNumber<m_nDepth; i_nNumber++ )				
					i_pAiNew = i_pAiNew->pNext;
				i_pAiNew->nNumber++;
				i_pField = i_pFields->Item["Breadth"];    i_pField->PutValue( (int)i_pAiNew->nNumber );
				i_pNew->nBreadth = i_pAiNew->nNumber;
			}	   
	        i_pField = i_pFields->Item["IsVisited"];      i_pNew->bIsVisited= i_pField->Value;
	        i_pField = i_pFields->Item["IsSolution"];     i_pNew->bIsSolution = i_pField->Value;	
	        i_pNew->pParent = i_pCurrent;
	        i_pNew->pNext = NULL;            // 新建头结点完成
			i_pTail->pNext = i_pNew;
			i_pTail = i_pNew;                // 将其放在链表尾部
			m_nTotalNum++;                   // 结点总数加1
			m_pRecordSet->MoveNext();
		}
		m_pRecordSet->Close();
		i_pCurrent = i_pCurrent->pNext;
	}
	while ( i_pAiHead )
	{
		i_pAiTail = i_pAiHead;
		i_pAiHead = i_pAiHead->pNext;
		delete( i_pAiTail );
	}
}
/*
//------------------------------------显示A*算法的结果链表------------------------------------------
void CIntelligence::show(CDC* pDC)
{
	//OnPrepareDC(pDC);
	TIntelligencePtr i_pCurrent = m_pResult;
	int i_nRowNum, i_nColNum;
	int i_nDispX, i_nDispY, i_nMaxX = 100 , i_nMaxY = 100;
	CString i_strDispItem; 
	CRect i_cRect;
	CPen i_bluePen;    CPen* i_oldPen;
	i_bluePen.CreatePen( PS_SOLID, 1, RGB(0,0,255));
	i_oldPen = pDC->SelectObject( &i_bluePen );
    while ( i_pCurrent )
	{ 
		i_nDispY = i_pCurrent->nDepth;
		i_nDispX = i_pCurrent->nBreadth;
		i_nDispX = 10 + ( i_nDispX -1 ) * 80;
        i_nDispY = 10 + ( i_nDispY -1 ) * 100;
		if ( i_pCurrent->bIsSolution )
		{
			pDC->SelectObject( &i_bluePen );
			pDC->SetTextColor( RGB ( 0, 0, 255 ) );
		}
		else
		{
			pDC->SelectObject( i_oldPen );
			pDC->SetTextColor( RGB ( 0, 0, 0 ) );
		}
		i_cRect.left = i_nDispX;           i_cRect.top = i_nDispY;
        i_cRect.right = i_nDispX + 70;     i_cRect.bottom = i_nDispY + 30;
		pDC->Ellipse( i_cRect );
		i_cRect.left = i_nDispX;           i_cRect.top = i_nDispY + 7;
        i_cRect.right = i_nDispX + 70;     i_cRect.bottom = i_nDispY + 25;
		pDC->DrawText( i_pCurrent->strNode, i_cRect, 1 );
		if ( i_pCurrent->nWeight!=0 )
		{
			i_cRect.left = i_nDispX + 20;      i_cRect.top = i_nDispY - 20;
		    i_cRect.right = i_nDispX + 70;     i_cRect.bottom = i_nDispY -5;
		    i_strDispItem.Format("%d", i_pCurrent->nWeight );
		    pDC->DrawText( i_strDispItem, i_cRect, 1 );
		}
		if ( i_pCurrent->pParent )
		{
			pDC->MoveTo( i_nDispX+35, i_nDispY );
			i_nRowNum = i_pCurrent->pParent->nBreadth;
			i_nColNum = i_pCurrent->pParent->nDepth;
			i_nRowNum = 10 + (i_nRowNum-1) * 80;
			i_nColNum = 10 + (i_nColNum-1) * 100;
			pDC->LineTo ( i_nRowNum+35,i_nColNum+30 );
		}
        if ( i_nDispX + 60 > i_nMaxX )
			i_nMaxX = i_nDispX+60;
        if ( i_nDispY > i_nMaxY )
			i_nMaxY = i_nDispY;
	    i_pCurrent = i_pCurrent->pNext;
	}
	//判断是否生成滚动条
	CSize sizeTotal;
	sizeTotal.cx = i_nMaxX+20;
	sizeTotal.cy = i_nMaxY+20;
	SetScrollSizes(MM_TEXT, sizeTotal);
} */

⌨️ 快捷键说明

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