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

📄 mainfrm.cpp

📁 我写的迷宫与搜路算法,自动生成随即迷宫,然后自动从起点到终点运动
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "WMaze.h"
#include"headfile.h"
#include "MainFrm.h"
#include <list>
#include<iterator>
using namespace std;

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_START, OnStart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
    cs.cx = 820;
    cs.cy = 800;
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers



void CMainFrame::OnStart() 
{
	// TODO: Add your command handler code here
//	MessageBox("fuck");
	CClientDC dc(this);
	CDC mdc;
	mdc.CreateCompatibleDC(&dc);
//	dc.FillSolidRect(347,FOOT+1,LSIZE-2,LSIZE-2,RGB(255,0,0));
//	for(int i=0;i<100;i++){
	int Top=20;//(int)(((float)rand() / (float)RAND_MAX) * 2 )+1;
	int Bottom=20;//(int)(((float)rand() / (float)RAND_MAX) * 2) +1;
	Find_Road2(Top,Bottom);
//	}
}


void CMainFrame::Find_Road1(int Top,int Bottom){
	CClientDC dc(this);
	CDC mdc;
	mdc.CreateCompatibleDC(&dc);
	int x=HEAD+(LSIZE-1)*Bottom+3;
	int y=HEAD+(LSIZE)*40+1;
	dc.FillSolidRect(x,y,13,13,RGB(0,255,0));
	for(;y-LSIZE+1>HEAD-LSIZE;){
		switch((int)(((float)rand() / (float)RAND_MAX) * 4)){
			case 0:
				if(dc.GetPixel(x+5,y+LSIZE-2)!=0)break;
				dc.FillSolidRect(x,y+LSIZE-1,13,13,RGB(255,0,0));
				dc.FillSolidRect(x,y,13,13,RGB(0,255,0));
				y=y+LSIZE-1;
				break;
			case 1:
				if(dc.GetPixel(x+LSIZE-2,y+5)!=0)break;
				dc.FillSolidRect(x+LSIZE-1,y,13,13,RGB(255,0,0));
				dc.FillSolidRect(x,y,13,13,RGB(0,255,0));
				x=x+LSIZE-1;
				break;
			case 2:
				if(dc.GetPixel(x-1,y+5)!=0)break;
				dc.FillSolidRect(x-LSIZE+1,y,13,13,RGB(255,0,0));
				dc.FillSolidRect(x,y,13,13,RGB(0,255,0));
				x=x-LSIZE+1;
				break;
			default:
				if(dc.GetPixel(x+5,y-1)!=0)break;
				dc.FillSolidRect(x,y-LSIZE+1,13,13,RGB(255,0,0));
				dc.FillSolidRect(x,y,13,13,RGB(0,255,0));
				y=y-LSIZE+1;
				break;
		}
	}
}


