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

📄 enterview.cpp

📁 这里有一个用vc写的人工智能八数码源代码
💻 CPP
字号:
// EnterView.cpp : implementation file
//

#include "stdafx.h"
#include "Chess.h"
#include "EnterView.h"
#include "ChessDoc.h"
#include "ctype.h"


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

/////////////////////////////////////////////////////////////////////////////
// CEnterView

IMPLEMENT_DYNCREATE(CEnterView, CFormView)

CEnterView::CEnterView()
	: CFormView(CEnterView::IDD)
{
	//AfxMessageBox("CEnterView");
	//CChessDoc *pDoc=(CChessDoc*)GetDocument();
	//if(pDoc->m_bFileOpen)
	//	return;
	m_N=3;
	m_NUM=9;
	
	//UpdateData(FALSE);
	//{{AFX_DATA_INIT(CEnterView)
	m_nType = 1;
	m_strEnd33_0 = _T("1");
	m_strEnd33_1 = _T("2");
	m_strEnd33_2 = _T("3");
	m_strEnd33_3 = _T("8");
	m_strEnd33_4 = _T("0");
	m_strEnd33_5 = _T("4");
	m_strEnd33_6 = _T("7");
	m_strEnd33_7 = _T("6");
	m_strEnd33_8 = _T("5");
	m_strStart33_0 = _T("2");
	m_strStart33_1 = _T("8");
	m_strStart33_2 = _T("3");
	m_strStart33_3 = _T("1");
	m_strStart33_4 = _T("6");
	m_strStart33_5 = _T("4");
	m_strStart33_6 = _T("7");
	m_strStart33_7 = _T("0");
	m_strStart33_8 = _T("5");	
	m_nScale = 0;
	m_nDepth = 2000;
	//}}AFX_DATA_INIT
	
}

CEnterView::~CEnterView()
{
}

void CEnterView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEnterView)
	DDX_Control(pDX, IDC_EDIT_END_33_1, m_gridEnd33_1);
	DDX_Control(pDX, IDC_EDIT_END_33_0, m_gridEnd33_0);
	DDX_Control(pDX, IDC_EDIT_END_33_2, m_gridEnd33_2);
	DDX_Control(pDX, IDC_EDIT_END_33_3, m_gridEnd33_3);
	DDX_Control(pDX, IDC_EDIT_END_33_4, m_gridEnd33_4);
	DDX_Control(pDX, IDC_EDIT_END_33_5, m_gridEnd33_5);
	DDX_Control(pDX, IDC_EDIT_END_33_6, m_gridEnd33_6);
	DDX_Control(pDX, IDC_EDIT_END_33_7, m_gridEnd33_7);
	DDX_Control(pDX, IDC_EDIT_END_33_8, m_gridEnd33_8);
	DDX_Control(pDX, IDC_EDIT_START_33_0, m_gridStart33_0);
	DDX_Control(pDX, IDC_EDIT_START_33_1, m_gridStart33_1);
	DDX_Control(pDX, IDC_EDIT_START_33_2, m_gridStart33_2);
	DDX_Control(pDX, IDC_EDIT_START_33_3, m_gridStart33_3);
	DDX_Control(pDX, IDC_EDIT_START_33_4, m_gridStart33_4);
	DDX_Control(pDX, IDC_EDIT_START_33_5, m_gridStart33_5);
	DDX_Control(pDX, IDC_EDIT_START_33_6, m_gridStart33_6);
	DDX_Control(pDX, IDC_EDIT_START_33_7, m_gridStart33_7);
	DDX_Control(pDX, IDC_EDIT_START_33_8, m_gridStart33_8);
	DDX_Control(pDX, IDC_COMBO_TYPE, m_cboType);
	DDX_CBIndex(pDX, IDC_COMBO_TYPE, m_nType);
	DDX_Text(pDX, IDC_EDIT_END_33_0, m_strEnd33_0);
	DDV_MaxChars(pDX, m_strEnd33_0, 1);
	DDX_Text(pDX, IDC_EDIT_END_33_1, m_strEnd33_1);
	DDV_MaxChars(pDX, m_strEnd33_1, 1);
	DDX_Text(pDX, IDC_EDIT_END_33_2, m_strEnd33_2);
	DDV_MaxChars(pDX, m_strEnd33_2, 1);
	DDX_Text(pDX, IDC_EDIT_END_33_3, m_strEnd33_3);
	DDV_MaxChars(pDX, m_strEnd33_3, 1);
	DDX_Text(pDX, IDC_EDIT_END_33_4, m_strEnd33_4);
	DDV_MaxChars(pDX, m_strEnd33_4, 1);
	DDX_Text(pDX, IDC_EDIT_END_33_5, m_strEnd33_5);
	DDV_MaxChars(pDX, m_strEnd33_5, 1);
	DDX_Text(pDX, IDC_EDIT_END_33_6, m_strEnd33_6);
	DDV_MaxChars(pDX, m_strEnd33_6, 1);
	DDX_Text(pDX, IDC_EDIT_END_33_7, m_strEnd33_7);
	DDV_MaxChars(pDX, m_strEnd33_7, 1);
	DDX_Text(pDX, IDC_EDIT_END_33_8, m_strEnd33_8);
	DDV_MaxChars(pDX, m_strEnd33_8, 1);
	DDX_Text(pDX, IDC_EDIT_START_33_0, m_strStart33_0);
	DDV_MaxChars(pDX, m_strStart33_0, 1);
	DDX_Text(pDX, IDC_EDIT_START_33_1, m_strStart33_1);
	DDV_MaxChars(pDX, m_strStart33_1, 1);
	DDX_Text(pDX, IDC_EDIT_START_33_2, m_strStart33_2);
	DDV_MaxChars(pDX, m_strStart33_2, 1);
	DDX_Text(pDX, IDC_EDIT_START_33_3, m_strStart33_3);
	DDV_MaxChars(pDX, m_strStart33_3, 1);
	DDX_Text(pDX, IDC_EDIT_START_33_4, m_strStart33_4);
	DDV_MaxChars(pDX, m_strStart33_4, 1);
	DDX_Text(pDX, IDC_EDIT_START_33_5, m_strStart33_5);
	DDV_MaxChars(pDX, m_strStart33_5, 1);
	DDX_Text(pDX, IDC_EDIT_START_33_6, m_strStart33_6);
	DDV_MaxChars(pDX, m_strStart33_6, 1);
	DDX_Text(pDX, IDC_EDIT_START_33_7, m_strStart33_7);
	DDV_MaxChars(pDX, m_strStart33_7, 1);
	DDX_Text(pDX, IDC_EDIT_START_33_8, m_strStart33_8);
	DDV_MaxChars(pDX, m_strStart33_8, 1);
	DDX_Radio(pDX, IDC_RADIO_TYPE33, m_nScale);
	DDX_Text(pDX, IDC_EDIT_DEPTH, m_nDepth);
	DDV_MinMaxUInt(pDX, m_nDepth, 0, 32767);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CEnterView, CFormView)
	//{{AFX_MSG_MAP(CEnterView)
	ON_BN_CLICKED(IDC_BUTTON_BEGIN, OnButtonBegin)
	ON_CBN_SELCHANGE(IDC_COMBO_TYPE, OnSelchangeComboType)
	ON_BN_CLICKED(IDC_RADIO_TYPE33, OnRadioType33)
	ON_EN_CHANGE(IDC_EDIT_DEPTH, OnChangeEditDepth)
	//ON_EN_CHANGE(IDC_EDIT_END_33_4, OnChangeEditEnd334)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEnterView diagnostics

