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

📄 labyrinthview.cpp

📁 谭浩强C语言书中的一个走迷宫的例子
💻 CPP
字号:
// LabyrinthView.cpp : implementation of the CLabyrinthView class
//

#include "stdafx.h"
#include "Labyrinth.h"

#include "LabyrinthDoc.h"
#include "LabyrinthView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLabyrinthView


unsigned char Maze[8][8]={
2,0,1,0,0,0,0,1,
0,0,1,0,1,0,1,0,
0,0,0,0,1,0,0,0,
0,1,0,1,0,0,1,0,
0,0,0,1,0,1,0,0,
0,1,0,0,0,1,0,0,
0,0,1,1,1,1,1,1,
1,0,0,0,0,0,0,3
};




unsigned char Map[8][8]={
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
};



IMPLEMENT_DYNCREATE(CLabyrinthView, CView)

BEGIN_MESSAGE_MAP(CLabyrinthView, CView)
	//{{AFX_MSG_MAP(CLabyrinthView)
	ON_COMMAND(ID_MAZE_SEARCH, OnMazeSearch)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLabyrinthView construction/destruction

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

	SearchFlag=FALSE;
	m_NodeArr_Pass.RemoveAll();
	m_NodeArr_Path.RemoveAll();
}

CLabyrinthView::~CLabyrinthView()
{
}

BOOL CLabyrinthView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLabyrinthView drawing

void CLabyrinthView::OnDraw(CDC* pDC)
{
	CLabyrinthDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	int colorflg;
	int i,j;
	for(i=0;i<8;i++)
	{
		for(j=0;j<8;j++)
		{
			CRect rect;
			rect.left=40+i*40;
			rect.top=40+j*40;
			rect.right=rect.left+40;
			rect.bottom=rect.top+40;
			colorflg=Maze[j][i];
			this->DrawMyRectCell(pDC,rect,colorflg);

		}
	}

	if(SearchFlag==TRUE)
	{
			CPen pen(PS_SOLID,0,RGB(255,0,255));
			CGdiObject *pold=pDC->SelectObject(&pen);

			int size=this->m_NodeArr_Pass.GetSize();

			pDC->MoveTo((40+20),(40+20));
			if(size>1)
			{
				for(i=1;i<size;i++)
				{
					MyNode node;
					node=this->m_NodeArr_Pass.GetAt(i);
					pDC->LineTo((40+20+node.x*40),(40+20+node.y*40));
				}
			}

				CPen pen1(2,0,RGB(0,0,255));
		
			pDC->SelectObject(&pen1);
			size=this->m_NodeArr_Path.GetSize();

			pDC->MoveTo((40+10),(40+10));
			if(size>1)
			{
				for(i=1;i<size;i++)
				{
					MyNode node;
					node=this->m_NodeArr_Path.GetAt(i);
					pDC->LineTo((40+10+node.x*40),(40+10+node.y*40));
				}
			}


			pDC->SelectObject(pold);
		
		
	}
	////

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CLabyrinthView printing

BOOL CLabyrinthView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CLabyrinthView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CLabyrinthView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CLabyrinthView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CLabyrinthView message handlers

void CLabyrinthView::DrawMyRectCell(CDC *pDC, CRect rect, int corlor)
{
	CPen pen(PS_SOLID,0,RGB(255,0,0));
	CGdiObject *pold=pDC->SelectObject(&pen);


	COLORREF pram;
	
	if(corlor==2)
		pram=RGB(255,0,0);
	else
		if(corlor==1)
				pram=RGB(0,255,255);
		else
			if(corlor==0)
				pram=RGB(255,255,255);
			else
				if(corlor==3)
					pram=RGB(0,255,0);


				


	CBrush pbrush(HS_DIAGCROSS   ,pram);

	pDC->SelectObject(&pbrush);

	pDC->Rectangle(rect);
/*
	pDC->MoveTo(rect.left,rect.top);
	pDC->LineTo(rect.right,rect.top);
	pDC->LineTo(rect.right,rect.bottom);
	pDC->LineTo(rect.left,rect.bottom);
	pDC->LineTo(rect.left,rect.top);
*/
	pDC->SelectObject(pold);

}

void CLabyrinthView::OnMazeSearch() 
{
	// TODO: Add your command handler code here

	this->m_NodeArr_Pass.RemoveAll();
	this->m_NodeArr_Path.RemoveAll();

	for(int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++)
			Map[i][j]=0;

	}
	SearchFlag=TRUE;
	CStdioFile file;
	file.Open("path.txt",CFile::modeCreate);
	file.Close();

	this->SearchMaze(0,0,0);

	this->Invalidate();
	
	
}

