📄 wumpus.cpp
字号:
else if(y>0&&handbook[x][y-1].in==blank&&handbook[x][y-1].visittime==0)
{
int step=abs(W-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=W;
}
else
{
if(x>0&&handbook[x-1][y].in==blank&&handbook[x-1][y].visittime<3)
{
int step=abs(N-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=N;
}
else if(y<3&&handbook[x][y+1].in==blank&&handbook[x][y+1].visittime<3)
{
int step=abs(E-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=E;
}
else if(x<3&&handbook[x+1][y].in==blank&&handbook[x+1][y].visittime<3)
{
int step=abs(S-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=S;
}
else if(y>0&&handbook[x][y-1].in==blank&&handbook[x][y-1].visittime<3)
{
int step=abs(W-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=W;
}
else
{
int evaluationN,evaluationS,evaluationW,evaluationE;
if(x>0)
{
evaluationN=3*handbook[x-1][y].pit+3*handbook[x-1][y].wumpus+handbook[x-1][y].visittime;
}
else evaluationN=100000;
if(x<3)
{
evaluationS=3*handbook[x+1][y].pit+3*handbook[x+1][y].wumpus+handbook[x+1][y].visittime;
}
else evaluationS=100000;
if(y>0)
{
evaluationW=3*handbook[x][y-1].pit+3*handbook[x][y-1].wumpus+handbook[x][y-1].visittime;
}
else evaluationW=100000;
if(y<3)
{
evaluationE=3*handbook[x][y+1].pit+3*handbook[x][y+1].wumpus+handbook[x][y+1].visittime;
}
else evaluationE=100000;
int evaluation=evaluationS;
if(evaluation>evaluationW) evaluation=evaluationW;
if(evaluation>evaluationE) evaluation=evaluationE;
if(evaluation>evaluationN) evaluation=evaluationN;
if(evaluation<4&&evaluation==evaluationN)
{
int step=abs(N-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=N;
}
else if(evaluation<4&&evaluation==evaluationS)
{
int step=abs(S-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=S;
}
else if(evaluation<4&&evaluation==evaluationW)
{
int step=abs(W-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=W;
}
else if(evaluation<4&&evaluation==evaluationE)
{
int step=abs(E-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=E;
}
else
{
noway=true;
int w=0;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(handbook[i][j].wumpus>w)
{
w=handbook[i][j].wumpus;
wumpos.x=i;
wumpos.y=j;
}
}
}
}
}
}
void Hero::choosedirB()//回到洞口,应尽可能地往南和往西走
{
if(x==3&&y==0)
climb();
else if(x<3&&(handbook[x+1][y].in==blank||handbook[x+1][y].in==deadwumpus))
currentdirection=S;
else if(y>0&&(handbook[x][y-1].in==blank||handbook[x][y-1].in==deadwumpus))
currentdirection=W;
else choosedirA();
}
void Hero::Init()
{
alive=true;
arrow=true;
currentdirection=N;
knownwupos=false;
gotgold=false;
noway=false;
wumpusalive=true;
success=false;
score=0;
firstgetgold=false;
firstshootwumpus=false;
x=3;
y=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
handbook[i][j].in=unknown;
handbook[i][j].wumpus=0;
handbook[i][j].pit=0;
handbook[i][j].visittime=0;
}
}
}
void Hero::feel()
{
grid currentgrid=map.gridstate(x,y);
handbook[x][y].visittime++;
if(currentgrid.sthin==wumpus)
{
score-=10000;
cout<<"可惜我们敬爱的英雄英年早逝,不小心被巫魔吃掉了!"<<endl;
HeroInfo+="可惜我们敬爱的英雄英年早逝,不小心被巫魔吃掉了!\r\n";
alive=false;
return;
}
if(currentgrid.sthin==pit)
{
score-=10000;
cout<<"可惜我们敬爱的英雄不小心掉到了陷阱里,挂了!"<<endl;
HeroInfo+="可惜我们敬爱的英雄不小心掉到了陷阱里,挂了!\r\n";
alive=false;
return;
}
if(handbook[x][y].visittime<=2)
{
if(!currentgrid.breeze&&!currentgrid.stench&&!currentgrid.glitter)
{
cout<<"英雄什么都没感觉到,附近有可能是安全的!"<<endl;
HeroInfo+="英雄什么都没感觉到,附近有可能是安全的!\r\n";
if(x>0)
{
handbook[x-1][y].in=blank;
handbook[x-1][y].pit=0;
handbook[x-1][y].wumpus=0;
}
if(x<3)
{
handbook[x+1][y].in=blank;
handbook[x+1][y].pit=0;
handbook[x+1][y].wumpus=0;
}
if(y>0)
{
handbook[x][y-1].in=blank;
handbook[x][y-1].pit=0;
handbook[x][y-1].wumpus=0;
}
if(y<3)
{
handbook[x][y+1].in=blank;
handbook[x][y+1].pit=0;
handbook[x][y+1].wumpus=0;
}
handbook[x][y].in=blank;
handbook[x][y].pit=0;
handbook[x][y].wumpus=0;
}
else
{
if(currentgrid.breeze)
{
cout<<"英雄听到了阵阵风声,附近应该有陷阱!"<<endl;
HeroInfo+="英雄听到了阵阵风声,附近应该有陷阱!\r\n";
if(x>0&&handbook[x-1][y].in==unknown)
handbook[x-1][y].pit++;
if(x<3&&handbook[x+1][y].in==unknown)
handbook[x+1][y].pit++;
if(y>0&&handbook[x][y-1].in==unknown)
handbook[x][y-1].pit++;
if(y<3&&handbook[x][y+1].in==unknown)
handbook[x][y+1].pit++;
handbook[x][y].in=blank;
handbook[x][y].pit=0;
handbook[x][y].wumpus=0;
}
if(currentgrid.stench)
{
if(wumpusalive)
{
cout<<"英雄闻到了臭味,附近应该有巫魔..."<<endl;
HeroInfo+="英雄闻到了臭味,附近应该有巫魔...\r\n";
int evaluationN,evaluationS,evaluationW,evaluationE,evaluation;
if(x>0&&handbook[x-1][y].in==unknown)
{
handbook[x-1][y].wumpus++;
evaluationN=handbook[x-1][y].wumpus;
if(evaluationN>=3&&wumpusalive)
{
knownwupos=true;
wumpos.x=x-1;
wumpos.y=y;
handbook[x-1][y].in=wumpus;
}
}
if(x<3&&handbook[x+1][y].in==unknown)
{
handbook[x+1][y].wumpus++;
evaluationS=handbook[x+1][y].wumpus;
if(evaluationS>=3&&wumpusalive)
{
knownwupos=true;
wumpos.x=x+1;
wumpos.y=y;
handbook[x+1][y].in=wumpus;
}
}
if(y>0&&handbook[x][y-1].in==unknown)
{
handbook[x][y-1].wumpus++;
evaluationW=handbook[x][y-1].wumpus;
if(evaluationW>=3&&wumpusalive)
{
knownwupos=true;
wumpos.x=x;
wumpos.y=y-1;
handbook[x][y-1].in=wumpus;
}
}
if(y<3&&handbook[x][y+1].in==unknown)
{
handbook[x][y+1].wumpus++;
evaluationE=handbook[x][y+1].wumpus;
if(evaluationE>=3&&wumpusalive)
{
knownwupos=true;
wumpos.x=x;
wumpos.y=y+1;
handbook[x][y+1].in=wumpus;
}
}
if(stepcount>=16&&!knownwupos)
{
evaluation=evaluationN;
wumpos.x=x-1;
wumpos.y=y;
if(evaluation<evaluationS)
{
evaluation=evaluationS;
wumpos.x=x+1;
wumpos.y=y;
}
else if(evaluation<evaluationW)
{
evaluation=evaluationW;
wumpos.x=x;
wumpos.y=y-1;
}
else if(evaluation<evaluationE)
{
evaluation=evaluationE;
wumpos.x=x;
wumpos.y=y+1;
}
}
handbook[x][y].in=blank;
}
else
{
cout<<"英雄闻到了射死了的巫魔的臭味,没什么可怕的了!"<<endl;
HeroInfo+="英雄闻到了射死了的巫魔的臭味,没什么可怕的了!\r\n";
handbook[x][y].in=blank;
}
handbook[x][y].in=blank;
handbook[x][y].pit=0;
handbook[x][y].wumpus=0;
}
if(currentgrid.glitter)
{
cout<<"英雄看到了闪光,该格子里有金子!"<<endl;
HeroInfo+="英雄看到了闪光,该格子里有金子!\r\n";
grab();
}
handbook[x][y].in=blank;
handbook[x][y].pit=0;
handbook[x][y].wumpus=0;
}
handbook[x][y].in=blank;
}
else
{
if(!currentgrid.breeze&&!currentgrid.stench&&!currentgrid.glitter)
{
cout<<"英雄什么都没感觉到,附近有可能是安全的!"<<endl;
HeroInfo+="英雄什么都没感觉到,附近有可能是安全的!\r\n";
}
else
{
if(currentgrid.breeze)
{
cout<<"英雄听到了阵阵风声,附近应该有陷阱!"<<endl;
HeroInfo+="英雄听到了阵阵风声,附近应该有陷阱!\r\n";
}
if(currentgrid.stench)
{
if(wumpusalive)
{
cout<<"英雄闻到了臭味,附近应该有巫魔..."<<endl;
HeroInfo+="英雄闻到了臭味,附近应该有巫魔...\r\n";
}
else
{
cout<<"英雄闻到了射死了的巫魔的臭味,没什么可怕的了!"<<endl;
HeroInfo+="英雄闻到了射死了的巫魔的臭味,没什么可怕的了!\r\n";
}
}
if(currentgrid.glitter)
{
cout<<"英雄看到了闪光,该格子里有金子!"<<endl;
HeroInfo+="英雄看到了闪光,该格子里有金子!\r\n";
grab();
}
}
handbook[x][y].in=blank;
}
if(!gotgold)
{
choosedirA();
}
else choosedirB();
}
void wumpusproblem::game()
{
cout<<"生成随机地图,请等待..."<<endl;
HeroInfo+="生成随机地图,请等待...\r\n";
Map map;
print(map);
printb(map);
prints(map);
hero.map=map;
print(hero.map);
printb(hero.map);
wait();
while(hero.alive&&!hero.success)
{
wait();
hero.feel();
printu(hero.handbook);
print(hero.handbook);
if((hero.knownwupos&&hero.wumpusalive&&hero.arrow)||(hero.arrow&&hero.noway)||(hero.arrow&&hero.stepcount>=16&&hero.wumpusalive))
hero.shoot();
hero.handbook[hero.x][hero.y].in=blank;
if(hero.alive&&!hero.success)
{
hero.stepforward();
if(hero.map.maze[hero.x][hero.y].sthin==blank)
hero.map.maze[hero.x][hero.y].sthin=him;
print(hero.map);
}
}
}
void print(Map map)
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(map.maze[i][j].sthin==wumpus)
cout<<"W ";
if(map.maze[i][j].sthin==gold)
cout<<"G ";
if(map.maze[i][j].sthin==pit)
cout<<"P ";
if(map.maze[i][j].sthin==deadwumpus)
cout<<"D ";
if(map.maze[i][j].sthin==blank)
cout<<"□ ";
if(map.maze[i][j].sthin==him)
cout<<"H ";
if(j==3)
cout<<endl;
}
cout<<endl;
}
void print(guide handbook[4][4])
{
cout<<"英雄的手册情况如下(各地点的危险程度):"<<endl;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
cout<<handbook[i][j].pit+handbook[i][j].wumpus<<" ";
if(j==3)
cout<<endl;
}
}
void printb(Map map)
{
cout<<"地图里有风的洞穴:"<<endl;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(map.maze[i][j].breeze)
cout<<"B ";
else cout<<"□ ";
if(j==3)
cout<<endl;
}
cout<<endl;
}
void prints(Map map)
{
cout<<"地图里有臭味的洞穴:"<<endl;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(map.maze[i][j].stench)
cout<<"s ";
else cout<<"□ ";
if(j==3)
cout<<endl;
}
cout<<endl;
}
void printu(guide handbook[4][4])
{
cout<<"英雄的手册情况如下(各地点是否已经知道是什么东西):"<<endl;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(handbook[i][j].in==unknown)
cout<<"u ";
else if(handbook[i][j].in==blank)
cout<<"b ";
else cout<<"□ ";
if(j==3)
cout<<endl;
}
cout<<endl;
}
void wait()
{
int time=clock();
while(clock()<time+500);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -