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

📄 aiview.cpp

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

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

#include "AIDoc.h"
#include "AIView.h"
#include "SearchAlg.h"


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

/////////////////////////////////////////////////////////////////////////////
// CAIView

IMPLEMENT_DYNCREATE(CAIView, CScrollView)

BEGIN_MESSAGE_MAP(CAIView, CScrollView)
	//{{AFX_MSG_MAP(CAIView)
	ON_COMMAND(ID_ASTAR, OnAstar)
	ON_COMMAND(IDM_ViewStep, OnViewStep)
	ON_COMMAND(IDM_ViewTree, OnViewTree)
	ON_UPDATE_COMMAND_UI(IDM_ViewStep, OnUpdateViewStep)
	ON_UPDATE_COMMAND_UI(IDM_ViewTree, OnUpdateViewTree)
	ON_COMMAND(ID_ASTARINPUT, OnAstarinput)
	ON_COMMAND(ID_BFS, OnBfs)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAIView construction/destruction

CAIView::CAIView()
{
	// TODO: add construction code here
	m_bIsDispAll = true;
	IsBFS=false;//

}

CAIView::~CAIView()
{
}

BOOL CAIView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAIView drawing

void CAIView::OnDraw(CDC* pDC)
{
	CAIDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	POSITION pos = m_DispList.GetHeadPosition();
	if(pos == NULL) return;
	//显示
	int DispX,DispY;
	while(pos)
	{
		CNodeState *Item = (CNodeState *)pDoc->m_DispList.GetNext(pos);
		DispY = (Item->GetCurrentG())*70+10;
		if(m_bIsDispAll) 
		{
			for(int i=0; i<3; i++)
			{
				DispX = (Item->GetCurrentCount())*70+10; 
				if(Item->GetIsAAnswer()) pDC->SetTextColor(RGB(255,128,0));
				else pDC->SetTextColor(RGB(0,0,0));
				for(int j=0; j<3; j++)
				{
					pDC->Rectangle(DispX-1,DispY-1,DispX+20,DispY+20);
					DataType DataItem = Item->GetNodeData(i,j);
					CString DispItem;
					if(DataItem != 0) DispItem.Format("%d ",DataItem);
					else DispItem ="  ";
					pDC->TextOut(DispX, DispY, DispItem);
					DispX += 20;
				} 
				DispY += 20;
			}
		 DispY += 10;
		}
		else
		{
			if(Item->GetIsAAnswer() == false) continue;
			for(int i=0; i<3; i++)
			{
				DispX = 10; 
				pDC->SetTextColor(RGB(255,128,0));
				for(int j=0; j<3; j++)
				{
					pDC->Rectangle(DispX-1,DispY-1,DispX+20,DispY+20);
					DataType DataItem = Item->GetNodeData(i,j);
					CString DispItem;
					if(DataItem != 0) DispItem.Format("%d ",DataItem);
					else DispItem ="  ";
					pDC->TextOut(DispX, DispY, DispItem);
					DispX += 20;
				} 
				DispY += 20;
			}
			DispY += 10;
		}
	}
	//判断是否生成滚动条
	CSize sizeTotal;
	sizeTotal.cx = DispX+80;
	sizeTotal.cy = DispY+80;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CAIView diagnostics

#ifdef _DEBUG
void CAIView::AssertValid() const
{
	CView::AssertValid();
}

void CAIView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CAIDoc* CAIView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAIDoc)));
	return (CAIDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAIView message handlers

void CAIView::OnInitialUpdate() 
{
 	CView::OnInitialUpdate();
 
 	CSize sizeTotal;
 	sizeTotal.cx = sizeTotal.cy = 100;
 	SetScrollSizes(MM_TEXT, sizeTotal);
 
}


void CAIView::OnAstar() 
{
	// TODO: Add your command handler code here
	m_bIsDispAll=false;
	IsBFS=false;
	CSearchAlg MainProcess;
	if(MainProcess.LoadData(false)) 
	{
		List *pList = MainProcess.GetResultListPoint();
		if(pList!=NULL)
			this->GenerateDispList(pList);//生成显示链表
		else
			AfxMessageBox("计算步骤过多,搜索失败。请重新输入初始状态");


	}
 	Invalidate();
	
}

void CAIView::OnAstarinput() 
{
	// TODO: Add your command handler code here
	IsBFS=false;
	CSearchAlg MainProcess;
	if(MainProcess.LoadData(true)) 
	{
		List *pList = MainProcess.GetResultListPoint();
		if(pList!=NULL)
			this->GenerateDispList(pList);//生成显示链表
		else
			AfxMessageBox("搜索步骤过多,搜索失败");


	}
 	Invalidate();
	
	
}

void CAIView::GenerateDispList(List *pScrList)
{
	if(m_DispList.GetCount() > 0) m_DispList.RemoveAll();

	POSITION pos = pScrList->GetHeadPosition();
	while(pos)
	{
		CNodeState * SrcItem = (CNodeState *)pScrList->GetNext(pos);
		this->m_DispList.AddTail(SrcItem);
	}
}

void CAIView::OnViewStep() 
{
	// TODO: Add your command handler code here
	this->m_bIsDispAll = FALSE;
	Invalidate();

}

void CAIView::OnViewTree() 
{
	// TODO: Add your command handler code here
	this->m_bIsDispAll = TRUE;
	Invalidate();
	
}


void CAIView::OnUpdateViewStep(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(!m_bIsDispAll);
}

void CAIView::OnUpdateViewTree(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_bIsDispAll);
}



void CAIView::OnBfs() 
{
	// TODO: Add your command handler code here
	IsBFS=true;
	m_bIsDispAll=true;
	CBfsAlg MainProcess;
	if(MainProcess.LoadData(false)) 
	{
		List *pList = MainProcess.GetResultListPoint();
		if(pList!=NULL)
			this->GenerateDispList(pList);//生成显示链表
		else
			AfxMessageBox("搜索步骤太多,BFS失败。请输入其它状态!");


	}
 	Invalidate();
	
}

void CAIView::DoDataExchange(CDataExchange* pDX) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CScrollView::DoDataExchange(pDX);
}

⌨️ 快捷键说明

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