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

📄 searchpathview.cpp

📁 寻径算法演示平台~~提供给计算机博弈爱好者参考学习
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
		if(m_ifLBTUp == 0)//如果在地图制作模式里面左键没有松开,而且鼠标在移动
		{
			int x,y;			
			x = point.x/5;
			y = point.y/5;
			map[y][x] = m_ModeOfMakeMap;
			map[y-1][x] = m_ModeOfMakeMap;
			map[y][x-1] = m_ModeOfMakeMap;
			map[y-1][x-1] = m_ModeOfMakeMap;

			Invalidate();//使窗口重绘
		}
	}
	
	CView::OnMouseMove(nFlags, point);
}

void CSearchPathView::OnSaveMap() 
{
	// TODO: Add your command handler code here
	
	if(m_numSaveMap >= 5)
	{
		AfxMessageBox("在一个文本文件中最多可以保存5幅地图,请把同一目录下的SaveMap文件删除或改名,以保存更多的地图!");
		return;
	}

	m_numSaveMap++;//先累计用户保存的地图数目
	ofstream outs;
	outs.open("SaveMap.txt",ios::app);
	outs<<"///map"<<m_numSaveMap<<"///"<<endl;
	for(int i = 0;i < Height;i++)
	{
		for(int j = 0;j < Width;j++)
		{
			if(map[i][j] == 0)
			{
				outs<<" "<<map[i][j]<<",";
			}
			else
			{
				outs<<map[i][j]<<",";
			}
		}
		outs<<endl;
	}
	outs.close();
	
	CMenu *mainmenu;
	mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单
	//(mainmenu->GetSubMenu (6))->AppendMenu (MF_SEPARATOR);//添加分隔符
	CString name;
	CString temp(m_numSaveMap + 48);
	name = " save_map" + temp;
	(mainmenu->GetSubMenu (6))->AppendMenu(MF_STRING,IDM_SaveMap1 + m_numSaveMap - 1,_T(name)); //添加新的菜单项
	DrawMenuBar(); //重画菜单
	
	
	AfxMessageBox("地图已经保存!");
	
}

void CSearchPathView::OnSaveMap1()
{
	CFile IndexFile;
	
	if(IndexFile.Open("SaveMap.txt",CFile::modeRead))
	{}
	else
	{
		AfxMessageBox("cant open result.txt!");
		return;
	}

	IndexFile.SeekToBegin();
	
	char character[1];
	character[1] = NULL;
	IndexFile.SeekToBegin();
	
	while (IndexFile.Read(character,1))
	{
		if(character[0] == '/')
		{
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			if(character[0] == '1')
			{
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				for(int i = 0;i < Height;i++)
				{
					for(int j = 0;j < Width;j++)
					{
						IndexFile.Read(character,1);
						IndexFile.Read(character,1);
						map[i][j] = -((int)character[0] - 48);
						IndexFile.Read(character,1);
					}
					IndexFile.Read(character,1);
					IndexFile.Read(character,1);
				}
				break;
			}
		}
	}	

	m_SearchSort = 0;//which alg are you use.
	m_ifOpen = 0;//等于一的时候就可以输入起点和终点
	m_ifDouble = 0;//表示没有双击
	m_ifMakemap = 0;//表示不能编辑地图
	m_showMode = 0;//不显示搜索过的节点

	IndexFile.Close();
	Invalidate();
}

void CSearchPathView::OnSaveMap2()
{
	CFile IndexFile;
	
	if(IndexFile.Open("SaveMap.txt",CFile::modeRead))
	{}
	else
	{
		AfxMessageBox("cant open result.txt!");
		return;
	}

	IndexFile.SeekToBegin();
	
	char character[1];
	character[1] = NULL;
	IndexFile.SeekToBegin();
	
	while (IndexFile.Read(character,1))
	{
		if(character[0] == '/')
		{
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			if(character[0] == '2')
			{
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				for(int i = 0;i < Height;i++)
				{
					for(int j = 0;j < Width;j++)
					{
						IndexFile.Read(character,1);
						IndexFile.Read(character,1);
						map[i][j] = -((int)character[0] - 48);
						IndexFile.Read(character,1);
					}
					IndexFile.Read(character,1);
					IndexFile.Read(character,1);
				}
				break;
			}
		}
	}	

	m_SearchSort = 0;//which alg are you use.
	m_ifOpen = 0;//等于一的时候就可以输入起点和终点
	m_ifDouble = 0;//表示没有双击
	m_ifMakemap = 0;//表示不能编辑地图
	m_showMode = 0;//不显示搜索过的节点

	IndexFile.Close();
	Invalidate();
}

void CSearchPathView::OnSaveMap3()
{
	CFile IndexFile;
	
	if(IndexFile.Open("SaveMap.txt",CFile::modeRead))
	{}
	else
	{
		AfxMessageBox("cant open result.txt!");
		return;
	}

	IndexFile.SeekToBegin();
	
	char character[1];
	character[1] = NULL;
	IndexFile.SeekToBegin();
	
	while (IndexFile.Read(character,1))
	{
		if(character[0] == '/')
		{
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			if(character[0] == '3')
			{
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				for(int i = 0;i < Height;i++)
				{
					for(int j = 0;j < Width;j++)
					{
						IndexFile.Read(character,1);
						IndexFile.Read(character,1);
						map[i][j] = -((int)character[0] - 48);
						IndexFile.Read(character,1);
					}
					IndexFile.Read(character,1);
					IndexFile.Read(character,1);
				}	
				break;
			}
		}
	}	

	m_SearchSort = 0;//which alg are you use.
	m_ifOpen = 0;//等于一的时候就可以输入起点和终点
	m_ifDouble = 0;//表示没有双击
	m_ifMakemap = 0;//表示不能编辑地图
	m_showMode = 0;//不显示搜索过的节点

	IndexFile.Close();
	Invalidate();
}

void CSearchPathView::OnSaveMap4()
{
	CFile IndexFile;
	
	if(IndexFile.Open("SaveMap.txt",CFile::modeRead))
	{}
	else
	{
		AfxMessageBox("cant open result.txt!");
		return;
	}

	IndexFile.SeekToBegin();
	
	char character[1];
	character[1] = NULL;
	IndexFile.SeekToBegin();
	
	while (IndexFile.Read(character,1))
	{
		if(character[0] == '/')
		{
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			if(character[0] == '4')
			{
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				for(int i = 0;i < Height;i++)
				{
					for(int j = 0;j < Width;j++)
					{
						IndexFile.Read(character,1);
						IndexFile.Read(character,1);
						map[i][j] = -((int)character[0] - 48);
						IndexFile.Read(character,1);
					}
					IndexFile.Read(character,1);
					IndexFile.Read(character,1);
				}
				break;
			}
		}
	}	

	m_SearchSort = 0;//which alg are you use.
	m_ifOpen = 0;//等于一的时候就可以输入起点和终点
	m_ifDouble = 0;//表示没有双击
	m_ifMakemap = 0;//表示不能编辑地图
	m_showMode = 0;//不显示搜索过的节点

	IndexFile.Close();
	Invalidate();
}

void CSearchPathView::OnSaveMap5()
{
	CFile IndexFile;
	
	if(IndexFile.Open("SaveMap.txt",CFile::modeRead))
	{}
	else
	{
		AfxMessageBox("cant open result.txt!");
		return;
	}

	IndexFile.SeekToBegin();
	
	char character[1];
	character[1] = NULL;
	IndexFile.SeekToBegin();
	
	while (IndexFile.Read(character,1))
	{
		if(character[0] == '/')
		{
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			if(character[0] == '5')
			{
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				for(int i = 0;i < Height;i++)
				{
					for(int j = 0;j < Width;j++)
					{
						IndexFile.Read(character,1);
						IndexFile.Read(character,1);
						map[i][j] = -((int)character[0] - 48);
						IndexFile.Read(character,1);
					}
					IndexFile.Read(character,1);
					IndexFile.Read(character,1);
				}	
				break;
			}
		}
	}	

	m_SearchSort = 0;//which alg are you use.
	m_ifOpen = 0;//等于一的时候就可以输入起点和终点
	m_ifDouble = 0;//表示没有双击
	m_ifMakemap = 0;//表示不能编辑地图
	m_showMode = 0;//不显示搜索过的节点

	IndexFile.Close();
	Invalidate();
}







void CSearchPathView::OnShowShade() 
{
	// TODO: Add your command handler code here
	m_showMode = 2;
	Invalidate();//使窗口重绘
}

void CSearchPathView::OnShowNode() 
{
	// TODO: Add your command handler code here
	m_showMode = 1;
	Invalidate();//使窗口重绘
}	

void CSearchPathView::OnShowPath() 
{
	// TODO: Add your command handler code here
	m_showMode = 0;
	Invalidate();//使窗口重绘
}

void CSearchPathView::OnReadMap() 
{
	// TODO: Add your command handler code here
	CFile IndexFile;
	/*
	if(FoundData.Open("d:\\result.txt", CFile::modeCreate | CFile::modeWrite))
	{}
	else{
		AfxMessageBox("cant open result.txt!");
		return;
	}
	*/
	
	if(IndexFile.Open("result.txt",CFile::modeRead))
	{}
	else
	{
		AfxMessageBox("cant open d:\\result.txt!");
		return;
	}

	IndexFile.SeekToBegin();
	
	char character[1];
	character[1] = NULL;
	IndexFile.SeekToBegin();
	/*
	while (IndexFile.Read(character,1))
	{
		character[0];
		if(character[0] == '$')
		{
			AfxMessageBox(character);
		}
	}
	*/

	for(int i = 0;i < Height;i++)
	{
		for(int j = 0;j < Width;j++)
		{
			IndexFile.Read(character,1);
			IndexFile.Read(character,1);
			map[i][j] = -((int)character[0] - 48);
			IndexFile.Read(character,1);
		}
		IndexFile.Read(character,1);
		IndexFile.Read(character,1);
	}
	

	m_SearchSort = 0;//which alg are you use.
	m_ifOpen = 0;//等于一的时候就可以输入起点和终点
	m_ifDouble = 0;//表示没有双击
	m_ifMakemap = 0;//表示不能编辑地图
	m_showMode = 0;//不显示搜索过的节点

	IndexFile.Close();
	Invalidate();
}


void CSearchPathView::OnInitialUpdate()
{
	CFile IndexFile;
		
		if(IndexFile.Open("SaveMap.txt",CFile::modeRead))
		{}
		else
		{
			return;
		}
		
		IndexFile.SeekToBegin();
		
		char character[1];
		character[1] = NULL;
		IndexFile.SeekToBegin();
		
		while (IndexFile.Read(character,1))
		{
			if(character[0] == '/')
			{
				m_numSaveMap++;
				CMenu *mainmenu;
				mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单
				CString name;
				CString temp(m_numSaveMap + 48);
				name = " save_map" + temp;
				(mainmenu->GetSubMenu (7))->AppendMenu(MF_STRING,IDM_SaveMap1 + m_numSaveMap - 1,_T(name)); //添加新的菜单项
				DrawMenuBar(); //重画菜单
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
				IndexFile.Read(character,1);
			}
		}	
	IndexFile.Close();
}

void CSearchPathView::OnDemoWave()
{
	// TODO: Add your command handler code here
	
	m_ifOpen=0;
	m_ifMakemap = 0;
	m_SearchSort = 3;
	m_showMode = 3;
	Wave = new CWave;
	Wave->prepareForStepByStep();

	CDC *pDC = GetDC();
	int temp;
	while(1)
	{
		temp = Wave->searchThePathStepByStep();
		OnDraw(pDC);
		if(temp == 1)
		{
			MessageBox("路径已经找到!");
			break;
		}
		else if(temp == 0)
		{
			MessageBox("两点之间没有可行的路径!");
			break;
		}
	}
	m_SearchSort = 0;	
}

void CSearchPathView::OnDemoAStar() 
{
	// TODO: Add your command handler code here
	m_ifOpen=0;
	m_ifMakemap = 0;
	m_SearchSort = 1;
	m_showMode = 3;
	AStar = new CAStar;
	AStar->prepareForStepByStep();

	CDC *pDC = GetDC();
	int temp;
	while(1)
	{
		temp = AStar->searchThePathStepByStep();
		OnDraw(pDC);
		if(temp == 1)
		{
			MessageBox("路径已经找到!");
			break;
		}
		else if(temp == 0)
		{
			MessageBox("两点之间没有可行的路径!");
			break;
		}
	}
	m_SearchSort = 0;
}

void CSearchPathView::OnDemoBothWave() 
{
	// TODO: Add your command handler code here
	m_ifOpen=0;
	m_ifMakemap = 0;
	m_SearchSort = 5;
	m_showMode = 3;
	BothWave = new CBothWave;
	BothWave->prepareForStepByStep();

	CDC *pDC = GetDC();
	int temp;
	while(1)
	{
		temp = BothWave->searchThePathStepByStep();
		OnDraw(pDC);
		if(temp == 1)
		{
			MessageBox("路径已经找到!");
			break;
		}
		else if(temp == 0)
		{
			MessageBox("两点之间没有可行的路径!");
			break;
		}
	}
	m_SearchSort = 0;
}

⌨️ 快捷键说明

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