#ifdef _DEBUG
void CEnterView::AssertValid() const
{
	CFormView::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CEnterView message handlers

//DEL void CEnterView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
//DEL {
//DEL 	// TODO: Add your message handler code here and/or call default
//DEL 	AfxMessageBox("aaa");
//DEL 	//CFormView::OnChar(nChar, nRepCnt, nFlags);
//DEL }

void CEnterView::OnButtonBegin() //开始搜索
{
	int i;
	unsigned char buf[15];

	CString tempChar;
	CChessDoc* pDoc=(CChessDoc*)GetDocument();
	CEdit *pEdit;
	UpdateData();
	for(i=0;i<m_NUM;i++)    //接收初始状态
	{

		if(m_N==3)
			pEdit=(CEdit*)GetDlgItem(IDC_EDIT_START_33_0+i);
	
		
		pEdit->GetWindowText(tempChar);
		if(tempChar.IsEmpty()) buf[i]='0';
		else 
		{
			if (isdigit(tempChar[0]))
				buf[i]=tempChar[0];
			else
				buf[i]=tempChar[0];
		}
	}
	if(ValidateGrid(buf,m_NUM))
	{
		memcpy(pDoc->m_strStart ,buf,m_NUM);   //把有效的数字传给初始类
		pDoc->m_N=m_N;
		pDoc->m_NUM=m_NUM;
	}
	else
	{
		AfxMessageBox("棋盘初始布局出错,请重新检查!");
		return;
	}
	for(i=0;i<m_NUM;i++)
	{               //接收结束状态的数
		if (m_N==3)
			pEdit=(CEdit*)GetDlgItem(IDC_EDIT_END_33_0+i);

		pEdit->GetWindowText(tempChar);
		if(tempChar.IsEmpty()) buf[i]='0';
		else 
		{
			if (isdigit(tempChar[0]))
				buf[i]=tempChar[0];
			else
				buf[i]=tempChar[0];
		}

	}
	if(ValidateGrid(buf,m_NUM))
		memcpy(pDoc->m_strEnd,buf,m_NUM);//把有效数字传给结束状态的类
	else
	{
		AfxMessageBox("棋盘目标布局出错,请重新检查!");
		return;
	}

	//char tempbuf[80];
	//sprintf(tempbuf,"%s\n%s",pDoc->m_strStart,pDoc->m_strEnd);
	//AfxMessageBox(tempbuf);
	if(memcmp(pDoc->m_strStart,pDoc->m_strEnd,m_NUM)==0 )//判断结束和初始是否相同
	{
		AfxMessageBox("棋盘初始布局和目标布局相同!");
		return;
	}
	else if(pDoc->Test(pDoc->m_strStart,pDoc->m_strEnd,m_N,m_NUM)!=0)//判断初始和结束是否可达
	{
		AfxMessageBox("不能由棋盘初始布局到达目标布局!");
		return;
	}
	pDoc->m_nResult=0;
	pDoc->m_nType=m_nType;//标识移动方法
	pDoc->m_N=m_N;
	pDoc->m_NUM=m_NUM;
	pDoc->m_bBeginDraw=TRUE;//画图
	pDoc->m_nDepth=m_nDepth;//记载走过多少步
	switch(m_nType)
	{
	case 0:
		pDoc->BFS(pDoc->m_strStart,pDoc->m_strEnd );
		if(pDoc->m_nResult<=0)
		{
			AfxMessageBox("没有搜索到解路径!");
		}
		break;
	case 1: 
		pDoc->SearchDFS();
		if(pDoc->m_nResult<=0)
		{
			AfxMessageBox("没有搜索到解路径!");
		}
		break;
	case 2:
		pDoc->BFSA1(pDoc->m_strStart,pDoc->m_strEnd );
		if(pDoc->m_nResult<=0)
		{
			AfxMessageBox("没有搜索到解路径!");
		}
		break;
	case 3:
		pDoc->BFSA2(pDoc->m_strStart,pDoc->m_strEnd );
		if(pDoc->m_nResult<=0)
		{
			AfxMessageBox("没有搜索到解路径!");
		}
		break;
	default: break;
	}
	
	pDoc->UpdateAllViews(this);//更新所有视图
}

BOOL CEnterView::ValidateGrid( unsigned char* buf, UINT scale)//检查表格的有效性
{
	UINT *num=new UINT[scale];
	UINT i;   //无符号整型

	for(i=0;i<scale;i++) num[i]=0;
	
	for(i=0;i<scale;i++)
	{
		num[buf[i]-'0']++;  //检查数字是否重复
	}
	for(i=0;i<scale;i++)
	{
		if (num[i]!=1)     //数字有重复
		{
			delete[] num;
			return FALSE;
		}
	}
	delete[] num;
	return TRUE;
		
}

void CEnterView::OnSelchangeComboType() //记录选择算法的 按钮
{
	// TODO: Add your control notification handler code here
	m_nType=m_cboType.GetCurSel();

}

void CEnterView::OnRadioType33()//显示3×3数组 
{
	// TODO: Add your control notification handler code here
	CEdit*  pGrid;
	int i;
	UpdateData();
	if(m_nScale==0) 
	{
		m_N=3;
		m_NUM=9;
	}

	for(i=0;i<9;i++)
	{
		pGrid=(CEdit*)GetDlgItem(IDC_EDIT_START_33_0+i);//接收初始状态并且显示出来
		pGrid->ShowWindow(SW_SHOW);
		pGrid=(CEdit*)GetDlgItem(IDC_EDIT_END_33_0+i);//接收结束状态并且显示出来
		pGrid->ShowWindow(SW_SHOW);
	}

}
//DEL int CEnterView::Start2GoalL(unsigned char *start, unsigned char *goal)
//DEL {
//DEL 
//DEL }

void CEnterView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) //系统自动生成
{
	// TODO: Add your specialized code here and/or call the base class
	//AfxMessageBox("OnUpdate");
	CChessDoc* pDoc=(CChessDoc*)  GetDocument();

	if(pDoc->m_bFileOpen )//文件打开
	{	
	
		m_NUM=pDoc->m_NUM;
		m_N=pDoc->m_N;
		char buf[10];//缓冲区
		unsigned char * c=pDoc->m_strStart;
		if(m_N==3)//输出初始状态和结束状态
		{
			sprintf(buf,"%c",c[0]);
			m_strStart33_0=buf;
			sprintf(buf,"%c",c[1]);
			m_strStart33_1=buf;
			sprintf(buf,"%c",c[2]);
			m_strStart33_2=buf;
			sprintf(buf,"%c",c[3]);
			m_strStart33_3=buf;
			sprintf(buf,"%c",c[4]);
			m_strStart33_4=buf;
			sprintf(buf,"%c",c[5]);
			m_strStart33_5=buf;
			sprintf(buf,"%c",c[6]);
			m_strStart33_6=buf;
			sprintf(buf,"%c",c[7]);
			m_strStart33_7=buf;
			sprintf(buf,"%c",c[8]);
			m_strStart33_8=buf;

			c=pDoc->m_strEnd;
			sprintf(buf,"%c",c[0]);
			m_strEnd33_0=buf;
			sprintf(buf,"%c",c[1]);
			m_strEnd33_1=buf;
			sprintf(buf,"%c",c[2]);
			m_strEnd33_2=buf;
			sprintf(buf,"%c",c[3]);
			m_strEnd33_3=buf;
			sprintf(buf,"%c",c[4]);
			m_strEnd33_4=buf;
			sprintf(buf,"%c",c[5]);
			m_strEnd33_5=buf;
			sprintf(buf,"%c",c[6]);
			m_strEnd33_6=buf;
			sprintf(buf,"%c",c[7]);
			m_strEnd33_7=buf;
			sprintf(buf,"%c",c[8]);
			m_strEnd33_8=buf;
		}
	
		m_nScale=m_N-3;//重制

		UpdateData(FALSE);
		if (m_nScale==0)
			OnRadioType33();
	}
	else
	{
		m_nScale=0;//默认3×3
		OnRadioType33();
	}
	
}

void CEnterView::OnChangeEditDepth() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFormView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

//DEL void CEnterView::OnDraw(CDC* pDC) 
//DEL {
//DEL 	// TODO: Add your specialized code here and/or call the base class
//DEL 	CChessDoc* pDoc=(CChessDoc*)  GetDocument();
//DEL 	CEdit *pEdit;
//DEL 	if(pDoc->m_bFileOpen )
//DEL 	{
//DEL 		m_NUM=pDoc->m_NUM;
//DEL 		m_N=pDoc->m_N;
//DEL 		char tempchar[10];
//DEL 		
//DEL 		for(BYTE i=0;i<m_NUM;i++)
//DEL 		{
//DEL 			sprintf(tempchar,"%c",pDoc->m_strStart[i]);
//DEL 
//DEL 			if(m_N==3)
//DEL 				pEdit=(CEdit*)GetDlgItem(IDC_EDIT_START_33_0+i);
//DEL 			else 
//DEL 				pEdit=(CEdit*)GetDlgItem(IDC_EDIT_START0+i);
//DEL 			
//DEL 			if (tempchar[0]=='0') tempchar[0]=NULL;
//DEL 			pEdit->SetWindowText(tempchar);
//DEL 		}
//DEL 		
//DEL 		for(i=0;i<m_NUM;i++)
//DEL 		{
//DEL 			sprintf(tempchar,"%c",pDoc->m_strEnd[i]);
//DEL 			if (m_N==3)
//DEL 				pEdit=(CEdit*)GetDlgItem(IDC_EDIT_END_33_0+i);
//DEL 			else
//DEL 				pEdit=(CEdit*)GetDlgItem(IDC_EDIT_END0+i);
//DEL 			if(tempchar[0]=='0') tempchar[0]=NULL;
//DEL 			pEdit->SetWindowText(tempchar);
//DEL 				
//DEL 		}
//DEL 	}
//DEL 	
//DEL }

/*void CEnterView::OnChangeEditEnd334() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFormView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}*/

⌨️ 快捷键说明

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