📄 wumpus.cpp
字号:
#include "stdafx.h"
#include "vwumpus.h"
#include "vwumpusDlg.h"
#include "wumpus.h"
#include <ctime>
#include <iostream>
using namespace std;
extern CString HeroInfo;
////////////////////////////////////////////////////////////////////////////////////////////////
//Map definition
////////////////////////////////////////////////////////////////////////////////////////////////
Map::Map()
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
maze[i][j].sthin=blank;
maze[i][j].breeze=false;
maze[i][j].stench=false;
maze[i][j].glitter=false;
}
}
maze[3][0].sthin=him;//英雄的位置
locatewumpus();
locategold();
locatepit1();
locatepit2();
locatepit3();
}
Map::~Map()
{
}
void Map::locatewumpus()
{
srand((unsigned)time(NULL));
int x=rand()%4;
int y=rand()%4;
while((x==3&&y==0)||(x==2&&y==0)||(x==3&&y==1))
{
srand((unsigned)time(NULL));
x=rand()%4;
y=rand()%4;
}//while
maze[x][y].sthin=wumpus;
wumpos.x=x;
wumpos.y=y;
if(x>0)maze[x-1][y].stench=true;
if(x<3)maze[x+1][y].stench=true;
if(y>0)maze[x][y-1].stench=true;
if(y<3)maze[x][y+1].stench=true;
}
void Map::locategold()
{
srand((unsigned)time(NULL));
int x=rand()%4;
int y=rand()%4;
while((x==3&&y==0)||(x==2&&y==0)||(x==3&&y==1)||(x==wumpos.x&&y==wumpos.y))
{
srand((unsigned)time(NULL));
x=rand()%4;
y=rand()%4;
}
goldpos.x=x;
goldpos.y=y;
maze[x][y].sthin=gold;
maze[x][y].glitter=true;
}
void Map::locatepit1()
{
srand((unsigned)time(NULL));
int x=rand()%4;
int y=rand()%4;
while((x==3&&y==0)||(x==2&&y==0)||(x==3&&y==1)||(x==wumpos.x&&y==wumpos.y)||
(x==goldpos.x&&y==goldpos.y))
{
srand((unsigned)time(NULL));
x=rand()%4;
y=rand()%4;
}
pitpos1.x=x;
pitpos1.y=y;
maze[x][y].sthin=pit;
if(x>0)maze[x-1][y].breeze=true;
if(x<3)maze[x+1][y].breeze=true;
if(y>0)maze[x][y-1].breeze=true;
if(y<3)maze[x][y+1].breeze=true;
}
void Map::locatepit2()
{
srand((unsigned)time(NULL));
int x=rand()%4;
int y=rand()%4;
while((x==3&&y==0)||(x==2&&y==0)||(x==3&&y==1)||(x==wumpos.x&&y==wumpos.y)||
(x==goldpos.x&&y==goldpos.y)||(x==pitpos1.x&&y==pitpos1.y))
{
srand((unsigned)time(NULL));
x=rand()%4;
y=rand()%4;
}
pitpos2.x=x;
pitpos2.y=y;
maze[x][y].sthin=pit;
if(x>0)maze[x-1][y].breeze=true;
if(x<3)maze[x+1][y].breeze=true;
if(y>0)maze[x][y-1].breeze=true;
if(y<3)maze[x][y+1].breeze=true;
}
void Map::locatepit3()
{
srand((unsigned)time(NULL));
int x=rand()%4;
int y=rand()%4;
while((x==3&&y==0)||(x==2&&y==0)||(x==3&&y==1)||(x==wumpos.x&&y==wumpos.y)||
(x==goldpos.x&&y==goldpos.y)||(x==pitpos1.x&&y==pitpos1.y)||
(x==pitpos2.x&&y==pitpos2.y))
{
srand((unsigned)time(NULL));
x=rand()%4;
y=rand()%4;
}
pitpos3.x=x;
pitpos3.y=y;
maze[x][y].sthin=pit;
if(x>0)maze[x-1][y].breeze=true;
if(x<3)maze[x+1][y].breeze=true;
if(y>0)maze[x][y-1].breeze=true;
if(y<3)maze[x][y+1].breeze=true;
}
grid Map::gridstate(int x, int y)
{
return maze[x][y];
}
////////////////////////////////////////////////////////////////////////////////////////////////
//Hero definition
////////////////////////////////////////////////////////////////////////////////////////////////
Hero::Hero()
{
alive=true;
arrow=true;
currentdirection=N;
knownwupos=false;
gotgold=false;
noway=false;
wumpusalive=true;
success=false;
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;
}
}
}
Hero::~Hero()
{
}
bool Hero::stepforward()
{
score-=1;
stepcount++;
if(currentdirection==N)
{
cout<<"英雄决定往北走一步!"<<endl;
HeroInfo+="英雄决定往北走一步!\r\n";
if(x==0)
{
cout<<"英雄碰到墙壁了,头顶起了一个大包!"<<endl;
HeroInfo+="英雄碰到墙壁了,头顶起了一个大包!\r\n";
makedecision();
return true;
}
if(map.maze[x][y].sthin==him)
map.maze[x][y].sthin=blank;
x--;
if(map.maze[x][y].sthin==blank)
map.maze[x][y].sthin=him;
return true;
}
if(currentdirection==S)
{
cout<<"英雄决定往南走一步!"<<endl;
HeroInfo+="英雄决定往南走一步!\r\n";
if(x==3)
{
cout<<"英雄碰到墙壁了,头顶起了一个大包!"<<endl;
HeroInfo+="英雄碰到墙壁了,头顶起了一个大包!\r\n";
makedecision();
return true;
}
if(map.maze[x][y].sthin==him)
map.maze[x][y].sthin=blank;
x++;
if(map.maze[x][y].sthin==blank)
map.maze[x][y].sthin=him;
return true;
}
if(currentdirection==W)
{
cout<<"英雄决定往西走一步!"<<endl;
HeroInfo+="英雄决定往西走一步!\r\n";
if(y==0)
{
cout<<"英雄碰到墙壁了,头顶起了一个大包!"<<endl;
HeroInfo+="英雄碰到墙壁了,头顶起了一个大包!\r\n";
makedecision();
return true;
}
if(map.maze[x][y].sthin==him)
map.maze[x][y].sthin=blank;
y--;
if(map.maze[x][y].sthin==blank)
map.maze[x][y].sthin=him;
return true;
}
if(currentdirection==E)
{
cout<<"英雄决定往东走一步!"<<endl;
HeroInfo+="英雄决定往东走一步!\r\n";
if(y==3)
{
cout<<"英雄碰到墙壁了,头顶起了一个大包!"<<endl;
HeroInfo+="英雄碰到墙壁了,头顶起了一个大包!\r\n";
makedecision();
return false;
}
if(map.maze[x][y].sthin==him)
map.maze[x][y].sthin=blank;
y++;
if(map.maze[x][y].sthin==blank)
map.maze[x][y].sthin=him;
return true;
}
return false;
}
void Hero::turnback()
{
if(currentdirection==N)
currentdirection=S;
else if(currentdirection==S)
currentdirection=N;
else if(currentdirection==W)
currentdirection=E;
else currentdirection=W;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(handbook[i][j].pit)handbook[i][j].pit--;
if(handbook[i][j].wumpus)handbook[i][j].wumpus--;
}
}
bool Hero::grab()
{
if((map.gridstate(x,y).sthin==gold)&&(map.gridstate(x,y).glitter==true))
{
score-=1;
cout<<"英雄捡起了金子!"<<endl;
HeroInfo+="英雄捡起了金子!\r\n";
gotgold=true;
firstgetgold=true;
handbook[x][y].in=blank;
map.maze[x][y].sthin=him;
map.maze[x][y].glitter=false;
return true;
}
else
{
score-=1;
cout<<"英雄判断失误,在一个没有金子的格子里试图捡到金子!"<<endl;
HeroInfo+="英雄判断失误,在一个没有金子的格子里试图捡到金子!\r\n";
return false;
}
}
bool Hero::climb()
{
score-=1;
if(x==3&&y==0)
{
cout<<"英雄爬出洞!"<<endl;
HeroInfo+="英雄爬出洞!\r\n";
score+=1000;
success=true;
return true;
}
return false;
}
void Hero::makedecision()
{
if(x==0&&y>0&&y<3)
{
int evaluationN,evaluationS,evaluationW,evaluationE,evaluation;
evaluationS=3*handbook[x+1][y].pit+3*handbook[x+1][y].wumpus+handbook[x+1][y].visittime;
evaluationW=3*handbook[x][y-1].pit+3*handbook[x][y-1].wumpus+handbook[x][y-1].visittime;
evaluationE=3*handbook[x][y+1].pit+3*handbook[x][y+1].wumpus+handbook[x][y+1].visittime;
evaluation=evaluationS;
if(evaluation<evaluationW)evaluation=evaluationW;
if(evaluation<evaluationE)evaluation=evaluationE;
if(evaluation==evaluationS) currentdirection=S;
else if(evaluation==evaluationW) currentdirection=W;
else if(evaluation==evaluationE) currentdirection=E;
}
if(x==0&&y==0)
{
int evaluationN,evaluationS,evaluationW,evaluationE,evaluation;
evaluationS=3*handbook[x+1][y].pit+3*handbook[x+1][y].wumpus+handbook[x+1][y].visittime;
evaluationE=3*handbook[x][y+1].pit+3*handbook[x][y+1].wumpus+handbook[x][y+1].visittime;
evaluation=evaluationS;
if(evaluation<evaluationE)evaluation=evaluationE;
if(evaluation==evaluationS) currentdirection=S;
else if(evaluation==evaluationE) currentdirection=E;
}
if(x==0&&y==3)
{
int evaluationN,evaluationS,evaluationW,evaluationE,evaluation;
evaluationS=3*handbook[x+1][y].pit+3*handbook[x+1][y].wumpus+handbook[x+1][y].visittime;
evaluationW=3*handbook[x][y-1].pit+3*handbook[x][y-1].wumpus+handbook[x][y-1].visittime;
// evaluationE=3*handbook[x][y+1].pit+3*handbook[x][y+1].wumpus+handbook[x][y+1].visittime;
// evaluationN=3*handbook[x-1][y].pit+3*handbook[x-1][y].wumpus+handbook[x-1][y].visittime;
evaluation=evaluationS;
if(evaluation<evaluationW)evaluation=evaluationW;
// if(evaluation<evaluationE)evaluation=evaluationE;
// if(evaluation<evaluationN)evaluation=evaluationN;
if(evaluation==evaluationS)currentdirection=S;
// else if(evaluation==evaluationN)currentdirection=N;
else if(evaluation==evaluationW)currentdirection=W;
// else if(evaluation==evaluationE)currentdirection=E;
}
if(x==3&&y>0&&y<3)
{
int evaluationN,evaluationS,evaluationW,evaluationE,evaluation;
// evaluationS=3*handbook[x+1][y].pit+3*handbook[x+1][y].wumpus+handbook[x+1][y].visittime;
evaluationW=3*handbook[x][y-1].pit+3*handbook[x][y-1].wumpus+handbook[x][y-1].visittime;
evaluationE=3*handbook[x][y+1].pit+3*handbook[x][y+1].wumpus+handbook[x][y+1].visittime;
evaluationN=3*handbook[x-1][y].pit+3*handbook[x-1][y].wumpus+handbook[x-1][y].visittime;
evaluation=evaluationN;
if(evaluation<evaluationW)evaluation=evaluationW;
if(evaluation<evaluationE)evaluation=evaluationE;
// if(evaluation<evaluationN)evaluation=evaluationN;
if(evaluation==evaluationN)currentdirection=N;
// else if(evaluation==evaluationN)currentdirection=N;
else if(evaluation==evaluationW)currentdirection=W;
else if(evaluation==evaluationE)currentdirection=E;
}
if(x==3&&y==0)
{
int evaluationN,evaluationS,evaluationW,evaluationE,evaluation;
// evaluationS=3*handbook[x+1][y].pit+3*handbook[x+1][y].wumpus+handbook[x+1][y].visittime;
// evaluationW=3*handbook[x][y-1].pit+3*handbook[x][y-1].wumpus+handbook[x][y-1].visittime;
evaluationE=3*handbook[x][y+1].pit+3*handbook[x][y+1].wumpus+handbook[x][y+1].visittime;
evaluationN=3*handbook[x-1][y].pit+3*handbook[x-1][y].wumpus+handbook[x-1][y].visittime;
evaluation=evaluationE;
// if(evaluation<evaluationW)evaluation=evaluationW;
if(evaluation<evaluationE)evaluation=evaluationE;
// if(evaluation<evaluationN)evaluation=evaluationN;
if(evaluation==evaluationN)currentdirection=N;
// else if(evaluation==evaluationN)currentdirection=N;
// else if(evaluation==evaluationW)currentdirection=W;
else if(evaluation==evaluationE)currentdirection=E;
}
if(x==3&&y==3)
{
int evaluationN,evaluationS,evaluationW,evaluationE,evaluation;
// evaluationS=3*handbook[x+1][y].pit+3*handbook[x+1][y].wumpus+handbook[x+1][y].visittime;
evaluationW=3*handbook[x][y-1].pit+3*handbook[x][y-1].wumpus+handbook[x][y-1].visittime;
// evaluationE=3*handbook[x][y+1].pit+3*handbook[x][y+1].wumpus+handbook[x][y+1].visittime;
evaluationN=3*handbook[x-1][y].pit+3*handbook[x-1][y].wumpus+handbook[x-1][y].visittime;
evaluation=evaluationN;
if(evaluation<evaluationW)evaluation=evaluationW;
// if(evaluation<evaluationE)evaluation=evaluationE;
// if(evaluation<evaluationN)evaluation=evaluationN;
if(evaluation==evaluationN)currentdirection=N;
// else if(evaluation==evaluationN)currentdirection=N;
else if(evaluation==evaluationW)currentdirection=W;
// else if(evaluation==evaluationE)currentdirection=E;
}
if(y==0&&x>0&&x<3)
{
int evaluationN,evaluationS,evaluationW,evaluationE,evaluation;
evaluationS=3*handbook[x+1][y].pit+3*handbook[x+1][y].wumpus+handbook[x+1][y].visittime;
// evaluationW=3*handbook[x][y-1].pit+3*handbook[x][y-1].wumpus+handbook[x][y-1].visittime;
evaluationE=3*handbook[x][y+1].pit+3*handbook[x][y+1].wumpus+handbook[x][y+1].visittime;
evaluationN=3*handbook[x-1][y].pit+3*handbook[x-1][y].wumpus+handbook[x-1][y].visittime;
evaluation=evaluationS;
// if(evaluation<evaluationW)evaluation=evaluationW;
if(evaluation<evaluationE)evaluation=evaluationE;
if(evaluation<evaluationN)evaluation=evaluationN;
if(evaluation==evaluationS)currentdirection=S;
else if(evaluation==evaluationN)currentdirection=N;
// else if(evaluation==evaluationW)currentdirection=W;
else if(evaluation==evaluationE)currentdirection=E;
}
if(y==3&&x>0&&x<3)
{
int evaluationN,evaluationS,evaluationW,evaluationE,evaluation;
evaluationS=3*handbook[x+1][y].pit+3*handbook[x+1][y].wumpus+handbook[x+1][y].visittime;
evaluationW=3*handbook[x][y-1].pit+3*handbook[x][y-1].wumpus+handbook[x][y-1].visittime;
// evaluationE=3*handbook[x][y+1].pit+3*handbook[x][y+1].wumpus+handbook[x][y+1].visittime;
evaluationN=3*handbook[x-1][y].pit+3*handbook[x-1][y].wumpus+handbook[x-1][y].visittime;
evaluation=evaluationS;
if(evaluation<evaluationW)evaluation=evaluationW;
// if(evaluation<evaluationE)evaluation=evaluationE;
if(evaluation<evaluationN)evaluation=evaluationN;
if(evaluation==evaluationS)currentdirection=S;
else if(evaluation==evaluationN)currentdirection=N;
else if(evaluation==evaluationW)currentdirection=W;
// else if(evaluation==evaluationE)currentdirection=E;
}
}
bool Hero::shoot()
{
if(arrow)
{
score-=1;
arrow=false;
cout<<"英雄射出了他仅有的一只箭!"<<endl;
HeroInfo+="英雄射出了他仅有的一只箭!\r\n";
if((wumpos.x==map.wumpos.x&&wumpos.y==map.wumpos.y)||
((wumpos.x==map.wumpos.x&&map.wumpos.x==x)&&((wumpos.y<y&&map.wumpos.y<y)||(wumpos.y>y&&map.wumpos.y>y)))||
((wumpos.y==map.wumpos.y&&map.wumpos.x==y)&&((wumpos.x<x&&map.wumpos.x<x)||(wumpos.x>x&&map.wumpos.x>x))))
{
cout<<"只听见巫魔一声惨叫,巫魔死掉了!"<<endl;
HeroInfo+="只听见巫魔一声惨叫,巫魔死掉了!\r\n";
map.maze[map.wumpos.x][map.wumpos.y].sthin=deadwumpus;
wumpusalive=false;
firstshootwumpus=true;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(handbook[i][j].in==wumpus)
handbook[i][j].in=deadwumpus;
handbook[i][j].wumpus=0;
}
handbook[wumpos.x][wumpos.y].in=blank;
if(!gotgold)
choosedirA();
else choosedirB();
return true;
}
cout<<"半天没听天声音,可惜没射中!"<<endl;
HeroInfo+="半天没听天声音,可惜没射中!\r\n";
return true;
}
return false;
}
void Hero::choosedirA()
{
if(x>0&&handbook[x-1][y].in==blank&&handbook[x-1][y].visittime==0)
{
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==0)
{
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==0)
{
int step=abs(S-currentdirection);
if(step>2){step=step%2;}
score-=step;
currentdirection=S;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -