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

📄 wumpusdlg.cpp

📁 一款有关于人工智能的游戏的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	int i,j;
	int ran,max;
	memset(m_World,0,8*8*sizeof(int));
	m_nGold=0;
	m_nPit=0;
	m_nWumpus=0;
	max=(int)(m_nHeight+m_nWidth)/2;
	srand( (unsigned)time( NULL ) );
	while(1)
	{
		ran=(int)rand()%max+1;
		i=(int)rand()%m_nHeight;
		j=(int)rand()%m_nWidth;
		if((i==0&&j==0))//避开起点
		continue;
		if(m_World[i][j]==0)
		{
			if(ran==1&&m_nPit<3)
			{
				m_nPit++;
				m_World[i][j]=1;
			}
			if(ran==2&&m_nGold==0)
			{
				m_nGold++;
				m_World[i][j]=2;
			}
			if(ran==3&&m_nWumpus==0)
			{
				m_nWumpus++;
				m_World[i][j]=3;
			}
		}
		if(m_nPit==3&&m_nGold==1&&m_nWumpus==1)
			break;

	}
					
	Invalidate();
}


void CWumpusDlg::OnStartButton() 
{
	// TODO: Add your control notification handler code here
		int i,j;
	on_start=1;
	m_World[0][0]=4;
	Invalidate();
	pit=3;gold=1;wumpus=1;
	for(i=0;i<5;i++)
		for(j=0;j<5;j++)
		{
			if(m_World[i][j]==1)
				Hero[i][j].pit=TRUE;
			if(m_World[i][j]==2)
				Hero[i][j].gold=TRUE;
			if(m_World[i][j]==3)
				Hero[i][j].wumpus=TRUE;
			if(Hero[i][j].pit==TRUE)
			{
				if(i-1>=0)
					Hero[i-1][j].Breeze =TRUE;
				if(i+1<5)
					Hero[i+1][j].Breeze=TRUE;
				if(j-1>=0)
					Hero[i][j-1].Breeze =TRUE;
				if(j+1<5)
					Hero[i][j+1].Breeze =TRUE;
			}
			if(Hero[i][j].wumpus ==TRUE)
			{
				if(i-1>=0)
					Hero[i-1][j].Stench =TRUE;
				if(i+1<5)
					Hero[i+1][j].Stench =TRUE;
				if(j-1>=0)
					Hero[i][j-1].Stench  =TRUE;
				if(j+1<5)
					Hero[i][j+1].Stench =TRUE;
			}
		}
	at_location();			//处理当前位置情况
}

bool CWumpusDlg::at_location()
{
	if(end_up==TRUE)
	{
		step_end=step;
		return TRUE;	//开始键为0则结束程序
	}
	++Hero[x][y].count;		//对经过当前位置的次数计数
	if(Hero[x][y].gold==TRUE)
	{
		found_gold=TRUE;
		goal_forward(0,0);		//捡到金子,向目标(0,0)位置前进
		end_up=TRUE;				//结束程序
		step_end=step;
		return TRUE;
	}
	if((Hero[x][y].pit==TRUE) ||( Hero[x][y].wumpus==TRUE))	//掉入陷井或进入wumpus房间,结束程序
	{
		end_up=TRUE;
		step_end=step;
		return TRUE;
	}
	SetValue(x,y);				//第一次处于当前位置,感知环境,并对各种情况赋值,保存无风险路径
	if(m_feasible>0)
	{
		if(Hero[goal_location[m_feasible-1].cx][goal_location[m_feasible-1].cy].count==0)
			{
				goal_forward(goal_location[m_feasible-1].cx,goal_location[m_feasible-1].cy);//向无风险的目标位置前进
				m_feasible--;
			}
		else
		m_feasible--;
	}
	else
	{
		risk_direction();		//判定具有最小风险的目标位置,并到达该位置
	}
	at_location();
	return TRUE;
}

void CWumpusDlg::forward_one_step()
{
//	m_World[x][y]=0;
	switch(direction)
	{
	case 1:
		x=x+1;
		break;
	case 2:
		y=y-1;		
		break;
	case 3:
		y=y+1;
		break;
	case 4:
		x=x-1;		
		break;
	default:
		break;
	}

//	m_World[x][y]=4;
	++step;
	pass[step].cx=x;
	pass[step].cy=y;
	if(step_wumpus==0)
	if(at_wumpus.cx==x&&at_wumpus.cy==y)
	if(!(at_wumpus.cx==0&&at_wumpus.cy==0))
		step_wumpus=step;
//	Invalidate();
//	Sleep(40);
	return;
}
void CWumpusDlg::goal_distance(int i,int j)
{
	int m,n,step;
	for(m=0;m<5;m++)
		for(n=0;n<5;n++)
			Hero[m][n].distance=-1;
	Hero[i][j].distance=0;
	for(step=0;step<25;step++)
	{
		for(m=0;m<5;m++)
			for(n=0;n<5;n++)
				if(Hero[m][n].distance==step)
				{
					if(m-1>=0)
						if(Hero[m-1][n].distance<0&&Hero[m-1][n].count>0)
							Hero[m-1][n].distance=step+1;
					if(n-1>=0)
						if(Hero[m][n-1].distance<0&&Hero[m][n-1].count>0)
							Hero[m][n-1].distance=step+1;
					if(m+1<5)
						if(Hero[m+1][n].distance<0&&Hero[m+1][n].count>0)
							Hero[m+1][n].distance=step+1;
					if(n+1<5)
						if(Hero[m][n+1].distance<0&&Hero[m][n+1].count>0)
							Hero[m][n+1].distance=step+1;
				}
	}
}
void CWumpusDlg::goal_forward(int i, int j)
{
	int distance=0;
	direction=0;
//到达目标位置,
	if(x==i&&y==j)
		return;
	goal_distance(i,j);			//计算出已走过的各点到目标位置的距离
	distance=Hero[x][y].distance;
//选定当前位置的相信方格中,距目标位置最近的点
	if(distance>0)
	{
		if(y-1>=0)
			if(Hero[x][y-1].distance==distance-1)
				direction=2;								//向上;
		if(x-1>=0)
			if(Hero[x-1][y].distance==distance-1)
				direction=4;								//向左;
		if(y+1<5)
			if(Hero[x][y+1].distance==distance-1)
				direction=3;								//向下;
		if(x+1<5)
			if(Hero[x+1][y].distance==distance-1)
				direction=1;								//向右;
	}
//选定方向后,向选定方向前进一步 
	forward_one_step();
//继续向目标方向前进
	goal_forward(i,j);
	return;
}


void CWumpusDlg::risk_direction()
{
	int wx=0,wy=0,max_wumpus=0,pitx,pity,min_pit=20;
	int i,j;
	bool IsBack=TRUE;
	m_feasible=0;	//无风险的目标数为零

	for(i=0;i<5;i++)
		for(j=0;j<5;j++)
		{
			max_wumpus=(max_wumpus>Hero[i][j].IsWumpus)?max_wumpus:Hero[i][j].IsWumpus;
			if(Hero[i][j].IsWumpus>=max_wumpus)
			{
				wx=i;	
				wy=j;	
				max_wumpus=Hero[i][j].IsWumpus;
			}
			if(Hero[i][j].IsWumpus==1&&max_wumpus==1)
			{
				wx=i;
				wy=j;
			}
		}
	for(i=0;i<5;i++)
		for(j=0;j<5;j++)
		{
			if(Hero[i][j].IsPit>0)
			min_pit=(min_pit<Hero[i][j].IsPit)?min_pit:Hero[i][j].IsPit;
			if(Hero[i][j].IsPit ==1)
			{
				pitx=i;pity=j;
			}
			if(Hero[i][j].IsPit >1&&min_pit>1)
			{
				pitx=i;pity=j;
			}			
		}
	if(max_wumpus>0)
	if(arrow==TRUE)		//	&&Hero[wx][wy].IsPit<0
	{
		
		if(Hero[wx][wy].wumpus ==TRUE)
			{
				if(wx-1>=0)
					Hero[wx-1][wy].Stench =FALSE;
				if(wx+1<5)
					Hero[wx+1][wy].Stench =FALSE;
				if(j-1>=0)
					Hero[wx][wy-1].Stench =FALSE;
				if(j+1<5)
					Hero[wx][wy+1].Stench =FALSE;
			}
		Hero[wx][wy].wumpus=FALSE;				//射出一箭,变成无箭状态
		at_wumpus.cx=wx;
		at_wumpus.cy=wy;
		goal_forward(wx,wy);			//前进到一新位置,返回到at_location中感知外界信息
		arrow=FALSE;
		at_location();
//		SetValue();
//		goal_forward(0,0);
//	/	end_up=TRUE;
		IsBack=TRUE;
		return;
	}
	if(min_pit>0&&min_pit<20)
		if(Hero[pitx][pity].IsWumpus<0&&Hero[pitx][pity].IsPit==1)
		{
			goal_forward(pitx,pity);
			IsBack=TRUE;
			return;
		}

	goal_forward(0,0);
	end_up=TRUE;
	return;
}

