Javascript 流行的';真空吸尘器&x27;使用简单反射剂解决玩具问题

Javascript 流行的';真空吸尘器&x27;使用简单反射剂解决玩具问题,javascript,artificial-intelligence,undefined,p5.js,agent,Javascript,Artificial Intelligence,Undefined,P5.js,Agent,我是理学学士的学生,正在学习“人工智能” 这是一个简单的Reflection agent程序,它正在“Python”上运行,但与我在p5.js(JavaScript)上尝试的制作UI相同 但是我犯了这个错误,谁能告诉我为什么这个.currentRoom没有得到这个.room1 或者你可以复制它并在在线编辑器上得到实际发生的事情 对不起,如果我问的方式不好,实际上这是我第一次在stackoverflow上问 function setup(){ createCanvas(600,400);

我是理学学士的学生,正在学习“人工智能”

这是一个简单的Reflection agent程序,它正在“Python”上运行,但与我在p5.js(JavaScript)上尝试的制作UI相同

但是我犯了这个错误,谁能告诉我为什么这个.currentRoom没有得到这个.room1

或者你可以复制它并在在线编辑器上得到实际发生的事情

对不起,如果我问的方式不好,实际上这是我第一次在stackoverflow上问

function setup(){
   createCanvas(600,400);
    vc = new VAgent();
    twoRooms = new VEnvironment(vc);
    twoRooms.executeStep(6);

}
function draw(){
    background(0);
}

class Room{
    constructor(location,status){
        this.location=location;
        this.status=status;
    }
    getAll(){
        console.log(this.location);
        console.log(this.status);
    }
}

class VEnvironment{
    contructor(agent){
        this.agent=agent;
        this.room1=new Room('a','Dirty');
        this.room2=new Room('b','Dirty');
        this.currentRoom=this.room1;
        this.actionStatus='';
        this.step=0;

    }

    executeStep(n){
        for(var i=0;i<n;i++){
            this.displayPerception();
            this.agent.sense(this);
            var res = this.agent.action();
            if(res=='clean'){
               this.currentRoom.status=='clean'
            }else if(res=='Right'){
               this.currentRoom=this.room2;
            }else{
                this.currentRoom=this.room1;
            }
            this.displayAction();
            this.step++;
        }
    }
    displayPerception(){
        console.log('Agent is Present At Room '+this.currentRoom.location+' And The Status For Room Is '+this.currentRoom.status);
    }
    displayAction(){
        console.log('Agent took at'+this.step+' And Action was ... '+this.currentRoom+'...');
    }
}


class VAgent{
    constructor(){

    }

    sense(currentEnv){
        this.en=currentEnv;
    }
    action(){
        if(this.en.currentRoom.status=='dirty'){
           return 'Clean'
        }else if(this.en.currentRoom.location=='a'){
           return 'Left'
        }else{
            return 'Right'
        }
    }
}

函数设置(){
createCanvas(600400);
vc=新的VAgent();
twoRooms=新的VenEnvironment(vc);
两个房间。行政人员(6);
}
函数绘图(){
背景(0);
}
教室{
建造商(位置、状态){
这个位置=位置;
这个状态=状态;
}
getAll(){
console.log(this.location);
console.log(this.status);
}
}
类环境{
承包商(代理人){
这个。代理=代理;
this.room1=新房间('a','Dirty');
this.room2=新房间('b','Dirty');
this.currentRoom=this.room1;
this.actionStatus='';
该步骤=0;
}
ExecuteTEP(n){

对于(var i=0;i当您有一段不理解的复杂代码时,您可以做的最好的事情是将问题缩小到一个简单的范围

例如,您可以将问题隔离到以下代码中:

function setup() {
  createCanvas(600, 400);

  const myContainer = new Container();
  myContainer.displayValue();
}

function draw() {
  background(0);
}

class Value {
  constructor() {
    this.x = 42;
  }
}

class Container {
  contructor() {
    this.value = new Value();
    this.currentvalue = this.value;
    console.log('constructor value: ' + this.currentValue);
  }

  displayValue() {
    console.log('display value: ' + this.value.x);
  }
}
此代码显示与您的代码相同的问题,没有任何与您的问题无关的额外代码

如果运行此代码,您会注意到,
构造函数值
print语句从未被触发。这是更仔细地查看构造函数的线索


你的问题是打字错误:你有
constructor
而不是
constructor

非常感谢你的关注。这真的很有帮助。我知道你想说什么,我会按照你的方式来做,你说的完全正确,它不会触发。实际上,它没有到达那里,在那之前它已经被调用了。什么我所做的就是,做一些我想达到的事情,必须执行(存在)。非常感谢。这真的很有帮助。