hi!
actually i have a very weak understanding about what’s going on with my code =) especially i’m always messing up with spacing and index incrementing. can someone tell me the way to improve this code. this is everything that i know for this moment, but i’ve spied a part of solution on a github.
loop:
whiteX = {'x':27, 'y':42}
items = self.findItems()
traps = self.findByType("fire-trap")
trapsIndex = 0
min = 9999
max = 0
enemies = self.findEnemies()
enemyIndex =0
while trapsIndex < len(traps):
trap = traps[trapsIndex]
if trap.value < min:
min = trap.value
if trap.value > max:
max = trap.value
trapIndex += 1
value = min + max
self.move(whiteX)
self.say(value)
if enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
enemyIndex += 1
if enemy:
self.attack(enemy)
You use “for” in the code above… and it is formatted incorrectly. It looks like that should be a “while”. I am not sure what you are doing with trap values either.
Your spacing under the current for loop and the if enemyIndex look wrong.
@Alex_Captain You need to increment your index within the while loop:
while trapsIndex < len(traps):
# blabla
trapIndex += 1 # note the indentation!
Furthermore, this part doesn’t need to be in a loop - you have to do it only once. The loop should start after that. Structure your code somehow like this:
# find the min and max values
# go to white X
# say the result
loop:
# if there is an enemy
# fight it
# otherwise
# move towards the exit
One last word of advice: the easiest way to fight enemies is to find the nearest one and attack it (if there is one).
First of all I can see that the code inside the while(true) loop isn’t indented (put 4 spaces forward).
And there’s more code at the top of the page which I can’t see, which might be causing the error.
Please could you post you code straight onto the Discourse, not take a screenshot.
Also make sure you format it, if you don’t know how to do that read this topic: link
Thanks,
With this being javascript, indentation isn’t required like Python, but very helpful.
The big piece is you can’t hard code the answer for the traps, but need to write code, functions being a suggestion, to find the answer since every time you submit, the trap values will be different.
Once you get past the traps, you should be ok if you have good enough armor and weapon.
Have you ever tried coding the password part of the code? Try doing it on your own first, then if you’re stuck, post your code here. This level should be easy if you’ve done the other desert levels.
// To disable the fire-traps add the lowest trap.value to the highest value.
// Move to the white X and say the answer to Kitty the cougar.
// Defeat all the ogres if you dare.
// Once all ogres are defeated move to the red X.
// Look out for potions to boost your health.
var whiteX = {x:27, y:42};
var redX = {x:151 , y: 118};
var low = 999;
var highest = 0;
var index = 0;
var findHazards = hero.findByType(“fire-trap”);
var enemies = hero.findEnemies();
var enemyIndex = 0;
while (index < findHazards.length) {
var trap = findHazards[index];
if (trap.value < low) {
low = trap.value;
}
if (trap.value > highest) {
highest = trap.value;
}
index += 1;
}
hero.moveXY(27, 42);
hero.say(low + highest);
var enemy = hero.findEnemies()[enemyIndex];
function attack() {
while (true) {
var enemy = hero.findEnemies()[enemyIndex];
if (enemy) {
if (hero.isReady("stomp")) {
hero.stomp(enemy);
}
if (hero.canCast("chain-lightning")) {
hero.cast("chain-lightning", enemy);
}
else if (hero.distanceTo(enemy) < 30) {
hero.attack(enemy);
enemyIndex = 0;
}
else {
hero.moveXY(58, 100);
}
enemyIndex += 2;
}
}