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

📄 towermfcdlg.cpp

📁 用c++的MFC写的汉诺塔程序
💻 CPP
字号:
// TowerMfcDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CTowerMfcDlg dialog

CTowerMfcDlg::CTowerMfcDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTowerMfcDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTowerMfcDlg)
	m_stopnum = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	first=false;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	floor=0;				//初始化层数
	m_i=0;					//初始化下标
	m_first=false;			//初始化变量
	m_move=true;
	m_first_add=true;
	arrivetop=false;
	arrivedown=false;
	IsPop=false;
}

void CTowerMfcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTowerMfcDlg)
	DDX_Text(pDX, IDC_EDIT1, m_stopnum);
	DDV_MinMaxInt(pDX, m_stopnum, 0, 10000);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTowerMfcDlg, CDialog)
	//{{AFX_MSG_MAP(CTowerMfcDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTowerMfcDlg message handlers

BOOL CTowerMfcDlg::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
	CInitDlg dlg;
	dlg.DoModal();
	m_tower_first.m_floor=floor;		//初始化楼层
	m_tower_first.m_add=WEIDTH/floor*1.0;	//初始化增量
	CRect rect;
	GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&rect);//获得第一个柱子的位置
	ScreenToClient(&rect);							//转化坐标系
	int weidthtemp=WEIDTH;						
	for(int i=0;i<floor;i++)						//初始化第一个柱子用于开始的时候显示
	{
		RECT temp;
		temp.left=rect.left-weidthtemp;	
		temp.right=rect.right+weidthtemp;
		temp.bottom=rect.bottom;
		temp.top=rect.bottom-HEIGTH;
		rect.bottom-=HEIGTH;
		weidthtemp-=m_tower_first.m_add;
		m_tower[0].allrect.push_back(temp);
	}
	hanoi(floor,1,2,3);								//计算路径
	return TRUE;  // return TRUE  unless you set the focus to a control
}
void CTowerMfcDlg::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 CTowerMfcDlg::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();
	}
	CClientDC dc(this);
	//显示第一个塔
	/*for(int i=0;i<m_tower_first.allrect.size();i++)
	{
		m_tower_first.DrawRectangle(dc.m_hDC,m_tower_first.allrect[i]);
	}
	//显示第二个塔
	for( i=0;i<m_tower_second.allrect.size();i++)
	{
		m_tower_second.DrawRectangle(dc.m_hDC,m_tower_second.allrect[i]);
	}
	//显示第三个塔
	for(i=0;i<m_tower_three.allrect.size();i++)
	{
		m_tower_three.DrawRectangle(dc.m_hDC,m_tower_three.allrect[i]);
	}*/
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<m_tower[i].allrect.size();j++)
		{
			m_tower[i].DrawRectangle(dc.m_hDC,m_tower[i].allrect[j]);
		}
	}
}
// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTowerMfcDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//DEL void CTowerMfcDlg::OnAdd() 
//DEL {
//DEL 	// TODO: Add your control notification handler code her
//DEL }
void CTowerMfcDlg::hanoi(int n,int a,int b,int c)
{
	StartToEnd temp;
	if(n==1)
	{
	//	printf("%d->%d\n",a,c);
		temp.a=a;
		temp.b=c;
		temp.move=false;
		
		m_line.push_back(temp);
		
	//	m_line
	}
	else
	{
		hanoi(n-1,a,c,b);
	//	printf("%d->%d\n",a,c);
		temp.a=a;
		temp.b=c;
		temp.move=false;
		m_line.push_back(temp);
		hanoi(n-1,b,a,c);
	}
}
void CTowerMfcDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call defaul
	if(!arrivetop&&!m_line[m_i].move)
	{
		Up(m_line[m_i].a);
	}
	if(!m_line[m_i].move)
	{
		if(m_line[m_i].a<m_line[m_i].b)
		{
			MoveAToB(m_line[m_i].a,m_line[m_i].b);
		
			
		}
		else
		{
			MoveBToA(m_line[m_i].a,m_line[m_i].b);
			
		}
		
		
	}
	if(arrivedown&&!m_line[m_i].move)
	{
		Down(m_line[m_i].b);
	}
	CDialog::OnTimer(nIDEvent);
}
void CTowerMfcDlg::Up(int i)
{
	CRect top;
	CRect rect;
	switch(i)
	{
	case 1:
		GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&top);//得到第一个柱子的矩形区域
		break;
	case 2:
		GetDlgItem(IDC_TOWER_SECOND)->GetWindowRect(&top);//得到第二个柱子的矩形区域
		break;
	case 3:
		GetDlgItem(IDC_TOWER_THREE)->GetWindowRect(&top);//得到第三个柱子的矩形区域
		break;
	}
	ScreenToClient(&top);								//转换坐标
	rect=m_tower[i-1].allrect[m_tower[i-1].allrect.size()-1];//得到顶层的矩形
	InvalidateRect(rect,true);							//刷新矩形区域
	if(top.top<rect.bottom-HEIGTH)						//是不是已经移动到了顶了
	{
		rect.bottom-=HEIGTH;							//移动矩形
		rect.top=rect.top-HEIGTH;
		m_tower[i-1].allrect[m_tower[i-1].allrect.size()-1]=rect;//设置新的矩形
		InvalidateRect(rect,true);
		
	}
	else
	{
		arrivetop=true;	
		m_stopnum++;
		UpdateData(FALSE);//记忆到了顶层
	//	MessageBox("到塔顶了");
	}
}
void CTowerMfcDlg::Down(int i)
{
	CRect rect;
	CRect next;
	CRect down;
	switch(i)
	{
	case 1:
		GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&down);
		break;
	case 2:
		GetDlgItem(IDC_TOWER_SECOND)->GetWindowRect(&down);
		break;
	case 3:
		GetDlgItem(IDC_TOWER_THREE)->GetWindowRect(&down);
		break;
	}
	ScreenToClient(&down);			//转换坐标
	if(m_tower[i-1].allrect.size()>=2)//如果这个他的层数大于两层
	{
		rect=m_tower[i-1].allrect[m_tower[i-1].allrect.size()-1];//得到顶层的坐标
		next=m_tower[i-1].allrect[m_tower[i-1].allrect.size()-2];//得到顶层下层的坐标
	//	InvalidateRect(rect,true);								//刷新区域
		if(rect.top<(next.top-HEIGTH))							//是不是已经对到了底了
		{
			rect.bottom+=HEIGTH;
			rect.top=rect.top+HEIGTH;						//移动矩形
			m_tower[i-1].allrect[m_tower[i-1].allrect.size()-1]=rect;
			InvalidateRect(rect,true);
			
		}
		else
		{
			arrivetop=false;								//当到了底了的时候将一些控制变量
			arrivedown=false;								//重新赋值
			IsPop=false;								
			//	arrivetop=true;
			//	MessageBox("到塔顶了");
			m_line[m_i].move=true;							//
			m_i++;
			///下一个路径
		}
	}
	else
	{
		rect=m_tower[i-1].allrect[m_tower[i-1].allrect.size()-1];//只有一层的时候
	//	ScreenToClient(&rect);
		InvalidateRect(rect,true);
		if(rect.bottom!=189)
		{
			rect.bottom+=HEIGTH;
			rect.top=rect.top+HEIGTH;
			m_tower[i-1].allrect[m_tower[i-1].allrect.size()-1]=rect;
			InvalidateRect(rect,true);
		}
		else
		{
			arrivetop=false;
			arrivedown=false;
			IsPop=false;
			//	arrivetop=true;
			//	MessageBox("到塔顶了");
			m_line[m_i].move=true;
			m_i++;
		}
	}
}

void CTowerMfcDlg::MoveAToB(int A, int B)
{
	CRect finish;
	switch(B)
	{
	case 1:
		GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&finish);
		break;
	case 2:
		GetDlgItem(IDC_TOWER_SECOND)->GetWindowRect(&finish);
		break;
	case 3:
		GetDlgItem(IDC_TOWER_THREE)->GetWindowRect(&finish);
		break;
	}
	ScreenToClient(&finish);
	//进展
	if(arrivetop)								//当上升到了顶的时候,水平移动矩形
	{
		if(!IsPop)								//如果是第一个次
		{
			//得到A的塔顶坐标
			CRect rectA=m_tower[A-1].allrect[m_tower[A-1].allrect.size()-1];//得到
			//A的矩形区域
			m_tower[B-1].allrect.push_back(rectA);//将这个矩形区域给B
			m_tower[A-1].allrect.pop_back();		//在a中去掉这个矩形区域
			InvalidateRect(rectA,true);
			IsPop=true;
		}
		CRect rect=m_tower[B-1].allrect[m_tower[B-1].allrect.size()-1];
		InvalidateRect(rect,true);
		if(rect.CenterPoint().x!=(finish.CenterPoint().x))//判断是不是到了终点
		{
			rect.left=rect.left+20;
			rect.right=rect.right+20;
			m_tower[B-1].allrect[m_tower[B-1].allrect.size()-1]=rect;
		}
		else
		{
			arrivedown=true;
		}
		InvalidateRect(rect,true);
		
	}
}
void CTowerMfcDlg::MoveBToA(int A, int B)
{
	CRect finish;
	switch(B)
	{
	case 1:
		GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&finish);
		break;
	case 2:
		GetDlgItem(IDC_TOWER_SECOND)->GetWindowRect(&finish);
		break;
	case 3:
		GetDlgItem(IDC_TOWER_THREE)->GetWindowRect(&finish);
		break;
	}
	ScreenToClient(&finish);
	//进展
	if(arrivetop)
	{
		if(!IsPop)
		{
			//得到A的塔顶坐标
			CRect rectA=m_tower[A-1].allrect[m_tower[A-1].allrect.size()-1];
			m_tower[B-1].allrect.push_back(rectA);
			m_tower[A-1].allrect.pop_back();
			InvalidateRect(rectA,true);
			IsPop=true;
		}
		CRect rect=m_tower[B-1].allrect[m_tower[B-1].allrect.size()-1];
		InvalidateRect(rect,true);
		if(rect.CenterPoint().x!=(finish.CenterPoint().x))
		{
			rect.left=rect.left-20;
			rect.right=rect.right-20;
			m_tower[B-1].allrect[m_tower[B-1].allrect.size()-1]=rect;
		}
		else
		{
			arrivedown=true;
		}
		InvalidateRect(rect,true);
		
	}
}

void CTowerMfcDlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	SetTimer(1,10,NULL);
}

void CTowerMfcDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
}

⌨️ 快捷键说明

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