innovative solution for the 'vacuumCleaner' toy problem with efficient reflex agent technology

Currently, I am pursuing a BSCS degree and my focus of study is 'Artificial Intelligence'.

I have developed a simple reflex agent program that operates in 'Python', but I also attempted to replicate it using p5.js (JavaScript) to create a user interface.

However, I encountered an error. Can someone please explain why 'this.currentRoom' is not being assigned as 'this.room1'?

I have attached a screenshot of the error for reference

Alternatively, you can copy the code and paste it into an online editor to better understand the issue.

I apologize if my request is unclear as this is my first time seeking assistance on 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'
        }
    }
}

Answer №1

When facing a complex piece of code that seems overwhelming, the key is to simplify the problem by creating a basic example program.

One way to do this is by isolating the issue in a smaller snippet of code:

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

This code replicates the issue you're experiencing without any irrelevant code complicating matters.

If you execute this code, you'll see that the print statement for constructor value never triggers, indicating a potential problem within the constructor function.

The mistake in your code is a simple typo: you have written contructor instead of constructor.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the accurate user agent for Windows Phone?

Can someone explain why PHP and JavaScript produce different user agents? Which one is considered the accurate user agent? PHP User Agent Output: <?php print_r($_SERVER['HTTP_USER_AGENT']); ?> User Agent: Mozilla/5.0 (Mobile; Windows Ph ...

manipulating an iframe on a separate domain

Currently, I am in the process of creating a mobile app using the Ionic framework for my website. Nevertheless, I believe that the issue at hand is not exclusive to Ionic. My goal within the app is to showcase a full-width, full-height iframe loading a sp ...

The jQuery inArray function fails to detect a match when the first element is the one being compared

I want to create an array and check if a specific value is present anywhere in the array. Depending on the presence of the value, I need the code to execute different actions. var Arr = [false, false, false, false, false]; // Works with this array: // Arr ...

Facing an issue where WordPress AJAX is not showing the expected content

Currently, I am in the process of creating a WordPress website that will feature an initial display of two rows of images. Upon clicking a button, two more rows should dynamically appear. There are four key files involved in this project: case-study-ca ...

"The concept of hoisting in JavaScript and its impact on global

Is there a way to set the variable "username_show" from this PouchDB document? I attempted using hoisting, but it seems that I may need to move the global variable outside of the data structure and then outside of the function. var db = new PouchDB ...

The SSE emitter sends out multiple signals, but occasionally the browser fails to receive them

When setting up an event emitter in a node.js/express application, I noticed that the events emitted are sometimes received multiple times by the front-end listener. Although I can confirm that emit is only called once, the same event gets emitted up to 4 ...

What is the process for transforming a block of plain text into a formatted text with multiple paragraphs while tracking the word count

I am faced with the task of transforming a simple text consisting of 5000 words into multiple paragraphs, each containing 1000 words. Is there a way to accomplish this? If so, I would greatly appreciate any guidance or assistance you can provide. ...

Avoid displaying hover effects on touch-enabled devices

Is there a way to prevent touch devices from triggering the CSS hover state without converting all hover styles into mouseover listeners? I am working on an application that needs to support both touch and pointer input. However, certain hover styles don& ...

Error: The function $(...) that is being called is not recognized as a dialog function

My website has been giving me headaches as I try to integrate a basic CMS. Despite three full days of work, I am still facing one persistent problem! I have exhausted all my known methods, researched some solutions, but none seem to work. In order to iden ...

Encountering a TypeError while utilizing a custom hook for an Apollo mutation operation

I'm facing a tough time debugging an issue with my Apollo mutation in my NextJS application. The error I'm encountering is as follows: "TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))" It appears t ...

Issues arise when attempting to retrieve JSON data through an Ajax GET call. Specifically, an "Undefined" error occurs for all records in the JSON object

Here is the client side code snippet I am working with: $.ajax({ url: 'http://localhost/App.WebAPI/api/Messages/AppName', type: 'GET', dataType: 'json', crossDom ...

Utilizing React Native to implement dynamic in-app badge notifications based on Firebase/Firestore data

My tech stack: React Native OneSignal for push notifications React Navigation React Native Elements (Badge component) React Native Vector Icons Firebase, Firestore as backend Within my app, I have two buttons located in the topRight Stack Screen. <Sta ...

What is the best way to create documentation for node.js APIs that already exist

Just started learning Node, and feeling overwhelmed by all the different libraries available for the same purpose. It's frustrating to see so many options but not making any progress. I have an existing Node.js + Express application and I am trying t ...

Javascript JSON is unable to retrieve the tag

Looking at this JSON structure: { "armament": { "air_armament": [ { "names": { "russian_name": "R-60", "NATO_name": "AA-8 Aphid" }, "weight": " ...

The ESLint rule "eqeqeq" configuration is deemed incorrect

After successfully running eslint with the provided .eslintrc file, I encountered an issue when making a simple change to use 'standard' instead of 'airbnb-base' as the extend: module.exports = { root: true, parser: 'babel-esl ...

I am receiving an undefined value when using document.getElementsByClassName

<canvas id="can" height="500px" width="1200px"></canvas> <div class="name"> <h1>LEONARDO</h1> </div> <script> var name=['WATSON','LEONARDO',"SMITH","EMILY"] var counter=0 var dat ...

Creating a structured model design in Javascript using Backbone framework

I am relatively new to JavaScript, so please bear with me as I ask this question. Currently, for my project, I am using NodeJS for the server and Backbone for the client. The idea is that the client will send a request to the server, which will then respo ...

Having trouble launching Cypress on my Mac - stating that it cannot find Cypress

Despite searching through multiple answers on S.O, none of them have solved my issue. To better explain my question, I've created a video. You can view it here Everything was working perfectly just yesterday, so what could have possibly gone wrong? ...

Tips for implementing validation in a multi-step form as outlined in w3schools tutorial

I came across a multistep form creation tutorial on w3schools. However, the tutorial only covers generic validation (checking if a field is empty), which is not sufficient for my needs. How can I implement HTML validation (e.g., min, max, length) for text ...

What is the proper way to implement SSE/EventSource in your project?

After watching tutorials online, it seems common practice for authors to use SSE/EventSource in setting up a connection where the server-side PHP script echoes data and then uses ob_flush() and flush() to send the information back to the client's brow ...