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

📄 w_8modedlg.cpp

📁 八数码游戏 visual c
💻 CPP
字号:
// W_8modeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "W_8mode.h"
#include "W_8modeDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//CBitmap picture[3][3];
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()

/////////////////////////////////////////////////////////////////////////////
// CW_8modeDlg dialog

CW_8modeDlg::CW_8modeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CW_8modeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CW_8modeDlg)
		// 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);

}

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

BEGIN_MESSAGE_MAP(CW_8modeDlg, CDialog)
	//{{AFX_MSG_MAP(CW_8modeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CW_8modeDlg message handlers

BOOL CW_8modeDlg::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
	inite();
	for(int i = 0;i<3;i++)
		for(int j = 0;j<3;j++)
			state[i][j] = i*3+j+1;
		state[2][1] = 0;
		state[2][2] = 8;
	Loading();
	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CW_8modeDlg::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 CW_8modeDlg::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
	{
		CDialog::OnPaint();
	}
//	Loading();
}

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

void CW_8modeDlg::OnDraw(CDC* pDC,CBitmap picture[3][3])
{
     CBitmap bitmap;
     CDC dcMemory;
     dcMemory.CreateCompatibleDC(pDC);
	 for(int i = 0;i<3;i++)
	 {
		 for(int j = 0;j<3;j++)
		 {
			 dcMemory.SelectObject(&picture[j][i]);
			 pDC->BitBlt(50+i*50,50+j*50,50,50,&dcMemory,0,0,SRCCOPY);
		 }
	 }
}


void CW_8modeDlg::OnOK() 
{
	//CDC* pDC;
	//pDC = GetDC();
	//OnDraw(pDC);
	int sum = 0;
	CString i;
	GetDlgItemText(IDC_EDIT1,i);
	sum = sum*10+i.GetAt(0)-48;
	GetDlgItemText(IDC_EDIT2,i);
	sum = sum*10+i.GetAt(0)-48;
	GetDlgItemText(IDC_EDIT3,i);
	sum = sum*10+i.GetAt(0)-48;
	GetDlgItemText(IDC_EDIT4,i);
	sum = sum*10+i.GetAt(0)-48;
	GetDlgItemText(IDC_EDIT5,i);
	sum = sum*10+i.GetAt(0)-48;
	GetDlgItemText(IDC_EDIT6,i);
	sum = sum*10+i.GetAt(0)-48;
	GetDlgItemText(IDC_EDIT7,i);
	sum = sum*10+i.GetAt(0)-48;
	GetDlgItemText(IDC_EDIT8,i);
	sum = sum*10+i.GetAt(0)-48;
	GetDlgItemText(IDC_EDIT9,i);
	sum = sum*10+i.GetAt(0)-48;
	findit(sum);
}

void CW_8modeDlg::inite()
{
	length = 0;
	stateInt = staArray[length] = 123456708;
	end_mode.parent = -1;
	zero_pos_r = 2;
	zero_pos_c = 1;
	my_mode.insert(hash_mode::value_type(stateInt,end_mode));


	for(int i = 0;i<=length;i++)
	{
		stateInt = staArray[i];
		toArray(stateInt);
		for(int k = 0;k<3;k++)
			for(int l = 0;l<3;l++)
				if(state[k][l] ==0)
				{
					zero_pos_r = k;
					zero_pos_c = l;
					break;
				}
		
		if(zero_pos_r>0)
			searchUp();
		if(zero_pos_c>0)
			searchLeft();
		if(zero_pos_r<2)
			searchDown();
		if(zero_pos_c<2)
			searchRight();			
	}
	
}

