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

📄 qiyuanview.cpp

📁 利用人工智能的经典算法实现迷宫游戏;里面的A星(a*)算法可以很方便的移植到应用程序中
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	   Astep++;
//递归算法,回到第二步判断open表是否为空
	   Asearch(a,s_n,s_g);
	   }
	   return NULL;
  }
}
// 程序结束
void CQiyuanView::OnAboutDialog() 
{
	// TODO: Add your command handler code here
	
	HWND hwnd = ::AfxGetMainWnd()->GetSafeHwnd();

	ShellAbout(hwnd,"迷宫程序  作者:刘琼 ||","E-mail:liuqiong@sina.com.cn",NULL);	
//return TRUE;
	
}

void CQiyuanView::OnUpdateAbout(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
//	HWND hwnd = ::AfxGetMainWnd()->GetSafeHwnd();

//	ShellAbout(hwnd,"迷宫程序  作者:**** ||","E-mail:***·***.com.cn",NULL);	
//return TRUE;
	
}
//时间处理函数,到时间则刷新显示
void CQiyuanView::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default
	if(FoundWayFlag)//若存在路径,则走之
	{
		if(GameStartFlag)
		{
			if(ViewMaze.CarPos!=ViewMaze.GoalPos)//如果不是终点小车位置加一
			{
				CarStep++;
				ViewMaze.CarPos=ViewMaze.Road[CarStep];
				CRect windowSonRect(MapWidth*ViewMaze.CarPos.y,MapHeight*ViewMaze.CarPos.x,MapWidth*ViewMaze.CarPos.y+MapWidth,MapHeight*ViewMaze.CarPos.x+MapHeight);
				InvalidateRect(&windowSonRect);	

				CRect windowSonNextRect(MapWidth*ViewMaze.Road[CarStep-1].y,MapHeight*ViewMaze.Road[CarStep-1].x,MapWidth*ViewMaze.Road[CarStep-1].y+MapWidth,MapHeight*ViewMaze.Road[CarStep-1].x+MapHeight);
				InvalidateRect(&windowSonNextRect);	

	//			CRect windowCarPosition//更新文字显示
				CRect windowTextCarposition(m_MazeWinWidth+20, 0+160,m_MazeWinWidth+100, 0+180);
				InvalidateRect(&windowTextCarposition);
			}
			else
			{
				
				KillTimer(1);
				CarStep=0;
				//AfxMessageBox("走到终点!");
				
				CRect windowSonRect(MapWidth*ViewMaze.CarPos.y,MapHeight*ViewMaze.CarPos.x,MapWidth*ViewMaze.CarPos.y+MapWidth,MapHeight*ViewMaze.CarPos.x+MapHeight);
				InvalidateRect(&windowSonRect);	

				CRect windowSonNextRect(MapWidth*ViewMaze.Road[CarStep-1].y,MapHeight*ViewMaze.Road[CarStep-1].x,MapWidth*ViewMaze.Road[CarStep-1].y+MapWidth,MapHeight*ViewMaze.Road[CarStep-1].x+MapHeight);
				InvalidateRect(&windowSonNextRect);	

	//			CRect windowCarPosition//更新文字显示
				CRect windowTextCarposition(m_MazeWinWidth+20, 0+160,m_MazeWinWidth+100, 0+180);
				InvalidateRect(&windowTextCarposition);
				CRect windowText2Rect(m_MazeWinWidth+20, 0+340,m_MazeWinWidth+100, 0+360);
				InvalidateRect(&windowText2Rect);
				ArtificialWalkFlag=0;
				HaveFoundFlag=0;
				GameStartFlag=0;
				OnTheEndFlag=1;
			}
		}
	}
	else
	{
		CarStep=0;
	}
//	CarStep=0;
	CView::OnTimer(nIDEvent);
}

void CQiyuanView::OnGameStart() 
{
	// TODO: Add your command handler code here
	GameStartFlag=1;
	if(ArtificialWalkFlag==0)//自动行走方式
	{
		if(FoundWayFlag)//存在路径
			{
						SetTimer(1,900-90*UserSpeed,NULL);
						CRect windowText2Rect(m_MazeWinWidth+20, 0+340,m_MazeWinWidth+100, 0+360);
						InvalidateRect(&windowText2Rect);
			}
	}
	else//人工行走
	{

		//ArtificialWalkFlag=0;
	}

}

