Converting (simple) stuff from Java to Javascript
Now ChatGPT and I are creating the class I am using for swords and armor and stuff. All these use three factors: an efficiency which is derived from the level of the tool, and a weight and a skill that are derived from the letter that the tool represents - different characters influence weight and skill differently. An 'h' weighs less than an 'x', obviously.
Now I got daring and created the class in 3 steps, each time feeding ChatGPT bits of the java class of my old app like:
private static float[] weightFactorsForLetters = new float[] {
// a, b, c, d, e, f, g, h
1f, 0.8f, 1.2f, 0.8012f, 1f, 1.1f, 1.2f, 0.6f,
// i, j, k, l, m, n, o, p, q,
1f, 1.1f, .9f, 0.75f, .8f, .8f, 1f, 0.9f, 1.1f,
// r, s, t, u, v, w, x, y, z
1.1f, 1.2f, 1.1f, 1f, .8f, 0.7f, 1.3f, .8f, 1.1f
};
or
protected void determineWeightAndSkill() {
String text = getWord().getText().toLowerCase();
int len = text.length();
skillFactor = 1.0f;
weight = getDefaultWeight();
for (int i=0; i<len; i++) {
int index = text.charAt(i)-'a';
if (index >= 0 && index < 26) {
skillFactor = skillFactor * skillFactorsForLetters[index];
weight = weight * weightFactorsForLetters[index];
}
}
}
And ChatGPT beautifully created the class for me.
Final communication was:
Sure! I've updated the Tool
class to include the efficiency
member, which is calculated in the constructor based on the level
from dungeonEntityInfo
.
export class Tool {
static skillFactorsForLetters = [
1, 0.9, 1.2, 0.8, 1, 1.1, 1.1, 1.3,
1, 1.2, 1.11938, 1.05, 0.8, 0.9, 1, 0.8, 1.3,
1.3, 0.9, 0.9, 1, 1.3, 1.1, 1.4, 1.2, 0.9
];
static weightFactorsForLetters = [
1, 0.8, 1.2, 0.8012, 1, 1.1, 1.2, 0.6,
1, 1.1, 0.9, 0.75, 0.8, 0.8, 1, 0.9, 1.1,
1.1, 1.2, 1.1, 1, 0.8, 0.7, 1.3, 0.8, 1.1
];
constructor(dungeonEntityInfo) {
this.dungeonEntityInfo = dungeonEntityInfo;
const word = dungeonEntityInfo.getWord();
const len = word.length;
const level = dungeonEntityInfo.getLevel();
this.skill = 1.0;
this.weight = this.getDefaultWeight();
for (let i = 0; i < len; i++) {
const index = word.charAt(i).toLowerCase().charCodeAt(0) - "a".charCodeAt(0);
if (index >= 0 && index < 26) {
this.skill = this.skill * Tool.skillFactorsForLetters[index];
this.weight = this.weight * Tool.weightFactorsForLetters[index];
}
}
this.efficiency = level - 1 + Math.pow(1.13, level);
this.efficiency = Math.round(this.efficiency * 10 + 0.5) / 10;
}
getDefaultWeight() {
return 0.1;
}
// You can add other methods here to manipulate or retrieve weight and skill values
}
Now the Tool
class has an efficiency
member that is calculated based on the level
from dungeonEntityInfo
.
I then asked it to create a test web page to enter a word that is used to create a DungeonEntityInfo from which a Tool is created and output the weight, skill, efficiency of the tool. Worked flawless.
(nb ChatGPT always makes a minor mistake in these web pages, trying to use the JS function to determine stuff from onInput of the input field, which doesn't work with "module scripts" (don't ask me, I just tell it the error I'm getting and it fixes the page :-) )
Thoughts on this
Yes, the java code was trivial to change. But I'm still impressed.
Also I find it cool that I can tell it things like:
okay, let's now construct the tool. Let's try to re-use the java method I had for that. weight and skill are computed from the word in DungeonEntityInfo with dungeonEntityInfo.getWord(); and then what the java code did was this:
<code>
I will tell you later how to implement getDefaultWeight(). Can you change the class?
and it will happily do so, incl.
...
getDefaultWeight() {
// Implement your logic for getDefaultWeight() here
}
...
This updated class now calculates the weight
and skill
values based on the word in the provided DungeonEntityInfo
.
You can now implement the getDefaultWeight()
method according to your requirements.
This way it is fairly easy to break down creating longer classes into bits, and tell it to leave this or that method unimplemented for now.
Kommentare
Kommentar veröffentlichen