void CWumpusDlg::SetValue(int x,int y)		//感知环境,并对各种情况赋值
{
	Hero[x][y].IsPit=-20;
	Hero[x][y].IsWumpus =-20;

	if(Hero[x][y].Breeze==TRUE)     //感受到微风,若相邻房间信息未知,则设定可能有微风
	{
		if(y+1<5)
			if(Hero[x][y+1].count==0)
				++Hero[x][y+1].IsPit;
		if(x-1>=0)
			if(Hero[x-1][y].count==0)
				++Hero[x-1][y].IsPit;
		if(y-1>=0)
			if(Hero[x][y-1].count==0)
				++Hero[x][y-1].IsPit;
		if(x+1<5)
			if(Hero[x+1][y].count==0)
				++Hero[x+1][y].IsPit;		
	}
	else							//没有感受到微风,则设定相邻房间一定没有微风
	{
		if(y+1<5)
			Hero[x][y+1].IsPit=-20;
		if(x-1>=0)
			Hero[x-1][y].IsPit=-20;
		if(x+1<5)
			Hero[x+1][y].IsPit=-20;
		if(y-1>=0)
			Hero[x][y-1].IsPit=-20;
		
	}

	if(Hero[x][y].Stench ==TRUE )	//闻到嗅味,则设定具有不确定信息的相邻房间可能有WUMPUS
	{
		if(x-1>=0)
			if(Hero[x-1][y].count==0)
				++Hero[x-1][y].IsWumpus;
		if(x+1<5)
			if(Hero[x+1][y].count==0)
				++Hero[x+1][y].IsWumpus;
		if(y-1>=0)
			if(Hero[x][y-1].count==0)
				++Hero[x][y-1].IsWumpus;
		if(y+1<5)
			if(Hero[x][y+1].count==0)
				++Hero[x][y+1].IsWumpus;
	}
	else						//没有闻到嗅味,则设定相邻房间一定没有WUMPUS
	{
		if(x-1>=0)
				Hero[x-1][y].IsWumpus=-20;
		if(x+1<5)
			Hero[x+1][y].IsWumpus=-20;
		if(y-1>=0)
			Hero[x][y-1].IsWumpus=-20;
		if(y+1<5)
			Hero[x][y+1].IsWumpus=-20;
	}
	if(Hero[x][y].Breeze ==FALSE&&Hero[x][y].Stench ==FALSE)
	{
		if(y-1>=0)
			if(Hero[x][y-1].count==0)
			{
				goal_location[m_feasible].cx=x;
				goal_location[m_feasible].cy=y-1;
				++m_feasible;
			}
		if(x-1>=0)
			if(Hero[x-1][y].count==0)
			{
				goal_location[m_feasible].cx=x-1;
				goal_location[m_feasible].cy=y;
				++m_feasible;
			}
		if(y+1<5)
			if(Hero[x][y+1].count==0)
			{
				goal_location[m_feasible].cx=x;
				goal_location[m_feasible].cy=y+1;
				++m_feasible;
			}
		if(x+1<5)
			if(Hero[x+1][y].count==0)
			{
				goal_location[m_feasible].cx=x+1;
				goal_location[m_feasible].cy=y;
				++m_feasible;
			}
	}
}

void CWumpusDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
//	CDC* pDC;
//	HDC hdc;
//	int a,b,i;
	if(end_up==false)
		return;
	if(arrow_wumpus>=20)
		arrow_draw=false;
	if(step_count==step_wumpus-1)
	if(nIDEvent==2&&arrow_wumpus<20)
	{	
		arrow_draw=true;
		Invalidate();
		arrow_wumpus=arrow_wumpus+3;
	}	
	if(nIDEvent==1)
	if(arrow_draw==false)
	{
		if(step_end!=0&&step_count<step_end)
		{
			Invalidate();
			++step_count;
			m_World[pass[step_count-1].cx][pass[step_count-1].cy]=0;
			m_World[pass[step_count].cx][pass[step_count].cy]=4;

		}
	}
	CDialog::OnTimer(nIDEvent);
}



void CWumpusDlg::OnSetPitRadio()
{ m_setstyle=0;
}



void CWumpusDlg::OnSetWumpusRadio() 
{
	// TODO: Add your control notification handler code here
	m_setstyle=1;
	
}

void CWumpusDlg::OnSetGoldRadio() 
{
	// TODO: Add your control notification handler code here
	m_setstyle=2;	
}

void CWumpusDlg::OnStaticMouseMove()
{

}

⌨️ 快捷键说明

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