void CQiyuanView::OnGamePause() 
{
	// TODO: Add your command handler code here
	GamePauseFlag=1;
	if(ArtificialWalkFlag==0)//自动行走方式
	{
		if(FoundWayFlag)//存在路径
			{
						KillTimer(1);//暂停
						CRect windowText2Rect(m_MazeWinWidth+20, 0+340,m_MazeWinWidth+100, 0+360);
						InvalidateRect(&windowText2Rect);
			}
	}
	else//人工行走
	{

		//ArtificialWalkFlag=0;
	}
}

void CQiyuanView::OnGameResume() 
{
	// TODO: Add your command handler code here
	GamePauseFlag=0;
	if(ArtificialWalkFlag==0)//自动行走方式
	{
		if(FoundWayFlag)//存在路径
			{
						SetTimer(1,900-90*UserSpeed,NULL);
						CRect windowText2Rect(m_MazeWinWidth+20, 0+340,m_MazeWinWidth+100, 0+360);
						InvalidateRect(&windowText2Rect);
			}
	}
	else//人工行走
	{
		//ArtificialWalkFlag=0;
	}
}

void CQiyuanView::OnTest() 
{
	// TODO: Add your command handler code here
	MapSet		dlg1;
	if ( IDMapSetOK==dlg1.DoModal()) 
 	{
 		FirstGame=FALSE;
//		if(IDWalkOK==dlg2.DoModal())
		
  	}
}



int CQiyuanView::GameRun()
{
		
		CarStep=0;//小车的步数初始化为零,初始化工作应该放入初始化函数

		Astep=0;//A*总步数初始化为零,初始化工作应该放入初始化函数

		dg_n=0; //修改步数初始化为零

		open_list=listnode::create_node(-1,-1,35533); 		// 创建open表,存放待扩展节点   
		closed_list=listnode::create_node(-1,-1,35533); 	// 创建closed表,放已扩展节点
		//road_list=listnode::create_node(-1,-1,35533); 	// 创建closed表,放已扩展节点
		sub_nodes=listnode::create_node(-1,-1,35533); 		// 创建sub_nodes表,放子节点
		//   调用a*搜索算法找到最佳路径存放到road连表里
		road_list=Asearch(ViewMaze.MazeMap,ViewMaze.StartPos,ViewMaze.GoalPos);

		//释放所有用到的节点资源,避免内存泄露
		listnode::Destroy(open_list);
		listnode::Destroy(sub_nodes);
		listnode::Destroy(closed_list);
		return 1;
}

int CQiyuanView::GameSet()
{
	m_Walk Setdlg2;
	UserSet dlg;
	m_ArtificialMapChoose dlg3;
			
			FoundWayFlag=0;
			HaveFoundFlag=0;
			OnTheEndFlag=0;
	if(IDOK==Setdlg2.DoModal())//第一设置行走方式和地图生成方式
	{
		ArtificialMapFlag=Setdlg2.m_CreateMethod;
		ArtificialWalkFlag=Setdlg2.m_WalkMethod;
		if(ArtificialMapFlag)
		{		
			if(IDOK==dlg3.DoModal())	//若是人工地图
			{		//调入人工地图
				ArtificialMap=dlg3.m_ArtificialMap;	
				FirstGame=FALSE;
				GameStartFlag=0;
				return 1;
			}
			else 
			{
				return 0;
			}
		}
		else//随机地图
		{	if ( IDOK==dlg.DoModal()) 
			{	

				ViewMaze.row=dlg.m_UserSetRow;
				ViewMaze.col=dlg.m_UserSetCol;
				ViewMaze.CarPos.x=ViewMaze.StartPos.x=dlg.m_UserSetStartRow-1;
				ViewMaze.CarPos.y=ViewMaze.StartPos.y=dlg.m_UserSetStartCol-1;
				ViewMaze.GoalPos.x=dlg.m_UserSetGoalRow-1;
				ViewMaze.GoalPos.y=dlg.m_UserSetGoalCol-1;
				ViewMaze.Road[0]=ViewMaze.StartPos;
				ViewMaze.name="我的迷宫";
				UserSpeed=dlg.m_UserSetSpeed;
				UserSetWallNum=float(0.5+0.1*dlg.m_UserSetWallNum);
				FirstGame=FALSE;
				GameStartFlag=0;
				return 1;
			}
			else 
			{
				return 0;
			}
		}

	}
	else 
	{
		return 0;
	}
return 0;
}

