Sometimes ChatGPT is nearly creepy in its level of understanding

TL;DR:  when helping to write code, ChatGPT is well aware of the meaning of the class you are writing, and will e.g. implement isTraversable() for a wall to return false, for a floor to return true, because it knows what walls are, what you do with walls in an RPG etc.

Today I wanted it to create simple classes for representing level entities like walls and floors.

So I first asked it to create a DrawingObject that pretty much only represents an image URL, and then a LevelObject that knows how to be drawn with a DrawingObject, and has a method "isTraversable()"

This is my prompt: note that I didn't say anything about the purpose of isTraversable()

okay, now I need a couple of JS classes. First a simple class "LevelObject" that has a method isTraversable() that throws a "need to be implemented by subclasses" error like the one you did before, a method getDrawingObject() that returns null

And it created the JS class. I then wanted it to change it slightly so I can pass a DrawingObject with this prompt (I kept it brief to check whether it still gets it): 

I guess it makes sense to add a member drawingObject and change the constructor and the getter to initialize this member and return it.

And I got what I wanted.

class LevelObject {
  constructor(drawingObject) {
    this.drawingObject = drawingObject;
  }

  isTraversable() {
    throw new Error("isTraversable() must be implemented by subclasses");
  }

  getDrawingObject() {
    return this.drawingObject;
  }
}

So far so good, this is all trivial and fully expected. But now comes the crazy stuff.

I want a floor class to subclass LevelObject, so I wrote this prompt:

now create a subclass Floor that extends DrawingObject, and that calls the super constructor by creating a new DrawingObject for 'img/floor/stones.png'.

Note that I messed up and confused LevelObject with DrawingObject. It got that right. Okay, a good AI should understand that. But the other thing it understood totally took me by surprise. Look at its reply (I've marked the mindboggling part in red).

It seems like you want to create a subclass Floor that extends LevelObject. Here's how you can do that:

 class Floor extends LevelObject {
  constructor() {
    super(new DrawingObject('img/floor/stones.png'));
  }

  isTraversable() {
    return true;
  }
}


This Floor subclass extends LevelObject and calls the super constructor with a new DrawingObject for 'img/floor/stones.png'. Additionally, I've implemented the isTraversable() method to return true for Floor objects, as floors are usually traversable in a game.

That.. is.. amazing.

For a check I asked it to create a wall object, again without mentioning anything about isTraversable. And it implemented it to always return false and said:

I've also implemented the isTraversable() method to return false for Wall objects, as walls are typically not traversable in a game.

Consider me stunned.


Kommentare

Beliebte Posts aus diesem Blog

ChatGPT as a teacher

How it started...

Pathfinding and A*