基本规则的介绍disi

Wumpus世界是一个简单的世界示例,用于说明基于知识的代理的价值并表示知识表示。它的灵感来自于1973年格里高里·尤布(Gregory Yob)的电子游戏《猎杀大亨》

Wumpus世界是一个山洞,有4/4个房间,这些房间与通道相连。因此,共有16个房间相互连接。我们有一个以知识为基础的代理商,他将在这个世界上前进。这个山洞里有一间屋子,里面有个叫巫普斯(Wumpus)的野兽,他会吃掉进屋的任何人。代理人可以射杀乌鸦,但代理人只有一个箭头。在Wumpus世界中,有一些无底洞室,如果特工落在深坑中,那么他将永远被困在那里。这个洞穴令人兴奋的是,在一个房间里有可能找到一大堆金子。因此,探员的目标是找到金子并爬出洞穴,而不会掉入坑或被Wumpus吞噬。如果特工用金子出来,他会得到奖励;如果被金鸡吞下或掉进坑里,他会受到惩罚。

注意:这里的Wumpus是静态的,不能移动。
以下是代表Wumpus世界的示例图。它显示了在世界(1,1)正方形位置上的一些带有坑的房间,带有Wumpus的一个房间和一个代理商。

与Wumpus房间相邻的房间很臭,因此会有些恶臭。
与PIT相邻的房间微风轻拂,因此,如果业务代表到达PIT附近,则他会感觉到微风。
当且仅当房间有金色时,房间才会闪闪发光。
如果特工面对它,则该特工可能会杀死该特工,并且特工会发出可怕的尖叫声,在山洞的任何地方都可以听到。

性能指标:
如果特工带着金从洞穴中出来,则可获得1000点奖励积分。
被Wumpus吃掉或掉进坑里的点数为-1000分。
-1表示每个操作,-10表示使用箭头。
如果特工死亡或从山洞出来,游戏就会结束。
环境:
4 * 4的房间网格。
该代理最初位于房间正方形[1,1]中,朝向右侧。
除了第一个正方形[1, 1]以外,都是随机选择Wumpus和黄金的位置。
洞穴的每个正方形都可以是第一个正方形以外的概率为0.2的坑。
执行器:

左转,
右转
前进

发布
射击。
传感器:

如果代理商在Wumpus附近的房间里,他会感觉到恶臭。 (不是对角线的)。
如果特工在紧邻坑的房间内,他会感觉到微风。
代理会感知到存在金的房间中的闪光。
特工走进墙壁会感觉到撞击。
射杀Wumpus时,它会发出可怕的尖叫声,在山洞的任何地方都可以感觉到。

ps,这里我设置的出山洞的指标是在坐标(1,1)向西移动。暂时的设定是不捡到金子不能出山洞,所以某些情况是必死的qaq
然后我附上我接近500行的代码

#include<bits/stdc++.h>
#include<iostream>
#include<time.h>
#include<cstdlib> 
#include<string>
#define random(x) (rand()%x)

using namespace std;

void getrand() { //获取随机数种子并去掉种子开头按时间规律的几位数 
    srand((int)time(0));
    int randomrefrash=random(100000);
}

int m=4,toward[5][2]={{0,0},{0,1},{1,0},{0,-1},{-1,0}},flag=0,tmp2;

class Box {
    public:
        string display="*";
        int Wumpus;
        int Pit;
        int Gold;
        int Wind;
        int Stench;
        int mayWumpus=0;
        int mayPit=0;
        int safe=0;
        int footprint=0;
}box[10][10],AIbox[10][10];



class Agent{
    public:
    int score=0;
    int x=1;
    int y=1;
    int face=1;
    int life=1; 
    int bullet=1;
    int foot=0;
    
