Overwhelming ChatGPT with a lengthy wish? Inventory management

TL;DR: Learned two things today: 1. when you confront ChatGPT with a too complicated request you just get a technical error. 2. ChatGPT is really good at explaining how the code works that it generates, incl. simple to understand code snippets.



Now this was underwhelming:


Me:

Assume I have a class Creature that represents a Creature (a monster, a NPC, a player) in a role playing game. This creature needs to have a bag in which it carries stuff. It also has the following limbs: head, arms, body, feet, and it has two hands. Also I have a class Tool that can be any tool that can be put into a bag, and from the bag moved to a limb or to a hand. A limb and a hand can only hold maximum 1 tool. If I move a tool from bag to limb or hand, and there is a tool there already, the former tool will automatically move to the bag. Can you create code for methods a) to put a tool in the bag b) to take a tool out of the bag and assign it to a limb. If the limb holds another tool, this is put into the bag. This method should call a method isToolFittingToLimb (to be implemented later) and only act if the method returns true. Some tools do not fit to some limbs. c) to remove a tool from a limb and put it into the bag d) to take a tool out of the bag and assign it to a hand. If the hand holds another tool, this tool will automatically put into the bag. A method isToolAWeapon (to be implemented later) checks whether this is possible before acting. e) remove a tool from a hand and put it into the bag f) remove a tool from the bag (drop it) can you show me the Javascript code for this?
 
ChatGPT:
 
Something went wrong. If this issue persists please contact us through our help center at help.openai.com.
 
Probably need to take baby steps with the AI...
 
But the baby steps then were pretty cool.
 
Me:
You are a Javascript expert developer, and together we create code for a role playing game. Assume there is a class Creature, and a class Tool. We want to add methods to creature that manage a bag that the creature has. You can add a tool to the bag, and you can remove a tool from the bag. Assume there is a method Tool.getWeight() that returns the weight of the tool. A bag can hold only tools that weigh at most MAX_WEIGHT = 100. If you want to add a Tool that would exceed the total weight cannot be added to the bag. Can you create the code for this?

ChatGPT returned this nice piece of code for this.
class Tool {
  constructor(weight) {
    this.weight = weight;
  }

  getWeight() {
    return this.weight;
  }
}

class Creature {
  constructor() {
    this.bag = [];
    this.MAX_WEIGHT = 100;
  }

  getCurrentWeight() {
    return this.bag.reduce((totalWeight, tool) => totalWeight + tool.getWeight(), 0);
  }

  canAddTool(tool) {
    return this.getCurrentWeight() + tool.getWeight() <= this.MAX_WEIGHT;
  }

  addTool(tool) {
    if (this.canAddTool(tool)) {
      this.bag.push(tool);
      console.log('Tool added to the bag.');
    } else {
      console.log('Cannot add tool to the bag. It would exceed the maximum weight.');
    }
  }

  removeTool(toolToRemove) {
    const index = this.bag.findIndex((tool) => tool === toolToRemove);

    if (index !== -1) {
      this.bag.splice(index, 1);
      console.log('Tool removed from the bag.');
    } else {
      console.log('Tool not found in the bag.');
    }
  }
}

const creature = new Creature();
const tool1 = new Tool(50);
const tool2 = new Tool(60);

creature.addTool(tool1); // Tool added to the bag.
creature.addTool(tool2); // Cannot add tool to the bag. It would exceed the maximum weight.
creature.removeTool(tool1); // Tool removed from the bag.
creature.addTool(tool2); // Tool added to the bag.



Now I must confess that I am really no JS guru, and was overwhelmed what in retrospective (hopefully unless the AI is completely hallucinating this :-) ) is standard array operations in JS. What is splice? What is that findIndex thing? Didn't get it. So I asked ChatGPT, and this is really where IMHO the magic of this AI lies: It can explain REALLY well, and you don't learn like reading a book but by understanding code in a real situation, where it solves a real problem. Here's our dialog:

Me:
Thank you, can you explain to me how exactly the bag member works? I don't understand what this is that I can do findIndex and push and such with it

ChatGPT (I'm using a screenshot to show how well formatted it replies)

That was really easy to understand.

 
 
 
 
 
 
 
 

Kommentare

Beliebte Posts aus diesem Blog

ChatGPT as a teacher

How it started...

Pathfinding and A*