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

📄 xongdlg.cpp

📁 8数码问题
💻 CPP
字号:
// xongDlg.cpp : implementation file
//

#include "stdafx.h"
#include "xong.h"
#include "xongDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CXongDlg dialog

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

    ////////////////////////////


    Paths = new KTList<RULETYPE>;
    


}
bool CXongDlg::GetBData(int mData[][3])
{
    CString s;
	for(int i=0;i<=8;i++)
	{
		m_b[i].GetWindowText(s);
        if(s.Compare("")==0)
		{
			mData[i/3][i%3]=0;
		}
		else
		{
			mData[i/3][i%3]=::atoi(s.GetBuffer(0));
		}
	}
		return true;

}


bool CXongDlg::GetEData(int mData[][3])
{
     CString s;
	for(int i=0;i<=8;i++)
	{
		m_e[i].GetWindowText(s);
        if(s.Compare("")==0)
		{
			mData[i/3][i%3]=0;
		}
		else
		{
			mData[i/3][i%3]=::atoi(s.GetBuffer(0));
		}
	}
		return true;

}

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

	DDX_Control(pDX, IDC_info, m_info);
 

	DDX_Control(pDX, IDC_x8, m_x[8]);
	DDX_Control(pDX, IDC_x6, m_x[6]);
	DDX_Control(pDX, IDC_x7, m_x[7]);
	DDX_Control(pDX, IDC_x5, m_x[5]);
	DDX_Control(pDX, IDC_x4, m_x[4]);
	DDX_Control(pDX, IDC_x3, m_x[3]);
	DDX_Control(pDX, IDC_x2, m_x[2]);
	DDX_Control(pDX, IDC_x1, m_x[1]);
	DDX_Control(pDX, IDC_x0, m_x[0]);

	DDX_Control(pDX, IDC_b8, m_b[8]);
	DDX_Control(pDX, IDC_b6, m_b[6]);
	DDX_Control(pDX, IDC_b7, m_b[7]);
	DDX_Control(pDX, IDC_b5, m_b[5]);
	DDX_Control(pDX, IDC_b4, m_b[4]);
	DDX_Control(pDX, IDC_b3, m_b[3]);
	DDX_Control(pDX, IDC_b2, m_b[2]);
	DDX_Control(pDX, IDC_b1, m_b[1]);
	DDX_Control(pDX, IDC_b0, m_b[0]);

	DDX_Control(pDX, IDC_e8, m_e[8]);
	DDX_Control(pDX, IDC_e6, m_e[6]);
	DDX_Control(pDX, IDC_e7, m_e[7]);
	DDX_Control(pDX, IDC_e5, m_e[5]);
	DDX_Control(pDX, IDC_e4, m_e[4]);
	DDX_Control(pDX, IDC_e3, m_e[3]);
	DDX_Control(pDX, IDC_e2, m_e[2]);
	DDX_Control(pDX, IDC_e1, m_e[1]);
	DDX_Control(pDX, IDC_e0, m_e[0]);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CXongDlg, CDialog)
	//{{AFX_MSG_MAP(CXongDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_compute, Oncompute)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_demo, Ondemo)
	ON_BN_CLICKED(IDC_priv, Onpriv)
	ON_BN_CLICKED(IDC_b0, Onb0)
	ON_BN_CLICKED(IDC_b1, Onb1)
	ON_BN_CLICKED(IDC_b2, Onb2)
	ON_BN_CLICKED(IDC_b3, Onb3)
	ON_BN_CLICKED(IDC_b4, Onb4)
	ON_BN_CLICKED(IDC_b5, Onb5)
	ON_BN_CLICKED(IDC_b6, Onb6)
	ON_BN_CLICKED(IDC_b7, Onb7)
	ON_BN_CLICKED(IDC_b8, Onb8)
	ON_BN_CLICKED(IDC_e0, One0)
	ON_BN_CLICKED(IDC_e1, One1)
	ON_BN_CLICKED(IDC_e2, One2)
	ON_BN_CLICKED(IDC_e3, One3)
	ON_BN_CLICKED(IDC_e4, One4)
	ON_BN_CLICKED(IDC_e5, One5)
	ON_BN_CLICKED(IDC_e6, One6)
	ON_BN_CLICKED(IDC_e7, One7)
	ON_BN_CLICKED(IDC_e8, One8)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CXongDlg message handlers





BOOL CXongDlg::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
	 
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CXongDlg::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 CXongDlg::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();
	}
}

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

 int CXongDlg::FindbEmpty()
 {
	 CString s;
	 for(int i=0;i<=8;i++)
	 {
		 m_b[i].GetWindowText(s);
		 if(s.Compare("")==0)
		 {
			 return i;
		 }
	 }
	 return -1;
 }
	int CXongDlg::FindeEmpty()
	{
		CString s;
	 for(int i=0;i<=8;i++)
	 {
		 m_e[i].GetWindowText(s);
		 if(s.Compare("")==0)
		 {
			 return i;
		 }
	 }
	 return -1;
	}

	void CXongDlg::Swapb(int i,int j)
	{
           CString si,sj;
		   m_b[i].GetWindowText(si);m_b[j].GetWindowText(sj);
		   m_b[i].SetWindowText(sj);m_b[j].SetWindowText(si);
	}
	void CXongDlg::Swape(int i,int j)
	{
           CString si,sj;
		   m_e[i].GetWindowText(si);m_e[j].GetWindowText(sj);
		   m_e[i].SetWindowText(sj);m_e[j].SetWindowText(si);
	}