    void turn_left(){
        if(this->face==4) this->face=1;
        else this->face++;
        this->score--;
    }
    void turn_right(){
        if(this->face==1) this->face=4;
        else this->face--;
        this->score--;
    }
    void advance() {
        if(this->x+toward[this->face][0]>m || this->y+toward[this->face][1]>m) {
            cout<<"\t\t前方是一面墙壁,无法再往前了"<<endl;
            return; 
        }
        if((this->x+toward[this->face][0]<=0 || this->y+toward[this->face][1]<=0) && this->y+toward[this->face][1]!=0) {
            cout<<"\t\t前方是一面墙壁,无法再往前了"<<endl;
            return;
        }
        if(this->y+toward[this->face][1]==0 && this->x+toward[this->face][0]!=1) {
            cout<<"\t\t前方是一面墙壁,无法再往前了"<<endl;
            return;
        }
        if(this->y+toward[this->face][1]==0 && this->x+toward[this->face][0]==1 && this->life!=2) {
            cout<<"\t\t还没有拿到金子,我不能出去"<<endl;
            return;
        }
        this->x+=toward[this->face][0];
        this->y+=toward[this->face][1]; 
    } 
    void feeling() {
        
        if(box[this->x][this->y].Pit==1) {
            cout<<"\t\t你一脚踏空,摔进陷阱里没有了气息"<<endl;
            this->life=0;
            this->score=0;
            return ;
        }
        if(box[this->x][this->y].Wumpus==1) {
            cout<<"\t\t你走进房间,一双血红的眼睛凝视着你,随后,你眼前一黑"<<endl;
            this->life=0;
            this->score=0;
            return ;
        }    
        if(this->face==1) cout<<"\t\t你细细回想,你现在应该面朝东方"<<endl;
        if(this->face==2) cout<<"\t\t你细细回想,你现在应该面朝北方"<<endl;
        if(this->face==3) cout<<"\t\t你细细回想,你现在应该面朝西方"<<endl;
        if(this->face==4) cout<<"\t\t你细细回想,你现在应该面朝南方"<<endl;
        
        if(box[this->x][this->y].Stench==1)
            cout<<"\t\t这里散发着一股恶臭的味道"<<endl;
        if(box[this->x][this->y].Wind==1)
            cout<<"\t\t这里有风"<<endl;    
        if(box[this->x][this->y].Gold==1)
            cout<<"\t\t这里亮起了金光!是金子!"<<endl;
    } 
    void catchgold() {
        if(box[this->x][this->y].Gold==1) {
            score+=1000;
            this->life=2;
            box[this->x][this->y].Gold=0;
            cout<<"\t\t你拾起了金子"<<endl;    
        }
        else if(box[this->x][this->y].Gold==0) {
            score-=1;
            cout<<"\t\t这里没有金子"<<endl;    
        }
    }
    
    void shoot() {
        if(this->bullet==1) {
            this->bullet=0;
            while(1) {
                int bulletx=this->x;
                int bullety=this->y;
                bulletx+=toward[this->face][0];
                bullety+=toward[this->face][1];    
                if(box[bulletx][bullety].Wumpus==1){
                    cout<<"\t\t一阵惨叫声传来,你知道怪物已经死了"<<endl;
                    box[bulletx][bullety].Wumpus=0;
                    for(int i=1;i<=m;i++) {
                        for(int j=1;j<=m;j++) {
                            box[i][j].Stench=0;
                        }
                    }
                    return; 
                }
                if(bulletx>m||bullety>m||bulletx<=0||bullety<=0) return ; 
            }
             
        }
        else if(this->bullet==0) {
            cout<<"\t\t你已经没有子弹了"<<endl;
        }
    } 
}agent;


void Initialize(int m) {//初始化地图函数 
    int tmp1,tmp2;
    for(int i=1;i<=m;i++) {//初始化陷阱Pit 
        for(int j=1;j<=m;j++) {
            if(i==1 && j==1) continue;
            tmp1=random(5);
            if(tmp1==0) {
                box[i][j].Pit=1;
            }
        }
    }
    while(1){//初始化怪兽Wumpus 
        tmp1=random(m)+1;
        tmp2=random(m)+1;
        if(tmp1==1 && tmp2==1) continue;
        box[tmp1][tmp2].Wumpus=1;
        break;
    }    
    while(1){//初始化黄金Gold 
        tmp1=random(m)+1;
        tmp2=random(m)+1;
        if(box[tmp1][tmp2].Pit==1) continue;
        box[tmp1][tmp2].Gold=1;
        break; 
    }    
    
    for(int i=1;i<=m;i++) {//初始化显示 
        for(int j=1;j<=m;j++) {
            if(box[i][j].Pit==1)
                box[i][j].display="P";
            else if(box[i][j].Wumpus==1)
                box[i][j].display="W";
            else if(box[i][j].Gold==1)
                box[i][j].display="G";
            if(box[i][j].Wumpus==1 && box[i][j].Pit==1)
                box[i][j].display="WP";
            if(box[i][j].Wumpus==1 && box[i][j].Gold==1)
                box[i][j].display="WG";    
        }
    }
    for(int i=1;i<=m;i++) {//初始化风Wind与恶臭Stench 
        for(int j=1;j<=m;j++) {
            if(box[i+1][j].Wumpus==1 ||box[i-1][j].Wumpus==1||box[i][j-1].Wumpus==1||box[i][j+1].Wumpus==1) 
                box[i][j].Stench=1;
            if(box[i+1][j].Pit==1 ||box[i-1][j].Pit==1||box[i][j-1].Pit==1||box[i][j+1].Pit==1) 
                box[i][j].Wind=1;
        }
    }
    for(int i=0;i<=m+1;i++) {
        AIbox[0][i].display="";
        AIbox[m+1][i].display="";
        AIbox[i][0].display="";
        AIbox[i][m+1].display="";
    }
}

void DisplayBox() {//展示地图 
    for(int i=m;i>=1;i--) {
        cout<<"\t\t";
        for(int j=1;j<=m;j++) 
            cout<<box[i][j].display<<"\t";
        cout<<endl<<endl<<endl;    
    }
}

void DisplayAIBox() {//展示地图 
    cout<<endl;
    for(int i=m;i>=1;i--) {
        cout<<"\t\t";
        for(int j=1;j<=m;j++) 
            cout<<AIbox[i][j].safe<<AIbox[i][j].footprint<<"\t";
        cout<<endl<<endl<<endl;
    }
}

void AIdecide(int *ope,int *flag,int *tmp2) {
    if(AIbox[agent.x+toward[agent.face][0]][agent.y+toward[agent.face][1]].Wumpus==1) {
        *ope=5;
        AIbox[agent.x+toward[agent.face][0]][agent.y+toward[agent.face][1]].Wumpus=0;
        *tmp2=0;
        *flag=0;
        return ; 
    }
    if(box[agent.x][agent.y].Gold==1) {//如果本格有金子,抓取 
        *ope=4;
        for(int i=1;i<=agent.x;i++) {
            for(int j=1;j<=agent.y;j++) {
                if(AIbox[i][j].footprint>=1)
                AIbox[i][j].safe=(m*2-i-j)*500;
            }
        } 
        return;
    }
    if(*flag==1) {
        if(*tmp2 != 0 && *tmp2-agent.face!=0) {
            *tmp2=0;
            return ;
        }
        *flag=0;
        *ope=3;
        return ;
    }
    //读入推理地图
    AIbox[agent.x][agent.y].Wind=box[agent.x][agent.y].Wind;
    AIbox[agent.x][agent.y].Stench=box[agent.x][agent.y].Stench;
    

    AIbox[agent.x][agent.y].footprint+=1;
    AIbox[agent.x][agent.y].safe=3;
    //进行推理
    if(agent.life!=2) {
        
    
        for(int i=1;i<=m;i++) {
            for(int j=1;j<=m;j++) {
                if(AIbox[i][j].Wind==1) {
                    AIbox[i+1][j].mayPit=1;
                    AIbox[i-1][j].mayPit=1;
                    AIbox[i][j+1].mayPit=1;
                    AIbox[i][j-1].mayPit=1;
                }
                if(AIbox[i][j].Stench==1) {
                    AIbox[i+1][j].mayWumpus=1;
                    AIbox[i-1][j].mayWumpus=1;
                    AIbox[i][j+1].mayWumpus=1;
                    AIbox[i][j-1].mayWumpus=1;
                }
            }
        }
        //计算危险指数
        for(int i=1;i<=m;i++) {
            for(int j=1;j<=m;j++) {
                if(AIbox[i][j].footprint>=1) continue;
                int cnt=0;
                if(AIbox[i-1][j].Stench==1 && AIbox[i-1][j].display!="" ) cnt++;
                if(AIbox[i][j-1].Stench==1 && AIbox[i][j-1].display!="") cnt++;
                if(AIbox[i+1][j].Stench==1 && AIbox[i+1][j].display!="") cnt++;
                if(AIbox[i][j+1].Stench==1 && AIbox[i][j+1].display!="") cnt++;
                if(cnt>=2) AIbox[i][j].Wumpus=1;
                
                cnt=0;
                if(AIbox[i-1][j].Wind==1 && AIbox[i-1][j].display!="") cnt++;
                if(AIbox[i][j-1].Wind==1 && AIbox[i][j-1].display!="") cnt++;
                if(AIbox[i+1][j].Wind==1 && AIbox[i+1][j].display!="") cnt++;
                if(AIbox[i][j+1].Wind==1 && AIbox[i][j+1].display!="") cnt++;
                if(cnt>=2) AIbox[i][j].Pit=1;
            }
        }
        //DisplayAIBox();
        for(int i=1;i<=m;i++) {
            for(int j=1;j<=m;j++) {
                if(AIbox[i][j].Pit==1||AIbox[i][j].Wumpus==1)
                    AIbox[i][j].safe=-100;
                if(AIbox[i][j].Wind==0 && AIbox[i][j].Stench==0 && AIbox[i][j].footprint>0) {
                    AIbox[i+1][j].safe=3;
                    AIbox[i-1][j].safe=3;
                    AIbox[i][j+1].safe=3;
                    AIbox[i][j-1].safe=3;
                }
                    
            }
        }
    }
    DisplayAIBox();
    //确定行进目标
    int tmp=-1000,tmp1=0;
    int x,y;
    for(int i=1;i<=4;i++) {
        x=agent.x+toward[i][0],y=agent.y+toward[i][1];
        if(x<=0||x>=m+1||y<=0||y>=m+1) continue;
        if(tmp<AIbox[x][y].safe-AIbox[x][y].footprint) {
            tmp1=i;
            tmp=AIbox[x][y].safe-AIbox[x][y].footprint;
        }
        
        else if(tmp==AIbox[x][y].safe-AIbox[x][y].footprint ) {
            int ran=random(200);
            if(ran<100) {
                //cout<<endl<<"!!!!i:"<<i<<"   tmp:"<<tmp<<"  xy:"<<AIbox[x][y].safe-AIbox[x][y].footprint<<endl;
                tmp1=i;
                tmp=AIbox[x][y].safe-AIbox[x][y].footprint;
            }
        }
    }
    if(agent.x==1 && agent.y==1 && agent.life==2) tmp1=3;
    //cout<<endl<<"tmp1:"<<tmp1<<endl;
    if(tmp1==agent.face) {
        *ope=3;
        return ;
    }
    else {
        if((tmp1-agent.face>0 && tmp1-agent.face<=2 )|| (tmp1-agent.face>-4 && tmp1-agent.face<=-2)) {
            *ope=1;
            *flag=1;
            *tmp2=tmp1;
            return ;
        }
        else {
            *ope=2;
            *flag=1;
            *tmp2=tmp1;
            return ;
        }
    }
    /*if(box[agent.x][agent.y].Gold==1) {//如果本格有金子,抓取 
        *ope=4;
        return;
    }
    if(agent.x+toward[agent.face][0]==1 && agent.y+toward[agent.face][1]==0 && agent.life==2) {
        *ope=3;
        return;
    }
    if(box[agent.x][agent.y].safe==0) {//如果本格没来过,推测周围的情况 
        if(box[agent.x][agent.y].Wind==1) {
            box[agent.x+1][agent.y].mayPit+=1;
            box[agent.x-1][agent.y].mayPit+=1;
            box[agent.x][agent.y+1].mayPit+=1;
            box[agent.x][agent.y-1].mayPit+=1;
        }
        if(box[agent.x][agent.y].Stench==1) {
            box[agent.x+1][agent.y].mayWumpus+=1;
            box[agent.x-1][agent.y].mayWumpus+=1;
            box[agent.x][agent.y+1].mayWumpus+=1;
            box[agent.x][agent.y-1].mayWumpus+=1;
        }
        if(box[agent.x][agent.y].Stench==0) {
            box[agent.x+1][agent.y].mayWumpus-=10000;
            box[agent.x-1][agent.y].mayWumpus-=10000;
            box[agent.x][agent.y+1].mayWumpus-=10000;
            box[agent.x][agent.y-1].mayWumpus-=10000;
        }
        if(box[agent.x][agent.y].Wind==0) {
            box[agent.x+1][agent.y].mayPit-=10000;
            box[agent.x-1][agent.y].mayPit-=10000;
            box[agent.x][agent.y+1].mayPit-=10000;
            box[agent.x][agent.y-1].mayPit-=10000;
        }
    }
    box[agent.x][agent.y].safe=1;
    box[agent.x][agent.y].mayWumpus=-10000;
    box[agent.x][agent.y].mayPit=-10000;
    if(agent.x+toward[agent.face][0]>=1 && agent.y+toward[agent.face][1]>=1 && agent.x+toward[agent.face][0]<=4 &&agent.y+toward[agent.face][1]<=4) {
        
    
        if(box[agent.x+toward[agent.face][0]][agent.y+toward[agent.face][1]].mayPit<0) {
            if(box[agent.x+toward[agent.face][0]][agent.y+toward[agent.face][1]].mayWumpus<0) {
                *ope=3;//若前方没有陷阱也没有怪兽 
                return ;
            }
        }
    }    
    if(box[agent.x+toward[agent.face][0]][agent.y+toward[agent.face][1]].mayWumpus>=2) {
        *ope=5;
        box[agent.x+toward[agent.face][0]][agent.y+toward[agent.face][1]].mayWumpus-=10000;
        return ; 
    }
    if(box[agent.x+toward[agent.face][0]][agent.y+toward[agent.face][1]].safe==1) {
        *ope=3;
        return ;
    }
    *ope=random(2)+1;*/
}

int main() {
    getrand();
    Initialize(m);
    //DisplayBox();
    cout<<"\t\t欢迎来到Wumpus世界,请选择你的游戏模式"<<endl;
    int tmp;int ope;
    cout<<"\t\t1,开始游戏"<<endl<<"\t\t2,AI代理(测试版2.1)"<<endl; 
    cin>>tmp;
    while(1) {
        if(agent.x ==1 &&agent.y ==0) {
            DisplayBox();
            cout<<"\t\t你最终离开了洞穴,你的得分为:"<<agent.score<<endl;
            return 0;
        }
        agent.feeling();
        if(agent.life==0) {
            DisplayBox();
            cout<<"\t\t你的得分为:"<<agent.score<<endl;
            return 0;
        }
        cout<<"\t\t你推断出了你的坐标:"<<agent.x<<" "<<agent.y<<endl;
        
        cout<<"\t\t1,左转  2,右转  3,前进  4,拾取  5,射击"<<endl<<"\t\t";
        if(tmp==1) 
            cin>>ope;
        else if(tmp==2) {
            
            AIdecide(&ope,&flag,&tmp2);
            cout<<endl;
        }
        agent.foot++;
        if(ope==1) agent.turn_left();
        if(ope==2) agent.turn_right();
        if(ope==3) agent.advance();
        if(ope==4) agent.catchgold();
        if(ope==5) agent.shoot();
    }
    cout<<"\t\t你的得分为:"<<agent.score<<endl;
    DisplayBox();
    return 0;
} 

发表评论