使用简单反射剂的流行的'vaccumCleaner'玩具问题解决方案

问题描述 投票:2回答:1

[我是bscs的学生,正在学习'人工智能'。

[这是一个简单的反射代理程序,它正在'Python'上运行,但我在p5.js(JavaScript)上尝试制作UI的方法也与此相同。

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

I'm adding error screenshot over here

或者您可以将其复制并在在线编辑器上以获取实际情况。

抱歉,如果我问的方式不好,实际上是我第一次问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'
        }
    }
}

javascript artificial-intelligence undefined p5.js agent
1个回答
2
投票

[当您有一段不了解的复杂代码时,您所能做的最好的事情就是将问题缩小到simplified example program

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

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);
  }
}

此代码与您的代码存在相同的问题,没有与您的问题无关的任何额外代码。

如果运行此代码,您会注意到constructor value打印语句永远不会触发。这是更仔细地了解构造函数的线索。

您的问题是错字:您有contructor而不是constructor

© www.soinside.com 2019 - 2024. All rights reserved.