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

📄 seat.cpp

📁 汉诺(hanoi)问题采用递归算法解决
💻 CPP
字号:
// Seat.cpp: implementation of the CSeat class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Hanoi.h"
#include "Seat.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSeat::CSeat(CString name, int x, int y,int trayNumber, int halfWidth)
{
	m_strName = name;
	m_ptPosition.x = x;
	m_ptPosition.y = y;
	m_nTrayNumber = trayNumber < MAX ?  trayNumber : MAX;
	m_nHalfWidth = halfWidth;
	m_nHight = 2*m_nHalfWidth;
	for(int i=0; i< m_nTrayNumber; i++)
	{
		m_pTray[i] = new CTray(m_nHalfWidth -10 -5*i);
	}
}


CSeat::~CSeat()
{

}

void  CSeat::Draw(CDC *pDC)
{
	pDC->MoveTo(m_ptPosition.x - m_nHalfWidth, m_ptPosition.y);
	pDC->LineTo(m_ptPosition.x + m_nHalfWidth, m_ptPosition.y);
	pDC->MoveTo(m_ptPosition.x - m_nHalfWidth, m_ptPosition.y+1);
	pDC->LineTo(m_ptPosition.x + m_nHalfWidth, m_ptPosition.y+1);
	pDC->MoveTo(m_ptPosition.x - m_nHalfWidth, m_ptPosition.y+2);
	pDC->LineTo(m_ptPosition.x + m_nHalfWidth, m_ptPosition.y+2);

	pDC->MoveTo(m_ptPosition.x, m_ptPosition.y);
	pDC->LineTo(m_ptPosition.x, m_ptPosition.y - m_nHight);

	pDC->TextOut(m_ptPosition.x- 5, m_ptPosition.y +10, m_strName);
}

void  CSeat::DrawTray(CDC *pDC)
{
	pDC->SetROP2( R2_NOT );
	for(int i=0; i<m_nTrayNumber; i++)
	{
		m_pTray[i]->Draw(pDC, CPoint(m_ptPosition.x, m_ptPosition.y - 10 - 10 * i) );
	}
}

bool  CSeat::Valid( CSeat * to)
{
	if(m_nTrayNumber == 0)
		return false;
	if(to->m_nTrayNumber == 0)
		return true;
	CTray *pFrom = m_pTray[m_nTrayNumber-1];
	CTray *pTo = to->m_pTray[to->m_nTrayNumber-1];

	if(pFrom->m_nHalfWidth  < pTo->m_nHalfWidth )
		return true;
	else
		return false;
}

void  CSeat::MoveTo(CDC *pDC, CSeat * to)
{	
	if(!Valid( to))
	{
		AfxMessageBox("移动非法!请检查。");
		return;
	}
	CTray *pTray = m_pTray[m_nTrayNumber-1];
	MoveVirtical  (pDC, pTray, m_ptPosition.x, m_ptPosition.y - m_nTrayNumber * 10, m_ptPosition.y -m_nHight);
	MoveHorizental(pDC, pTray, m_ptPosition.y - m_nHight, m_ptPosition.x, to->m_ptPosition.x);
	m_nTrayNumber --;
	to->m_nTrayNumber ++;
	to->m_pTray[to->m_nTrayNumber - 1] = pTray;
	MoveVirtical  (pDC, pTray,to->m_ptPosition.x, to->m_ptPosition.y - to->m_nHight ,to->m_ptPosition.y - to->m_nTrayNumber * 10);
}

void  CSeat::MoveHorizental(CDC *pDC, CTray *moveTray, int y, int x1, int x2)
{
	int i;
	pDC->SetROP2( R2_NOT );
	pDC->MoveTo(x1 - moveTray->m_nHalfWidth, y);
	pDC->LineTo(x1 + moveTray->m_nHalfWidth, y);
	if(x1 < x2)
	{
		for(i=x1; i<x2 ;i+=5)
		{
			pDC->MoveTo(i - moveTray->m_nHalfWidth, y);
			pDC->LineTo(i + moveTray->m_nHalfWidth, y);
			Sleep(DELAY);
			pDC->MoveTo(i - moveTray->m_nHalfWidth, y);
			pDC->LineTo(i + moveTray->m_nHalfWidth, y);
		}
	}
	else
	{
		for(i=x1; i>x2 ;i-=5)
		{
			pDC->MoveTo(i - moveTray->m_nHalfWidth, y);
			pDC->LineTo(i + moveTray->m_nHalfWidth, y);
			Sleep(DELAY);
			pDC->MoveTo(i - moveTray->m_nHalfWidth, y);
			pDC->LineTo(i + moveTray->m_nHalfWidth, y);
		}
	}
	pDC->MoveTo(x2 - moveTray->m_nHalfWidth, y);
	pDC->LineTo(x2 + moveTray->m_nHalfWidth, y);
}

void  CSeat::MoveVirtical(CDC *pDC, CTray *moveTray, int x, int y1, int y2)
{
	int i;
	pDC->SetROP2( R2_NOT );
	pDC->MoveTo(x - moveTray->m_nHalfWidth, y1);
	pDC->LineTo(x + moveTray->m_nHalfWidth, y1);
	if(y1 < y2)
	{
		for(i=y1+5; i<y2 ;i+=5)
		{
			pDC->MoveTo(x - moveTray->m_nHalfWidth, i);
			pDC->LineTo(x + moveTray->m_nHalfWidth, i);
			Sleep(DELAY);
			pDC->MoveTo(x - moveTray->m_nHalfWidth, i);
			pDC->LineTo(x + moveTray->m_nHalfWidth, i);

		}
	}
	else
	{
		for(i=y1-5; i>y2 ;i-=5)
		{
			pDC->MoveTo(x - moveTray->m_nHalfWidth, i);
			pDC->LineTo(x + moveTray->m_nHalfWidth, i);
			Sleep(DELAY);
			pDC->MoveTo(x - moveTray->m_nHalfWidth, i);
			pDC->LineTo(x + moveTray->m_nHalfWidth, i);
		}
	}
	pDC->MoveTo(x - moveTray->m_nHalfWidth, y2);
	pDC->LineTo(x + moveTray->m_nHalfWidth, y2);
}

⌨️ 快捷键说明

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