?? wumpusdlg.cpp
字號(hào):
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))//避開(kāi)起點(diǎn)
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(); //處理當(dāng)前位置情況
}
bool CWumpusDlg::at_location()
{
if(end_up==TRUE)
{
step_end=step;
return TRUE; //開(kāi)始鍵為0則結(jié)束程序
}
++Hero[x][y].count; //對(duì)經(jīng)過(guò)當(dāng)前位置的次數(shù)計(jì)數(shù)
if(Hero[x][y].gold==TRUE)
{
found_gold=TRUE;
goal_forward(0,0); //撿到金子,向目標(biāo)(0,0)位置前進(jìn)
end_up=TRUE; //結(jié)束程序
step_end=step;
return TRUE;
}
if((Hero[x][y].pit==TRUE) ||( Hero[x][y].wumpus==TRUE)) //掉入陷井或進(jìn)入wumpus房間,結(jié)束程序
{
end_up=TRUE;
step_end=step;
return TRUE;
}
SetValue(x,y); //第一次處于當(dāng)前位置,感知環(huán)境,并對(duì)各種情況賦值,保存無(wú)風(fēng)險(xiǎn)路徑
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);//向無(wú)風(fēng)險(xiǎn)的目標(biāo)位置前進(jìn)
m_feasible--;
}
else
m_feasible--;
}
else
{
risk_direction(); //判定具有最小風(fēng)險(xiǎn)的目標(biāo)位置,并到達(dá)該位置
}
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;
//到達(dá)目標(biāo)位置,
if(x==i&&y==j)
return;
goal_distance(i,j); //計(jì)算出已走過(guò)的各點(diǎn)到目標(biāo)位置的距離
distance=Hero[x][y].distance;
//選定當(dāng)前位置的相信方格中,距目標(biāo)位置最近的點(diǎn)
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; //向右;
}
//選定方向后,向選定方向前進(jìn)一步
forward_one_step();
//繼續(xù)向目標(biāo)方向前進(jìn)
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; //無(wú)風(fēng)險(xiǎn)的目標(biāo)數(shù)為零
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; //射出一箭,變成無(wú)箭狀態(tài)
at_wumpus.cx=wx;
at_wumpus.cy=wy;
goal_forward(wx,wy); //前進(jìn)到一新位置,返回到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) //感知環(huán)境,并對(duì)各種情況賦值
{
Hero[x][y].IsPit=-20;
Hero[x][y].IsWumpus =-20;
if(Hero[x][y].Breeze==TRUE) //感受到微風(fēng),若相鄰房間信息未知,則設(shè)定可能有微風(fēng)
{
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 //沒(méi)有感受到微風(fēng),則設(shè)定相鄰房間一定沒(méi)有微風(fēng)
{
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 ) //聞到嗅味,則設(shè)定具有不確定信息的相鄰房間可能有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 //沒(méi)有聞到嗅味,則設(shè)定相鄰房間一定沒(méi)有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()
{
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -