Javascript: Magic exam Help

Here Is my code.

//Try to get the best grade (gold) at the magic exam.
// Move to each X mark, then use a spell.
var item = hero.findNearestItem();
var enemy = hero.findNearestEnemy();
hero.moveXY(18, 24);
var friend = hero.findNearestFriend();
if (friend) {
    hero.cast("heal", friend);
}
if (enemy) {
    hero.cast("force-bolt", enemy);
}
hero.moveXY(34, 24);
if (friend) {
    hero.cast("heal", friend);
}
if (enemy) {
    hero.cast("poison-cloud", enemy);
}
hero.moveXY(50, 24);
if (friend) {
    hero.cast("grow", friend);
}
if (enemy) {
    hero.cast("force-bolt", enemy);
}
hero.moveXY(18, 40);
if (friend) {
    hero.cast("heal", friend);
}
if (enemy) {
    hero.cast("force-bolt", enemy);
}
hero.moveXY(34, 40);
if (friend) {
    hero.cast("heal", friend);
}
if (enemy) {
    hero.cast("force-bolt", enemy);
}
hero.moveXY(50, 40);
if (friend) {
    hero.cast("heal", friend);
}
if (enemy) {
    hero.cast("force-bolt", enemy);
}
hero.moveXY(66, 40);
if (item) {
    if (item.type == "potion") {
        hero.moveXY(item.pos.x, item.pos.y);
    }
}
hero.moveXY(66, 24);
if (item) {
    if (item.type == "potion") {
        hero.moveXY(item.pos.x, item.pos.y);
    }
    if (item.type == "poison") {
        hero.cast("shrink", item);
    }
}

What am I doing wrong?

1 Like
  1. not all friends need to be healed, one needs to be regened. 2. I don’t think that you can cast shrink on the poison(at least, that’s not what I did) and 3. open the top and bottom doors in a column, before preforming the spells and looking for targets.(the top could have something good OR something bad, you don’t know until you open it.) I hope that helps.
1 Like

First of all, I think you should use a function like this one:

function action() {
    var friend = hero.findNearestFriend();
    if (friend) {
        
        if (friend.type == "soldier") {
            hero.cast("heal", friend);
        }
        if (friend.type == "goliath") {
            hero.cast("grow", friend);
        }
        if (friend.type == "paladin") {
            hero.cast("regen", friend);
        }
    }
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        
        if (enemy.type == "brawler") {
        hero.cast("shrink", enemy);
        }
        if (enemy.type == "scout") {
        hero.cast("poison-cloud", enemy);
        }
        if (enemy.type == "ogre") {
        hero.cast("force-bolt", enemy);
        }
    }
}

Okay, look at it this way. Okar you need to grow, shrink the brawler, poison cloud cast for scout, ogre force-bolt, poison cast grow on yourself and then drink it, potion drink, paladin regen.

1 Like

What do I do with the potions?
I got the people down.

You check whether the type is “potion” or “poison”. If it’s “potion”, you moveXY to the location because it’s good for you. And if it’s “poison”, you’ll want to cast “grow” on yourself so you don’t die from the effects.
Danny

2 Likes

Thank you for the tips.

var item = hero.findNearestItem();
    if (item) {
        if (item.type == "poison") {
            hero.cast("grow", hero);
            hero.moveXY("poison".pos.x, "poison".pos.y);
        }
        if (item.type == "potion") {
            hero.moveXY(item.pos.x, item.pos.y);
        }
    }

Here is my code, the hero picks up the health potion but does not grow ot pick up the poison.
What am I doing wrong?

You can’t get the position of a string
hero.moveXY(item.pos.x, item.pos.y);

As I said, I would find it really helpful if you would use a function that checks what type the friend/enemy is. Then you would use the correct spell on that.

Last you would need to Use the function consecutive times like this:
(These coordinates are not the real ones)

Hero.moveXY(45, 60)
Action()

Action would be the name of the function

Oh sorry I did not know we were already further.

But how I did it is I would cast growth on the hero and then just get bothe of the items (potion and poison)

In this case, you can use this code twice. Functions can be really helpful in this level and make your code cleaner.

1 Like

most certainly

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.