Prime Pathing (JavaScript)

After over an hour of work, I got the message no one wants to get: “There is a problem with your code.” It doesn’t even say where the problem is! Anyway, here’s my code:

var firetraps = hero.findByType("fire-trap");
var beartraps = hero.findByType("bear-trap");
var f1 = [];
var f2 = [];
var b = [];

function findPrimes(number) {
    for (var i = 2, i < Math.ceil(Math.sqrt(number.value)); i ++) {
        if (number.value % i == 0) {
            return false;
        }
    }
    return true
}

for (var x = 0; x < firetraps.length; x ++) {
    var firetrap = firetraps[x];
    if (firetrap.pos.y > 30) {
        if (findPrimes(firetrap) === true) {
            f1.push(firetrap);
        }
    }
    else {
        if (findPrimes(firetrap) === true) {
            f2.push(firetrap);
        }
    }
}

for (var y = 0; y < beartraps.length; y ++) {
    var beartraps = beartraps[y];
    if (findPrimes(beartrap) === true) {
        b.push(beartrap);
    }
}

var friends = hero.findFriends();

for (var c = 0, c < f1.length; c ++) {
    var trap1 = f1[c];
    if (trap1) {
        hero.moveXY(trap1.pos.x + 10, trap1.pos.y);
        hero.moveXY(trap1.pos.x, trap1.pos.y);
        hero.moveXY(trap1.pos.x - 10, trap1.pos.y);
        for (var m = 0; m < friends.length; m ++) {
            var friend = friends[m]
            hero.command(friend, "move", {'x': trap1.pos.x + 10, 'y': trap1.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap1.pos.x, 'y': trap1.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap1.pos.x - 10, 'y': trap1.pos.y});
        }
    }
}

for (var d = 0, d < f2.length; d ++) {
    var trap2 = f2[d];
    if (trap2) {
        hero.moveXY(trap2.pos.x - 10, trap2.pos.y);
        hero.moveXY(trap2.pos.x, trap2.pos.y);
        hero.moveXY(trap2.pos.x + 10, trap2.pos.y);
        for (var n = 0; n < friends.length; n ++) {
            var friend = friends[n]
            hero.command(friend, "move", {'x': trap2.pos.x - 10, 'y': trap2.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap2.pos.x, 'y': trap2.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap2.pos.x + 10, 'y': trap2.pos.y});
        }
    }
}

for (var e = 0, e < b.length; e ++) {
    var trap3 = f2[e];
    if (trap3) {
        hero.moveXY(trap3.pos.x - 10, trap3.pos.y);
        hero.moveXY(trap3.pos.x, trap3.pos.y);
        hero.moveXY(trap3.pos.x + 10, trap3.pos.y);
        for (var o = 0; o < friends.length; o ++) {
            var friend = friends[o]
            hero.command(friend, "move", {'x': trap3.pos.x - 10, 'y': trap3.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap3.pos.x, 'y': trap3.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap3.pos.x + 10, 'y': trap3.pos.y});
        }
    }
}

It’s a bit extensive, but as far as I can tell, the functions are complete, and none of the commands are incomplete. I’ve extinguished my list of what to check. I must have gotten something wrong, though … any help?

In JavaScript, you don’t define objects with {"property": value} you just do {property: value}, no quotes

Also, I would recommend not doing the bear traps, as the numbers there get a bit big and sometimes have a problem

And, just one more thing, I seem to be the one replying to all of your posts :joy:

Try a semicolon instead of a comma here:

for (var i = 2; i < Math.ceil(Math.sqrt(number.value)); i++) {

Same thing on the other loop lines that use a comma.

I haven’t looked through the rest of the code, but noticed that.

1 Like

Thanks for pointing that out! I hadn’t noticed it at all :laughing:

:rofl: Yeah! You’re contributions really help, though.

Okay, I changed the code and there’s some progress. Here’s what I have now:

var firetraps = hero.findByType("fire-trap");
//var beartraps = hero.findByType("bear-trap");
var f1 = [];
var f2 = [];
//var b = [];

function findPrimes(number) {
    for (var i = 2; i < Math.ceil(Math.sqrt(number.value)); i ++) {
        if (number.value % i === 0) {
            return false;
        }
    }
    return true
}

for (var x = 0; x < firetraps.length; x ++) {
    var firetrap = firetraps[x];
    if (firetrap.pos.y > 30) {
        if (findPrimes(firetrap) === true) {
            f1.push(firetrap);
        }
    }
    else {
        if (findPrimes(firetrap) === true) {
            f2.push(firetrap);
        }
    }
}

//for (var y = 0; y < beartraps.length; y ++) {
//    var beartrap = beartraps[y];
//    if (findPrimes(beartrap) === true) {
//        b.push(beartrap);
//    }
//}

var friends = hero.findFriends();

for (var c = 0; c < f1.length; c ++) {
    var trap1 = f1[c];
    if (trap1) {
        hero.moveXY(trap1.pos.x + 10, trap1.pos.y);
        hero.moveXY(trap1.pos.x, trap1.pos.y);
        hero.moveXY(trap1.pos.x - 10, trap1.pos.y);
        for (var m = 0; m < friends.length; m ++) {
            var friend = friends[m]
            hero.command(friend, "move", {'x': trap1.pos.x + 10, 'y': trap1.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap1.pos.x, 'y': trap1.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap1.pos.x - 10, 'y': trap1.pos.y});
        }
    }
}

for (var d = 0; d < f2.length; d ++) {
    var trap2 = f2[d];
    if (trap2) {
        hero.moveXY(trap2.pos.x - 10, trap2.pos.y);
        hero.moveXY(trap2.pos.x, trap2.pos.y);
        hero.moveXY(trap2.pos.x + 10, trap2.pos.y);
        for (var n = 0; n < friends.length; n ++) {
            var friend = friends[n]
            hero.command(friend, "move", {'x': trap2.pos.x - 10, 'y': trap2.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap2.pos.x, 'y': trap2.pos.y});
            hero.wait(1);
            hero.command(friend, "move", {'x': trap2.pos.x + 10, 'y': trap2.pos.y});
        }
    }
}

//for (var e = 0; e < b.length; e ++) {
//    var trap3 = f2[e];
//    if (trap3) {
//        hero.moveXY(trap3.pos.x - 10, trap3.pos.y);
//        hero.moveXY(trap3.pos.x, trap3.pos.y);
//        hero.moveXY(trap3.pos.x + 10, trap3.pos.y);
//        for (var o = 0; o < friends.length; o ++) {
//            var friend = friends[o]
//            hero.command(friend, "move", {'x': trap3.pos.x - 10, 'y': trap3.pos.y});
//            hero.wait(1);
//            hero.command(friend, "move", {'x': trap3.pos.x, 'y': trap3.pos.y});
//            hero.wait(1);
//            hero.command(friend, "move", {'x': trap3.pos.x + 10, 'y': trap3.pos.y});
//        }
//    }
//}

I commented out the beartrap code for now. Now my hero is just walking into the nearest mine, though …

hero.findByType() is not in order by distance, you have to organize the array, I wrote an almost-working function:

function organize (array, property, greater) {
    let sorted = [];
    let gt = (a, p, g) => {
        let bt = a[0];
        let condition = g ? ((t) => (t[p] > bt[p])) : ((t) => (t[p] < bt[p]));
        for (let item of a) {
            if (condition(item)) {
                bt = item;
            }
        }
        return bt;
    }
    for (let i in array) {
        sorted.push(gt(array.filter(tt => sorted.indexOf(tt) === -1), property, greater));
    }
    return sorted;
}

You need to use it with the distanceTo method somewhere :]

Okay, so I tried to make a function that would sort by distance, this is what I ended up doing:

function findNearest(array) {
    for (let j = 0; j < array.length; j ++) {
        let item = array[j];
        let distance = hero.distanceTo(item);
    }
    let sorted = array.sort(function(a, b) {return a.distance - b.distance});
    return sorted;
}

I got a message saying “Couldn’t read property: distance.” on the line with my sort function. I have a limited amount of knowledge of the “.sort()” function, so there’s probably something I’m missing, I just don’t know what.

Thank you for sending this function. I’m sure it works, but I didn’t understand all the different parts, so I wasn’t sure how to incorporate hero.distanceTo(). I appreciate your readiness to help with this, though! :blush:

1 Like

I’ve tried a couple of other things, and this was my most recent try:

while (f1.length > 0) {
    var trap1 = hero.findNearest(f1)
    if (trap1) {
        hero.moveXY(trap1.pos.x + 10, trap1.pos.y);
        hero.moveXY(trap1.pos.x, trap1.pos.y);
        hero.moveXY(trap1.pos.x - 11, trap1.pos.y);
        for (var m = 0; m < friends.length; m ++) {
            var friend = friends[m]
            hero.command(friend, "move", {'x': trap1.pos.x + 10, 'y': trap1.pos.y});
            hero.wait(1.6);
            hero.command(friend, "move", {'x': trap1.pos.x, 'y': trap1.pos.y});
            hero.command(friend, "move", {'x': trap1.pos.x - 11, 'y': trap1.pos.y});
        }
        f1 = f1 - trap1.id;
    }
}

It didn’t work, but I felt like I might be on to something, but I’m not sure how I’d do it. I thought that array.splice, array.remove, or maybe even array.filter might work, but I’m really not sure how I’d do that. Any help?

f1.splice(f1.indexOf(trap), 1) and objects are made like this: {property: value} not {"property": value} in JavaScript

Okay, perfect, that worked! The only problem is that now everyone except the last two people passes the final fire trap. I feel like this should be obvious, but is there a way to make everyone move together?
As a reference, here’s my code as it is now:

while (f1.length > 0) {
    var trap1 = hero.findNearest(f1);
    if (trap1) {
        hero.moveXY(trap1.pos.x + 4, trap1.pos.y);
        hero.moveXY(trap1.pos.x - 4, trap1.pos.y);
        for (var m = 0; m < friends.length; m++) {
            var friend = friends[m];
            hero.command(friend, "move", {x: trap1.pos.x + 4, y: trap1.pos.y});
            while (friend.pos.y != trap1.pos.y) {
                hero.wait(0.01);
            }
            hero.command(friend, "move", {x: trap1.pos.x - 4, y: trap1.pos.y});
        }
        f1.splice(f1.indexOf(trap1), 1);
    }
}

Hmmm, I don’t know what’s wrong there, I’ll send a copy of my code (where I remove some parts) and you can check where it went wrong

Nothing is wrong, it might be a bad seed. Just submit a couple of times.

1 Like

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