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

📄 eightnumdlg.cpp

📁 绝对原创!用的是A*算法.解决8数码问题.绝对可用.初学者可以看看.
💻 CPP
字号:
// EightNumDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EightNum.h"
#include "EightNumDlg.h"
#include "Algorithm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEightNumDlg dialog

CEightNumDlg::CEightNumDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEightNumDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEightNumDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_ImageList.Create(32,32,ILC_MASK,0,1);
	m_ImageTargetList.Create(32,32,ILC_MASK,0,1);
	m_pResult = new (CQNode*);
	*m_pResult = NULL;
	IsShowResult = FALSE;
}

void CEightNumDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEightNumDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEightNumDlg, CDialog)
	//{{AFX_MSG_MAP(CEightNumDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_WORK, OnWork)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEightNumDlg message handlers

BOOL CEightNumDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//加载图片文件
	HICON hIcon;
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON0);
    m_ImageList.Add(hIcon);
	m_ImageTargetList.Add(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
	m_ImageList.Add(hIcon);
	m_ImageTargetList.Add(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON2);
	m_ImageList.Add(hIcon);
	m_ImageTargetList.Add(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON3);
	m_ImageList.Add(hIcon);
	m_ImageTargetList.Add(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON4);
	m_ImageList.Add(hIcon);
	m_ImageTargetList.Add(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON5);
	m_ImageList.Add(hIcon);
	m_ImageTargetList.Add(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON6);
	m_ImageList.Add(hIcon);
	m_ImageTargetList.Add(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON7);
	m_ImageList.Add(hIcon);
	m_ImageTargetList.Add(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON8);
	m_ImageList.Add(hIcon);
	m_ImageTargetList.Add(hIcon);

	//初始化初始位置
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
		{
			m_pDataPos[i][j] = i*3+j;
            m_pTargetPos[i][j] = i*3+j;
		}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEightNumDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEightNumDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CPoint ptImage(10,10);
		CPoint ptTarget(100,10);
		int x = ptImage.x;
		int y = ptImage.y;
		CPaintDC dc(this);
		for(int i=0;i<3;i++)
		{
		  ptImage.y += 32;
		  ptImage.x = x;
		  ptTarget.x = x+230;
		  ptTarget.y += 32;
		  for(int j=0;j<3;j++)
		  {	
			ptImage.x += 32;
			m_ImageList.Draw(&dc,m_pDataPos[i][j],ptImage,ILD_NORMAL);
			ptTarget.x += 32;
			m_ImageTargetList.Draw(&dc,m_pTargetPos[i][j],ptTarget,ILD_NORMAL);
		  }
		}
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEightNumDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEightNumDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//CRect pRect;
	int i,j;
	if(!IsShowResult) //如果没有在显示结果
	{
    for(i=0;i<3;i++) //如果改变的位置是显示结果的区域
	{
		for(j=0;j<3;j++)
		{
		//	pRect.SetRect(10+i*32,10+j*32,42+i*32,42+j*32);
			CRect pRect(10+j*32,10+i*32,42+j*32,42+i*32);
            
			CPoint pt;
			pt.x = point.x-32;
			pt.y = point.y-32;
			if(pRect.PtInRect(pt))
			{
                if(m_pDataPos[i][j] == 0) //如果是空白块,什么都不做
					break;
				else
				{
					int k,t; //找到空白块的位置
					for(int m=0;m<3;m++)
					{
						for(int n=0;n<3;n++)
						{
							if(m_pDataPos[m][n] == 0)
							{
								k = m;
								t = n;
								break;
							}
						}
					    if(n<3)break;
					}

					//判断当前块和空白块的关系
					if((i-1==k && j==t) || (i+1==k && j==t) ||
						(i==k && j-1==t) || (i==k && j+1==t)) //相邻
					{
                        //交换
						int temp;
						temp = m_pDataPos[i][j];
						m_pDataPos[i][j] = m_pDataPos[k][t];
						m_pDataPos[k][t] = temp;

						Invalidate(FALSE);
					}
					break;
				}
				
			}

		}
		if(j<3) break;
	}

	if(i==3 && j==3) //如果改变的区域是目标区域
	{
		for(i=0;i<3;i++)
		{
		   for(j=0;j<3;j++)
		   {
	    	//	pRect.SetRect(10+i*32,10+j*32,42+i*32,42+j*32);
			CRect pRect(10+j*32+230,10+i*32,42+j*32+230,42+i*32);
            
			CPoint pt;
			pt.x = point.x-32;
			pt.y = point.y-32;
			if(pRect.PtInRect(pt))
			{
                if(m_pTargetPos[i][j] == 0) //如果是空白块,什么都不做
					break;
				else
				{
					int k,t; //找到空白块的位置
					for(int m=0;m<3;m++)
					{
						for(int n=0;n<3;n++)
						{
							if(m_pTargetPos[m][n] == 0)
							{
								k = m;
								t = n;
								break;
							}
						}
					    if(n<3)break;
					}

					//判断当前块和空白块的关系
					if((i-1==k && j==t) || (i+1==k && j==t) ||
						(i==k && j-1==t) || (i==k && j+1==t)) //相邻
					{
                        //交换
						int temp;
						temp = m_pTargetPos[i][j];
						m_pTargetPos[i][j] = m_pTargetPos[k][t];
						m_pTargetPos[k][t] = temp;

						Invalidate(FALSE);
					}
					break;
				}
				
			}
		   }
		   if(j<3) break;
		}
	}
    }
	CDialog::OnLButtonDown(nFlags, point);
}

void CEightNumDlg::OnWork() 
{
	// TODO: Add your control notification handler code here
	int initPos[9];
	int targetPos[9];

	memcpy(initPos,m_pDataPos,sizeof(int)*9);
	memcpy(targetPos,m_pTargetPos,sizeof(int)*9);
	BeginWaitCursor();
	if(ASearch(initPos,targetPos,m_pResult,Q)) //执行A*算法
	{
		EndWaitCursor();
		MessageBox("我已经知道结果啦!^_^","Message",MB_OK);
		SetTimer(0,500,NULL);
		IsShowResult = TRUE;
		m_pMark = *m_pResult;
	}
}

void CEightNumDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_pMark!=NULL)
	{
		memcpy(m_pDataPos,m_pMark->m_nElemData,sizeof(int)*9);
		m_pMark = m_pMark->nextResult;
		Invalidate(FALSE);
	}
    else
	{	
		IsShowResult = FALSE;
		KillTimer(0);
		MessageBox("OK!^_^","Message",MB_OK);
		Q.Destroy();
	}
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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