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

📄 astar.cpp

📁 寻径算法演示平台~~提供给计算机博弈爱好者参考学习
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				char buf[4];
				sprintf(buf,"%d", m_element[i][j]->f);
				pDC->TextOut(j*bmMetric2.bmWidth,i*bmMetric2.bmHeight,buf);
			}
		}
	}

	pDC->SelectObject(poldfont);
	newfont->DeleteObject();

	*/
	
	if(showMode == 1)
	{
		//显示搜索过的点(还在open表里面的是红色,在close表里面的是绿色的)
		for(i=0;i<Height;i++)
		{
			for(int j=0;j<Width;j++)
			{
				if(m_element[i][j]->f!=-1)
				{
					if(m_element[i][j]->open)
					{
						memDC.SelectObject(&bm3);
						pDC->BitBlt(j*bmMetric2.bmWidth, i*bmMetric2.bmHeight,bmMetric2.bmWidth,bmMetric2.bmHeight,&memDC,0,0,SRCCOPY);
					}
					else if(m_element[i][j]->close)
					{
						memDC.SelectObject(&bm4);
						pDC->BitBlt(j*bmMetric2.bmWidth, i*bmMetric2.bmHeight,bmMetric2.bmWidth,bmMetric2.bmHeight,&memDC,0,0,SRCCOPY);
					}
					//char buf[4];
					//sprintf(buf,"%d", m_element[i][j]->f);
					//pDC->TextOut(j*bmMetric2.bmWidth,i*bmMetric2.bmHeight,buf);
				}
			}
		}
	}


	//用渐变颜色显示搜索过的点
	if(showMode == 2)
	{
		//先找出估值的最大值
		double max = 0;
		for(i=0;i<Height;i++)
		{
			for(int j=0;j<Width;j++)
			{
				if(m_element[i][j]->f > max)
				{
					max = m_element[i][j]->f;
				}
			}
		}
		//已经找到估值的最大值

		const int colorBeg = 70;
		int totalStep = (255 - colorBeg) * 5;
		int R, G, B, temp;
		for(int i=0;i<Height;i++)
		{
			for(int j=0;j<Width;j++)
			{
				if(m_element[i][j]->f != -1)
				{
					int curStep = (int)(m_element[i][j]->f/max * totalStep);
					
					if(curStep > (temp = (255 - colorBeg) * 4))
					{
						R = curStep - temp + colorBeg;
						G = colorBeg;
						B = 255;
					}
					else if(curStep > (temp = (255 - colorBeg) * 3))
					{
						R = colorBeg;
						G = 255 - (curStep - temp);
						B = 255;
					}
					else if(curStep > (temp = (255 - colorBeg) * 2))
					{
						R = colorBeg;
						G = 255;
						B = curStep - temp + colorBeg;
					}
					else if(curStep > (temp = (255 - colorBeg)))
					{
						R = 255 - (curStep - temp);
						G = 255;
						B = colorBeg;
					}
					else
					{
						R = 255;
						G = curStep + colorBeg;
						B = colorBeg;
					}
					CRect tempRect(j*bmMetric2.bmWidth,i*bmMetric2.bmHeight,(j+1)*bmMetric2.bmWidth,(i+1)*bmMetric2.bmHeight);
					CBrush brushBg;
					brushBg.CreateSolidBrush(RGB(R,G,B));
					pDC->FillRect(&tempRect,&brushBg);
				}
			}
		}
	}
//结束描绘渐变色


	//用渐变颜色一步一步显示搜索过的点
	if(showMode == 3)
	{
		//先找出估值的最大值
		double max = 0;
		for(i=0;i<Height;i++)
		{
			for(int j=0;j<Width;j++)
			{
				if(m_element[i][j]->f > max)
				{
					max = m_element[i][j]->f;
				}
			}
		}
		//已经找到估值的最大值

		const int colorBeg = 70;
		int totalStep = (255 - colorBeg) * 5;
		int R, G, B, temp;
		for(int i=0;i<Height;i++)
		{
			for(int j=0;j<Width;j++)
			{
				if(m_element[i][j]->f != -1)
				{
					int curStep = (int)(m_element[i][j]->f/max * totalStep);
					
					if(curStep > (temp = (255 - colorBeg) * 4))
					{
						R = curStep - temp + colorBeg;
						G = colorBeg;
						B = 255;
					}
					else if(curStep > (temp = (255 - colorBeg) * 3))
					{
						R = colorBeg;
						G = 255 - (curStep - temp);
						B = 255;
					}
					else if(curStep > (temp = (255 - colorBeg) * 2))
					{
						R = colorBeg;
						G = 255;
						B = curStep - temp + colorBeg;
					}
					else if(curStep > (temp = (255 - colorBeg)))
					{
						R = 255 - (curStep - temp);
						G = 255;
						B = colorBeg;
					}
					else
					{
						R = 255;
						G = curStep + colorBeg;
						B = colorBeg;
					}
					CRect tempRect(j*bmMetric2.bmWidth,i*bmMetric2.bmHeight,(j+1)*bmMetric2.bmWidth,(i+1)*bmMetric2.bmHeight);
					CBrush brushBg;
					brushBg.CreateSolidBrush(RGB(R,G,B));
					pDC->FillRect(&tempRect,&brushBg);
				}
			}
		}
	}
	//用渐变颜色一步一步显示搜索过的点结束

	//显示最短路径
	if(showMode != 3)
	{
		CPen pen,*p_pen;
		pen.CreatePen(PS_SOLID,2,RGB(0,0,255));
		p_pen = pDC->SelectObject(&pen);
		pDC->MoveTo(targetX * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , targetY * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
		while(tempHead!=NULL)
		{
			pDC->LineTo(tempHead->x * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , tempHead->y * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
			tempHead = tempHead->parent;
		}
		pDC->SelectObject(p_pen);
		pen.DeleteObject();
	}


	

	memDC.SelectObject(pbm);
	bm1.DeleteObject();
	bm2.DeleteObject();
	memDC.DeleteDC();

	//return time;//返回搜索路径所需要的时间
}

void CAStar::prepareForStepByStep()
{
	for(int i=0;i<Height;i++)
	{
		for(int j=0;j<Width;j++)
		{
			if(map[i][j]==0)
			{
				head = m_element[i][j];
				head->x=j;
				head->y=i;
				head->fore=NULL;
				head->next=NULL;
				head->g=0;
				//head->h=
				head->close=0;
				head->open=1;
				//head->f=
				head->parent=NULL;
			}
			else if(map[i][j]==-3)
			{
				targetX=j;
				targetY=i;
			}
		}
	}
}

int CAStar::searchThePathStepByStep()
{
	if(head!=NULL)
	{
		as_node *tempHead;
		
		if(head->next==NULL)
		{
			tempHead=NULL;
		}
		else
		{
			tempHead=head->next;//head下一个节点作为新的链表头指针
			
			head->next->fore=NULL;
		}
		m_element[head->y][head->x]->open=0;
		m_element[head->y][head->x]->close=1;//把head放进close表
		
		if(map[head->y][head->x+1]!=-1)//如果临近的方格不是墙壁
		{
			if(m_element[head->y][head->x+1]->close)
			{}
			else if(m_element[head->y][head->x+1]->open)
			{
				int temp = m_element[head->y][head->x]->g+1+m_element[head->y][head->x+1]->h;
				if(m_element[head->y][head->x+1]->f > temp)
				{
					m_element[head->y][head->x+1]->g = m_element[head->y][head->x]->g+1;
					m_element[head->y][head->x+1]->f = temp;
					m_element[head->y][head->x+1]->parent = m_element[head->y][head->x];
					tempHead=adjustNode(tempHead,m_element[head->y][head->x+1]);
				}
			}	
			else
			{
				if(head->x+1==targetX && head->y==targetY){return 1;}//找到终点!//修改这里
				
				//as_node *node=new as_node;
				as_node *node=m_element[head->y][head->x+1];
				node->x=head->x+1;//修改这里
				node->y=head->y;//修改这里
				node->open=1;
				node->close=0;
				node->parent=m_element[head->y][head->x];
				node->g=node->parent->g+1;
				node->h=abs(node->x - targetX) + abs(node->y - targetY);
				node->f=node->g + node->h;
				tempHead=insertNode(tempHead,node);
			}
			//最后记住把head放进close表
		}
		
		if(map[head->y][head->x-1]!=-1)//如果临近的方格不是墙壁
		{
			if(m_element[head->y][head->x-1]->close)
			{}
			else if(m_element[head->y][head->x-1]->open)
			{
				int temp = m_element[head->y][head->x]->g+1+m_element[head->y][head->x-1]->h;
				if(m_element[head->y][head->x-1]->f > temp)
				{
					m_element[head->y][head->x-1]->g = m_element[head->y][head->x]->g+1;
					m_element[head->y][head->x-1]->f = temp;
					m_element[head->y][head->x-1]->parent=m_element[head->y][head->x];
					tempHead=adjustNode(tempHead,m_element[head->y][head->x-1]);
				}
			}	
			else
			{
				if(head->x-1==targetX && head->y==targetY){return 1;}//找到终点!//修改这里
				
				//as_node *node=new as_node;
				as_node *node=m_element[head->y][head->x-1];
				node->x=head->x-1;//修改这里
				node->y=head->y;//修改这里
				node->open=1;
				node->close=0;
				node->parent=m_element[head->y][head->x];
				node->g=node->parent->g+1;
				node->h=abs(node->x - targetX) + abs(node->y - targetY);
				node->f=node->g + node->h;
				tempHead=insertNode(tempHead,node);
			}
			//最后记住把head放进close表
		}
		
		if(map[head->y+1][head->x]!=-1)//如果临近的方格不是墙壁
		{
			if(m_element[head->y+1][head->x]->close)
			{}
			else if(m_element[head->y+1][head->x]->open)
			{
				int temp = m_element[head->y][head->x]->g+1+m_element[head->y+1][head->x]->h;
				if(m_element[head->y+1][head->x]->f > temp)
				{
					m_element[head->y+1][head->x]->g = m_element[head->y][head->x]->g+1;
					m_element[head->y+1][head->x]->f = temp;
					m_element[head->y+1][head->x]->parent=m_element[head->y+1][head->x];
					tempHead=adjustNode(tempHead,m_element[head->y+1][head->x]);
				}
			}	
			else
			{
				if(head->x==targetX && head->y+1==targetY){return 1;}//找到终点!//修改这里
				
				//as_node *node=new as_node;
				as_node *node=m_element[head->y+1][head->x];
				node->x=head->x;//修改这里
				node->y=head->y+1;//修改这里
				node->open=1;
				node->close=0;
				node->parent=m_element[head->y][head->x];
				node->g=node->parent->g+1;
				node->h=abs(node->x - targetX) + abs(node->y - targetY);
				node->f=node->g + node->h;
				tempHead=insertNode(tempHead,node);
			}
			//最后记住把head放进close表
		}
		
		
		if(map[head->y-1][head->x]!=-1)//如果临近的方格不是墙壁
		{
			if(m_element[head->y-1][head->x]->close)
			{}
			else if(m_element[head->y-1][head->x]->open)
			{
				int temp = m_element[head->y][head->x]->g+1+m_element[head->y-1][head->x]->h;
				if(m_element[head->y-1][head->x]->f > temp)
				{
					m_element[head->y-1][head->x]->g = m_element[head->y][head->x]->g+1;
					m_element[head->y-1][head->x]->f = temp;
					m_element[head->y-1][head->x]->parent=m_element[head->y-1][head->x];
					tempHead=adjustNode(tempHead,m_element[head->y-1][head->x]);
				}
			}	
			else
			{
				if(head->x==targetX && head->y-1==targetY){return 1;}//找到终点!//修改这里
				
				//as_node *node=new as_node;
				as_node *node=m_element[head->y-1][head->x];
				node->x=head->x;//修改这里
				node->y=head->y-1;//修改这里
				node->open=1;
				node->close=0;
				node->parent=m_element[head->y][head->x];
				node->g=node->parent->g+1;
				node->h=abs(node->x - targetX) + abs(node->y - targetY);
				node->f=node->g + node->h;
				tempHead=insertNode(tempHead,node);
			}
			//最后记住把head放进close表
		}		
		head=tempHead;
		return 2;
	}
	return 0;//如果head是NULL表示open表已为空,所以找不到路径
}

⌨️ 快捷键说明

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