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

📄 eightview.cpp

📁 八数码问题的求解八数码问题的求解八数码问题的求解八数码问题的求解
💻 CPP
字号:
// EightView.cpp : implementation of the CEightView class
//

#include "stdafx.h"
#include "Eight.h"

#include "EightDoc.h"
#include "EightView.h"


#include "NineBox.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEightView

IMPLEMENT_DYNCREATE(CEightView, CView)

BEGIN_MESSAGE_MAP(CEightView, CView)
	//{{AFX_MSG_MAP(CEightView)
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEightView construction/destruction

CEightView::CEightView()
{
	// TODO: add construction code here

}

CEightView::~CEightView()
{
}

BOOL CEightView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEightView drawing
void CEightView::DrawBasic(CDC* pDC)
{
	CEightDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	NineBox* obj=pDoc->GetBoxObj();
	CString str="×";
	CRect rc;
	
	int x0=90;
	int y0=40;
	int x1=150;
	int y1=150;
	
	pDC->SetBkMode(TRANSPARENT);
	pDC->TextOut(10,370,"使用W,S,A,D或者箭头实现上下左右移动空格.");
	pDC->SetTextColor(RGB(255,0,0));
	for (int k=0;k<9;k++)
	{
		int i,j;
		obj->GetXYByValue(k,i,j);
		if (k!=0) 
		{
			str.Format("%d",obj->GetBoxValue(i,j));
		}
		
		rc.SetRect(x0+j*100,y0+i*100,x0+j*100+100,y0+i*100+100);
		pDC->Rectangle(rc);
		pDC->TextOut(x0+j*100+50,y0+i*100+50,str);
		if (k==0) 
		{
			pDC->SetTextColor(RGB(0,0,255));
		}
	}
}
void CEightView::OnDraw(CDC* pDC)
{
	CEightDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	NineBox* obj=pDoc->GetBoxObj();
	DrawBasic(pDC);
	
	int x0=90;
	int y0=40;
	int x1=100;
	int y1=100;

	if (true)//some thing
	{
		CPen pen;
		CPen *oldpen;
		
		//------------------
		int i,j,t;
		int m,n,x,y;
		obj->GetXYByValue(0,i,j);
		t=obj->GetLastMoveType();
		if(t!=-1)
		{
			pen.CreatePen(1,2,RGB(255,25,180));
			x=m=x0+j*100+50;
			y=n=y0+i*100+50;
			
			
			switch(t)
			{
			case 1:
				y+=20;
				n+=80;
				break;
			case 2:
				y-=20;
				n-=80;
				break;
			case 3:
				x+=20;
				m+=80;
				break;
			case 4:
				x-=20;
				m-=80;
				break;
			default:;
			}
			
			oldpen=pDC->SelectObject(&pen);
			
			pDC->MoveTo(x,y);
			pDC->LineTo(m,n);

			for (int i=0;i<2;i++) 
			{
			pDC->MoveTo(x,y);
			m=x;n=y;
			HelpDraw(t,m,n);
			pDC->LineTo(m,n);
			}

			pDC->SelectObject(oldpen);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CEightView diagnostics

#ifdef _DEBUG
void CEightView::AssertValid() const
{
	CView::AssertValid();
}

void CEightView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CEightDoc* CEightView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEightDoc)));
	return (CEightDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEightView message handlers

void CEightView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CEightDoc* doc=this->GetDocument();
	NineBox* obj=doc->GetBoxObj();
	switch(nChar)
	{
	case 'W':
	case 38:
		obj->MoveUp();
		break;
	case 'A':
	case 37:
		obj->MoveLeft();
		break;
	case 'S':
	case 40:
		obj->MoveDown();
		break;
	case 'D':
	case 39:
		obj->MoveRight();
		break;
	default:goto END;
	}
	doc->UpdateAllViews(0);
END:CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

⌨️ 快捷键说明

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