void CMainFrame::Find_Road2(int Top,int Bottom){
	CClientDC dc(this);
	CDC mdc;
	mdc.CreateCompatibleDC(&dc);
	int x=HEAD+(LSIZE-1)*Bottom+3;
	int y=HEAD+(LSIZE)*40+1;
	list<int *>::iterator i;
	list<int *>::iterator j;
	list< int * > MyList;
	MyList.clear();

	int *first=new int[6];
	first[0]=x;
	first[1]=y;
	dc.GetPixel(x+5,y-1)==0?first[2]=0:first[2]=1;
	dc.GetPixel(x+5,y+LSIZE-2)==0?first[3]=0:first[3]=1;
	dc.GetPixel(x-1,y+5)==0?first[4]=0:first[4]=1;
	dc.GetPixel(x+LSIZE-2,y+5)==0?first[5]=0:first[5]=1;
	MyList.push_back(first);
	dc.FillSolidRect(x,y,13,13,RGB(0,255,0));

	for(;y-LSIZE+1>HEAD-LSIZE;){
		i=MyList.end();
		i--;
		int *temp=*i;
		x=temp[0];
		y=temp[1];
		for (j=MyList.begin();j!=MyList.end(); ++j){
			int *u=*j;
			if(u[0]==x&&u[1]==y-LSIZE+1&&temp[2]==0){
				temp[2]=1;
				u[3]=1;
			}
			if(u[0]==x&&u[1]==y+LSIZE-1&&temp[3]==0){
				temp[3]=1;
				u[2]=1;
			}
			if(u[0]==x-LSIZE+1&&u[1]==y&&temp[4]==0){
				temp[4]=1;
					u[5]=1;
			}
			if(u[0]==x+LSIZE-1&&u[1]==y&&temp[5]==0){
				temp[5]=1;
					u[4]=1;
			}
		}


		if(temp[2]!=0&&temp[3]!=0&&temp[4]!=0&&temp[5]!=0){
			MyList.remove(temp);
			if(MyList.size()==0){
				MessageBox("无路可走");	
				return;
			}	
		}
		else if(temp[2]==0){
//			dc.FillSolidRect(x,y,13,13,RGB(255,0,0));
			temp[2]=1;
			y=y-LSIZE+1;
			int *listeam=new int[6];
			listeam[0]=x;
			listeam[1]=y;
			dc.GetPixel(x+5,y-1)==0?listeam[2]=0:listeam[2]=1;
			listeam[3]=1;
			dc.GetPixel(x-1,y+5)==0?listeam[4]=0:listeam[4]=1;
			dc.GetPixel(x+LSIZE-2,y+5)==0?listeam[5]=0:listeam[5]=1;
			MyList.push_back(listeam);
//			dc.FillSolidRect(x,y,13,13,RGB(0,255,0));
		}


		 else if(temp[5]==0){
//			dc.FillSolidRect(x,y,13,13,RGB(255,0,0));
			temp[5]=1;
			x=x+LSIZE-1;
			int *listeam=new int[6];
			listeam[0]=x;
			listeam[1]=y;
			dc.GetPixel(x+5,y-1)==0?listeam[2]=0:listeam[2]=1;
			dc.GetPixel(x+5,y+LSIZE-2)==0?listeam[3]=0:listeam[3]=1;
			listeam[4]=1;
			dc.GetPixel(x+LSIZE-2,y+5)==0?listeam[5]=0:listeam[5]=1;
			MyList.push_back(listeam);
//			dc.FillSolidRect(x,y,13,13,RGB(0,255,0));
		}	
		 else if(temp[3]==0){
//			dc.FillSolidRect(x,y,13,13,RGB(255,0,0));
			temp[3]=1;
			y=y+LSIZE-1;
			int *listeam=new int[6];
			listeam[0]=x;
			listeam[1]=y;
			listeam[2]=1;
			dc.GetPixel(x+5,y+LSIZE-2)==0?listeam[3]=0:listeam[3]=1;
			dc.GetPixel(x-1,y+5)==0?listeam[4]=0:listeam[4]=1;
			dc.GetPixel(x+LSIZE-2,y+5)==0?listeam[5]=0:listeam[5]=1;
			MyList.push_back(listeam);

//			dc.FillSolidRect(x,y,13,13,RGB(0,255,0));			
		}
		 		else if(temp[4]==0){	
//			dc.FillSolidRect(x,y,13,13,RGB(255,0,0));
			temp[4]=1;
			x=x-LSIZE+1;
			int *listeam=new int[6];
			listeam[0]=x;
			listeam[1]=y;
			dc.GetPixel(x+5,y-1)==0?listeam[2]=0:listeam[2]=1;
			dc.GetPixel(x+5,y+LSIZE-2)==0?listeam[3]=0:listeam[3]=1;
			dc.GetPixel(x-1,y+5)==0?listeam[4]=0:listeam[4]=1;
			listeam[5]=1;
			MyList.push_back(listeam);
//			dc.FillSolidRect(x,y,13,13,RGB(0,255,0));
		}
	}

	for (j=MyList.begin();j!=MyList.end(); ++j){
		int *u=*j;			
		dc.FillSolidRect(u[0],u[1],13,13,RGB(0,255,0));
	}
}

⌨️ 快捷键说明

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