void CXongDlg::Oncompute() 
{
	// TODO: Add your control notification handler code here

   int mData[3][3];
   //从界面得到起始状态
  if(! GetBData(mData))
  {
	  AfxMessageBox("开始状态不全!");
	  return ;
  }
   //设置起始状态的数据
   ResetBData(mData);

   KCopy(NData,mData); //当前状态

   //从界面得到目标状态
  if(! GetEData(mData))
  {
	  AfxMessageBox("结束状态不全!");
	  return;
  }
  
  
  //设置目标状态的数据
   ResetEData(mData);

   Paths->Empty(); //清空规则序列


    if (BackTrack(NData,0)) {
      //成功,设置界面导航
#ifdef _Debug
  
#endif
		CString s;
		s.Format("%d",Paths->GetCount());
	  AfxMessageBox(s);
      AfxMessageBox("计算成功!");

   Paths->First();
   KCopy(NData,BData);
   CurrentStep=0;
     s.Format("%d/%d",CurrentStep,Paths->GetCount());
	   m_info.SetWindowText(s);

   } else
	  AfxMessageBox("计算失败!");


}

void CXongDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	delete Paths;
}

void CXongDlg::Ondemo() 
{
	// TODO: Add your control notification handler code here
   RULETYPE mR;
   
   if (!Paths->IsEof())
   {
      Paths->GetData(mR);
      Gen(mR,NData);
      CString s;
	  for(int i=0;i<3;i++)
	  {
		  for(int j=0;j<3;j++)
		  {
                  s.Format("%d",NData[i][j]);
				  if(NData[i][j]==0) s="";
				  m_x[i*3+j].SetWindowText(s);
		  } 
	  }
     
      Paths->Next();
      CurrentStep = CurrentStep + 1;
    //   m_demo.SetWindowText(s);
       s.Format("%d/%d",CurrentStep,Paths->GetCount());
	   m_info.SetWindowText(s);
   }
  else
      AfxMessageBox("这已经是最后一步了,不能再向下走了!!");
}

void CXongDlg::Onpriv() 
{
	// TODO: Add your control notification handler code here
	
   RULETYPE mR;

   if (!Paths->IsHead()) {
      Paths->Prior();
      Paths->GetData(mR);
      Gen(Reverse(mR),NData);
      CurrentStep = CurrentStep - 1;
      CString s;
	  for(int i=0;i<3;i++)
	  {
		  for(int j=0;j<3;j++)
		  {
                  s.Format("%d",NData[i][j]);
				  if(NData[i][j]==0) s="";
				  m_x[i*3+j].SetWindowText(s);
		  } 
	  }
	   s.Format("%d/%d",CurrentStep,Paths->GetCount());
	   m_info.SetWindowText(s);
   }
   else
      AfxMessageBox("这是第一步!");
}

void CXongDlg::Onb0() 
{
	// TODO: Add your control notification handler code here
	Swapb(0,FindbEmpty());
}

void CXongDlg::Onb1() 
{
	// TODO: Add your control notification handler code here
		Swapb(1,FindbEmpty());
}

void CXongDlg::Onb2() 
{
	// TODO: Add your control notification handler code here
		Swapb(2,FindbEmpty());
}

void CXongDlg::Onb3() 
{
	// TODO: Add your control notification handler code here
		Swapb(3,FindbEmpty());
}

void CXongDlg::Onb4() 
{
	// TODO: Add your control notification handler code here
		Swapb(4,FindbEmpty());
}

void CXongDlg::Onb5() 
{
	// TODO: Add your control notification handler code here
		Swapb(5,FindbEmpty());
}

void CXongDlg::Onb6() 
{
	// TODO: Add your control notification handler code here
		Swapb(6,FindbEmpty());
}

void CXongDlg::Onb7() 
{
	// TODO: Add your control notification handler code here
		Swapb(7,FindbEmpty());
}

void CXongDlg::Onb8() 
{
	// TODO: Add your control notification handler code here
		Swapb(8,FindbEmpty());
}

void CXongDlg::One0() 
{
	// TODO: Add your control notification handler code here
		Swape(0,FindeEmpty());
}

void CXongDlg::One1() 
{
	// TODO: Add your control notification handler code here
		Swape(1,FindeEmpty());
}

void CXongDlg::One2() 
{
	// TODO: Add your control notification handler code here
		Swape(2,FindeEmpty());
}

void CXongDlg::One3() 
{
	// TODO: Add your control notification handler code here
		Swape(3,FindeEmpty());
}

void CXongDlg::One4() 
{
	// TODO: Add your control notification handler code here
		Swape(4,FindeEmpty());
}

void CXongDlg::One5() 
{
	// TODO: Add your control notification handler code here
		Swape(5,FindeEmpty());
}

void CXongDlg::One6() 
{
	// TODO: Add your control notification handler code here
		Swape(6,FindeEmpty());
}

void CXongDlg::One7() 
{
	// TODO: Add your control notification handler code here
		Swape(7,FindeEmpty());
}

void CXongDlg::One8() 
{
	// TODO: Add your control notification handler code here
	Swape(8,FindeEmpty());
}

⌨️ 快捷键说明

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