int CQiyuanView::CreateMaze()
{
		if(ArtificialMapFlag)//若是人工地图
	{		//调入人工地图
		ViewMaze=ArtificialMaze[ArtificialMap];
	}
	else//随机地图
	{
		
	int index,randnum;
	int i, j;
	//随机生成迷宫
		int index1=0,index2=0;
	for  (index1=0;index1<MOSTNUM;index1++)
		{
			for(index2=0;index2<MOSTNUM;index2++)
			{
				ViewMaze.MazeMap[index1][index2]=1;
			};
		};	
	//埋下随机种子
		srand( (unsigned)time( NULL ) );
		randnum=UserSetWallNum*ViewMaze.row*ViewMaze.col;
		//随即的改造迷宫
		for( index = 0;index < randnum;index++) 
		{	
		//取随即数
			i = rand() % ViewMaze.row;
			j = rand() % ViewMaze.col;

			if (i ==ViewMaze.row && j == ViewMaze.col) continue;
			if((j<ViewMaze.col-1)&&(i<ViewMaze.row-1)&&i&&j&&(ViewMaze.MazeMap[i][j]!= ROAD) )
			{
				ViewMaze.MazeMap[i][j]= ROAD;//修改属性为可以走的路
			}
		}
		//打开目标点和开始点周围的小区域,
		for(index1 = -SMALLAREA;index1 <=SMALLAREA;index1++)
	{
		for(index2 = -SMALLAREA;index2 <=SMALLAREA;index2++)
		{
			if(((0<(ViewMaze.GoalPos.x+index1))&&((ViewMaze.GoalPos.x+index1)<ViewMaze.row-1))&&((0<(ViewMaze.GoalPos.y+index2))&&((ViewMaze.GoalPos.y+index2)<ViewMaze.col-1)))
			{
					ViewMaze.MazeMap[ViewMaze.GoalPos.x+index1][ViewMaze.GoalPos.y+index2]=ROAD;	
			}
			if(((0<(ViewMaze.StartPos.x+index1))&&((ViewMaze.StartPos.x+index1)<ViewMaze.row-1))&&((0<
				(ViewMaze.StartPos.y+index2))&&(ViewMaze.StartPos.y+index2<ViewMaze.col-1)))
					{					
						ViewMaze.MazeMap[ViewMaze.StartPos.x+index1][ViewMaze.StartPos.y+index2]=ROAD;					
					}
		}	
	}

	}
	ViewMaze.MazeMap[ViewMaze.GoalPos.x][ViewMaze.GoalPos.y]=0;
	ViewMaze.MazeMap[ViewMaze.StartPos.x][ViewMaze.StartPos.y]=CAR;
	//	更新一下显示
	CRect windowRect;
	GetClientRect(&windowRect);
	Invalidate();
return 1;
}

void CQiyuanView::OnChooseRobot() //选择机器人的处理函数
{
	// TODO: Add your command handler code here
	RobotChoose Robotdlg;
	if(IDOK==Robotdlg.DoModal())	//若是人工地图
	{		//调入人工地图
		RobotNum=Robotdlg.m_RobotChoose;	
	}
	//更新显示
	CRect windowSonRect(MapWidth*ViewMaze.CarPos.y,MapHeight*ViewMaze.CarPos.x,MapWidth*ViewMaze.CarPos.y+MapWidth,MapHeight*ViewMaze.CarPos.x+MapHeight);
	InvalidateRect(&windowSonRect);	
	return ;
}

int CQiyuanView::LoadCar()
{
return 1;
}

void CQiyuanView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(GameStartFlag)
	{
		OnGameResume();
	}
	else
	{
		OnGameStart();
	}
	CView::OnLButtonDblClk(nFlags, point);
}

void CQiyuanView::OnRButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
OnGameResume();
	CView::OnRButtonDblClk(nFlags, point);
}

void CQiyuanView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(GameStartFlag)
	{
		OnGameResume();
	}
	else
	{
		OnGameStart();
	}
	CView::OnLButtonUp(nFlags, point);
}

void CQiyuanView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	OnGamePause();
	CView::OnRButtonUp(nFlags, point);
}

⌨️ 快捷键说明

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