void CW_8modeDlg::toArray(int sum)
{
	for(int i = 2; i >= 0; i--)
		for( int j = 2; j >= 0; j--)
		{
			state[i][j] = sum % 10;
			sum /= 10;
		}
}
void CW_8modeDlg::searchUp()
{

	int sum = 0,temp;
	temp = state[zero_pos_r-1][zero_pos_c];
	state[zero_pos_r-1][zero_pos_c] = state[zero_pos_r][zero_pos_c];
	state[zero_pos_r][zero_pos_c] = temp;
	for(int i = 0; i < 3;i++)
		for(int j = 0;j <3; j++)
			sum = sum*10+state[i][j];
	temp = state[zero_pos_r-1][zero_pos_c];
	state[zero_pos_r-1][zero_pos_c] = state[zero_pos_r][zero_pos_c];
	state[zero_pos_r][zero_pos_c] = temp;
	hash_mode::iterator point;
	point = my_mode.find(sum);		
	if(point != my_mode.end())
		return;
	_8mode tmp;
	tmp.parent = stateInt;
	my_mode.insert(hash_mode::value_type(sum,tmp));	
	length++;
	staArray[length] = sum;
}
void CW_8modeDlg::searchLeft()
{

	int sum = 0,temp;
	temp = state[zero_pos_r][zero_pos_c-1];
	state[zero_pos_r][zero_pos_c-1] = state[zero_pos_r][zero_pos_c];
	state[zero_pos_r][zero_pos_c] = temp;
	for(int i = 0; i < 3;i++)
		for(int j = 0;j <3; j++)
			sum = sum*10+state[i][j];
	temp = state[zero_pos_r][zero_pos_c-1];
	state[zero_pos_r][zero_pos_c-1] = state[zero_pos_r][zero_pos_c];
	state[zero_pos_r][zero_pos_c] = temp;
	hash_mode::iterator point;
	point = my_mode.find(sum);		
	if(point != my_mode.end())
		return;
	_8mode tmp;
	tmp.parent = stateInt;
	my_mode.insert(hash_mode::value_type(sum,tmp));	
	length++;
	staArray[length] = sum;
}
void CW_8modeDlg::searchRight()
{

	int sum = 0,temp;
	temp = state[zero_pos_r][zero_pos_c+1];
	state[zero_pos_r][zero_pos_c+1] = state[zero_pos_r][zero_pos_c];
	state[zero_pos_r][zero_pos_c] = temp;
	for(int i = 0; i < 3;i++)
		for(int j = 0;j <3; j++)
			sum = sum*10+state[i][j];
	temp = state[zero_pos_r][zero_pos_c+1];
	state[zero_pos_r][zero_pos_c+1] = state[zero_pos_r][zero_pos_c];
	state[zero_pos_r][zero_pos_c] = temp;
	hash_mode::iterator point;
	point = my_mode.find(sum);		
	if(point != my_mode.end())
		return;
	_8mode tmp;
	tmp.parent = stateInt;
	my_mode.insert(hash_mode::value_type(sum,tmp));
	length++;
	staArray[length] = sum;
}
void CW_8modeDlg::searchDown()
{

	int sum = 0,temp;
	temp = state[zero_pos_r+1][zero_pos_c];
	state[zero_pos_r+1][zero_pos_c] = state[zero_pos_r][zero_pos_c];
	state[zero_pos_r][zero_pos_c] = temp;
	for(int i = 0; i < 3;i++)
		for(int j = 0;j <3; j++)
			sum = sum*10+state[i][j];
	temp = state[zero_pos_r+1][zero_pos_c];
	state[zero_pos_r+1][zero_pos_c] = state[zero_pos_r][zero_pos_c];
	state[zero_pos_r][zero_pos_c] = temp;
	hash_mode::iterator point;
	point = my_mode.find(sum);		
	if(point != my_mode.end())
		return;
	_8mode tmp;
	tmp.parent = stateInt;
	my_mode.insert(hash_mode::value_type(sum,tmp));	
	length++;
	staArray[length] = sum;
}

void CW_8modeDlg::findit(int sum)
{
	hash_mode::iterator point;
	point = my_mode.find(sum);
	if(point == my_mode.end())
	{
		AfxMessageBox("Can not find it");
		return;
	}
	
	toArray(sum);
	Loading();
	AfxMessageBox("Let us go!");
	int i=0;
	while(1)
	{
		::Sleep(1500);
		i++;
		point = my_mode.find(point->second.parent);
		if(point->second.parent == -1)
			return;
		sum = point->second.parent;
		toArray(sum);
		Loading();
		
		
	}
	AfxMessageBox("Game over!");
}
void CW_8modeDlg::Loading()
{
	CBitmap picture[3][3];
	for(int i = 0;i<3;i++)
		for(int j = 0;j<3;j++)
		{
			switch(state[i][j])
			{
			case 0:picture[i][j].LoadBitmap(IDB_BITMAP0);break;
			case 1:picture[i][j].LoadBitmap(IDB_BITMAP1);break;
			case 2:picture[i][j].LoadBitmap(IDB_BITMAP2);break;
			case 3:picture[i][j].LoadBitmap(IDB_BITMAP3);break;
			case 4:picture[i][j].LoadBitmap(IDB_BITMAP4);break;
			case 5:picture[i][j].LoadBitmap(IDB_BITMAP5);break;
			case 6:picture[i][j].LoadBitmap(IDB_BITMAP6);break;
			case 7:picture[i][j].LoadBitmap(IDB_BITMAP7);break;
			case 8:picture[i][j].LoadBitmap(IDB_BITMAP8);break;
			default:AfxMessageBox("error");
			
			}
		}
	CDC* pDC = GetDC();
	OnDraw(pDC,picture);
}

⌨️ 快捷键说明

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