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

📄 nodestate.cpp

📁 人工智能中经典算法-宽度搜索和启发是搜索(A*算法)在VC环境下的实现代码
💻 CPP
字号:
// NodeState.cpp: implementation of the CNodeState class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AI.h"
#include "NodeState.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CNodeState::CNodeState()
{
	this->m_bAnswer = false;
	this->m_NodeType = NotYet;
}

CNodeState::~CNodeState()
{

}


//////////////////////////////////////////////////
// LoadData:载入状态
// 载入有两种方式:1.输入,2.复制已有状态
//////////////////////////////////////////////////
void CNodeState::LoadData(int Data,int i,int j)
{
	m_NodeData[i][j] = Data;
}

void CNodeState::LoadData(CNodeState *Item)
{
	int Src[3][3];
	for(int i=0; i<3; i++)
	 for(int j=0; j<3; j++)
	 {
	  Src[i][j] = Item->GetNodeData(i,j);
	  m_NodeData[i][j] = Src[i][j];
	 }
}

////////////////////////////////////
// FindBlankPostion:寻找空白位的位置
////////////////////////////////////
void CNodeState::FindBlankPosition()
{
	m_BlankPosition = Position(65535,65535);
	for(int i=0; i<3; i++)
	 for(int j=0; j<3; j++) 
	  if(m_NodeData[i][j] == Blank) 
		  m_BlankPosition = Position(i,j);	
}

////////////////////////////////////////////////////
// MoveBlank:移动空白位
// 用于复制已有状态到本身之后 移动空白位使产生新状态
// 参数:MoveDirection 移动方式
////////////////////////////////////////////////////
void CNodeState::MoveBlank(UINT MoveDirection)
{
	FindBlankPosition();
	Position TmpPos = m_BlankPosition;
	switch(MoveDirection)
	{
	case Left : TmpPos = m_BlankPosition-Position(0,1);break;
	case Right : TmpPos = m_BlankPosition+Position(0,1);break;
	case Up : TmpPos = m_BlankPosition-Position(1,0);break;
	case Down : TmpPos = m_BlankPosition+Position(1,0);break;
	default : AfxMessageBox("ERROR MoveBlank");break;
	}
	int TempData = m_NodeData[TmpPos.x][TmpPos.y];
	m_NodeData[TmpPos.x][TmpPos.y] = m_NodeData[m_BlankPosition.x][m_BlankPosition.y];
	m_NodeData[m_BlankPosition.x][m_BlankPosition.y] = TempData;
} 

//////////////////////////////////////
// IsEuqal:此节点状态与目标节点状态是否相同
// 参数:目标节点Item
// 返回值:TRUE 相等 FALSE 不等
//////////////////////////////////////
BOOL CNodeState::IsEqual(CNodeState *Item)
{
	int k=0;
	int Src[3][3];
	for(int i=0; i<3; i++)
	 for(int j=0; j<3; j++)
	 {
	  Src[i][j] = Item->GetNodeData(i,j);
	  if(Src[i][j] == m_NodeData[i][j]) k++;
	 }
	if(k == 3*3) return true;
	else return false;
}

bool CNodeState::IsEqual(int goalstate[3][3])
{
	int k=0;
//	int Src[3][3];
	for(int i=0; i<3; i++)
	 for(int j=0; j<3; j++)
	 {
//	  Src[i][j] = Item->GetNodeData(i,j);
	  if(goalstate[i][j] == m_NodeData[i][j]) k++;
	 }
	if(k == 3*3) return true;
	else return false;
}

////////////////////////////////////////////
// GetDispData:对外接口 取得当前位置的状态值
// 参数:位置变量i,j
////////////////////////////////////////////
int CNodeState::GetNodeData(int i, int j)
{
	return m_NodeData[i][j];
}

////////////////////////////////////////////////
// SetThisIsAAnswer: 对外接口 设置节点为正解节点
////////////////////////////////////////////////
void CNodeState::SetThisIsAAnswer()
{
	this->m_bAnswer = true;
}

////////////////////////////////////
// GetNoteType:对外接口 取得节点类型
////////////////////////////////////
UINT CNodeState::GetNodeType()
{
	return this->m_NodeType;
}

/////////////////////////////////////////
// SetNoteType:对外接口 设置节点类型
/////////////////////////////////////////
void CNodeState::SetNodeType(UINT nodeType)
{
	this->m_NodeType = nodeType;
}

/////////////////////////////////////
// SetCurrentG:对外接口 设置节点的G值
/////////////////////////////////////
void CNodeState::SetCurrentG(int CurG)
{
	this->m_CurrentG = CurG;
}

////////////////////////////////////
// GetCurrentG:对外接口 取得节点G值
////////////////////////////////////
int CNodeState::GetCurrentG()
{
	return this->m_CurrentG;
}

///////////////////////////////////////////////
// GetIsAAnswer:对外接口 取得节点是否是正解节点
///////////////////////////////////////////////
BOOL CNodeState::GetIsAAnswer()
{
	return this->m_bAnswer;
}

//////////////////////////////////////////
// SetCurrentCount:对外接口 设置节点记数值
//////////////////////////////////////////
void CNodeState::SetCurrentCount(int Count)
{
	this->m_CurrentCount = Count;
}

////////////////////////////////////////////
// GetCurrentCount:对外接口 设取得节点记数值
////////////////////////////////////////////
int CNodeState::GetCurrentCount()
{
	return this->m_CurrentCount;
}

////////////////////////////////////////////
// GetBlankPositin:对外接口 提交空白位的位置
////////////////////////////////////////////
Position CNodeState::GetBlankPosition()
{
	FindBlankPosition();
	return m_BlankPosition;
}

⌨️ 快捷键说明

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