int CLabyrinthView::GetNextNode(int x, int y, int orient,int* xx,int* yy)
{
	switch(orient)
	{
	case 2:
		x++;
		break;
	case 3:
		y++;
		break;
	case 0:
		x--;		
		break;
	case 1:
		y--;
		break;
	}
	*xx=x;
	*yy=y;
	if(x>7||x<0||y>7||y<0)
		return -1;   //////////////越界
	if(Maze[y][x]==2)
		return 2;   //入口
	if(Maze[y][x]==3)
		return 3;   //出口
	if(Map[x][y]==1)
		return 4;////已标记过

	if(Maze[y][x]==0)
		return 0;   //可行走
	else 
		//Maze[y][x]==1)
		return 1;/////墙 不通
}

void CLabyrinthView::SearchMaze(int x, int y, int orient)
{
	if(Maze[y][x]==3)
		return;///////出口


	CStdioFile file;
	file.Open("path.txt",CFile::modeWrite);

	file.SeekToEnd();

	CString txt;
	txt.Format("\n");

	file.WriteString(txt);



	int ret;
	int xx,yy;
	MyNode nextnode;

	Map[x][y]=1;
if(orient==4)
AfxMessageBox("dfsdf");
	for(int i=orient;i<4;i++)
	{
		ret=GetNextNode(x,y,i,&xx,&yy);


		if(ret==3)
			return;

			nextnode.o=0;
			nextnode.x=xx;
			nextnode.y=yy;
		if(ret==0||ret==4)
		{
			MyNode node;
			
			node.o=i;
			node.x=x;
			node.y=y;

			
			
			this->m_NodeArr_Path.Add(node);

			txt.Format("x=%d  y=%d o=%d\n",x,y,i);
			file.WriteString(txt);

			if(ret==0)
			{
				this->m_NodeArr_Pass.Add(node);
				break;
			}
		}
		if(ret==2)
		{
			int size=this->m_NodeArr_Pass.GetSize();
			if(size==0)
				return;
		}

	//	else
		//	this->m_NodeArr_Path.Add(nextnode);


	}
	if(i==4)
	{
		int size=this->m_NodeArr_Pass.GetSize();
		if(size>0)
		{
			nextnode=this->m_NodeArr_Pass.GetAt(size-1);
		//	nextnode.o++;
			m_NodeArr_Pass.RemoveAt(size-1);
		}
	}

	file.Close();

	this->SearchMaze(nextnode.x,nextnode.y,nextnode.o);

}

void CLabyrinthView::GetPath(int x, int y, int orient)
{
	if(Maze[x][y]==3)
		return;///////出口
	
	int ret;
	int xx,yy;
	MyNode nextnode;
	Map[x][y]=1;
	for(int i=orient;i<4;i++)
	{
		ret=GetNextNode(x,y,i,&xx,&yy);
		if(ret==3)
			return;
			nextnode.o=0;
			nextnode.x=xx;
			nextnode.y=yy;
		if(ret==0||ret==4)
		{
			MyNode node;
			
			node.o=i;
			node.x=x;
			node.y=y;			
			this->m_NodeArr_Path.Add(node);
			if(ret==0)
			{
				this->m_NodeArr_Pass.Add(node);
				break;
			}
		}
	}
	if(i==4)
	{
		int size=this->m_NodeArr_Pass.GetSize();
		if(size>0)
		{
			nextnode=this->m_NodeArr_Pass.GetAt(size-1);
			nextnode.o++;
			m_NodeArr_Pass.RemoveAt(size-1);
		}
	}
	this->SearchMaze(nextnode.x,nextnode.y,nextnode.o);
}

int CLabyrinthView::GetNextNode1(int x, int y, int orient,int* xx,int* yy)
{
	switch(orient)
	{
	case 0:
		x++;
		break;
	case 1:
		y++;
		break;
	case 2:
		x--;		
		break;
	case 3:
		y--;
		break;
	}
	*xx=x;
	*yy=y;
	if(x>7||x<0||y>7||y<0)
		return -1;   //////////////越界
	//if(Maze[y][x]==2)
	//	return 2;   //出口
	if(Maze[y][x]==3)
		return 3;   //出口
	if(Map[x][y]==1)
		return 4;////已标记过

	if(Maze[y][x]==0)
		return 0;   //可行走
	else 
		//Maze[y][x]==1)
		return 1;/////墙 不通
}

⌨️ 快捷键说明

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