Yet Another Crux of the Desert

First character I used moved too slowly so that I couldn’t get down to the bottom left in time. Second character I used moved too quickly it seemed, for he would set up the 4th fire-trap when the second ogre was coming. This would mean the ogre would not take a straight path and just barely (but accidentally) walk around the fire-trap. So, I decided to slow down my character by adding a say command, still to no avail. Long story short, now he tries to set up a second fire-trap where one is already set up, and blows himself up.

// Figure out which direction the ogres are coming from.

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Left: enemy.pos.x is less than hero.pos.x
        var isLeft = hero.pos.x  > enemy.pos.x;
        // Above: enemy.pos.y is greater than hero.pos.y
        var isAbove = hero.pos.y < enemy.pos.y;
        // Right: enemy.pos.x is greater than hero.pos.x
        var isRight = hero.pos.x > enemy.pos.x;
        // Below: enemy.pos.y is less than hero.pos.y
        var isBelow = hero.pos.y < enemy.pos.y;
        
        // If enemy isAbove and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isLeft && isAbove) {
            hero.buildXY("fire-trap", 20, 51);
           
            hero.moveXY(40, 34);
        }
        
        // If enemy isAbove and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isAbove && isRight) {
            hero.buildXY("fire-trap", 60, 51);
            hero.say("message");
            hero.moveXY(40, 34);
        }
        
        // If enemy isBelow and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isLeft) {
            hero.buildXY("fire-trap", 20, 17);
            hero.moveXY(40, 34);
        }
        
        // If enemy isBelow and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isLeft) {
            hero.buildXY("fire-trap", 60, 17);
            hero.moveXY(40, 34);
    } else {
        hero.moveXY(40, 34);
    }
}
}

Quick glance. Last group, check left or right again.

1 Like

Very strange. I fixed that issue, and now, my hero sits there and does nothing…normally I don’t have such unfortunate results from improving my code!

Another quick glance:

        // Right: enemy.pos.x is greater than hero.pos.x
        var isRight = hero.pos.x > enemy.pos.x; //?

        // Below: enemy.pos.y is less than hero.pos.y
        var isBelow = hero.pos.y < enemy.pos.y; // ? 
        
        // If enemy isBelow and isRight:
        if(isBelow && isLeft) {
2 Likes

Ok, I see that, and fixed it, thank you…and now what I have happening is everything running perfectly, for several iterations. Then, my guy decides to randomly (double checked the X,Y) overshoot moving to the center. Which is extra odd, since he ran that code before just fine… And so he cannot see the ogre coming, which creates all kinds of problems.

(I might need to make a short video.) Anyways, here is the full updated code:

// Figure out which direction the ogres are coming from.

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Left: enemy.pos.x is less than hero.pos.x
        var isLeft = hero.pos.x > enemy.pos.x;
        // Above: enemy.pos.y is greater than hero.pos.y
        var isAbove = hero.pos.y < enemy.pos.y;
        // Right: enemy.pos.x is greater than hero.pos.x
        var isRight = hero.pos.x < enemy.pos.x;
        // Below: enemy.pos.y is less than hero.pos.y
        var isBelow = hero.pos.y > enemy.pos.y;
        
        // If enemy isAbove and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isLeft && isAbove) {
            hero.buildXY("fire-trap", 20, 51);
           
            hero.moveXY(40, 34);
        }
        
        // If enemy isAbove and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isAbove && isRight) {
            hero.buildXY("fire-trap", 60, 51);
            hero.moveXY(40, 34);
        }
        
        // If enemy isBelow and isLeft:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isLeft) {
            hero.buildXY("fire-trap", 20, 17);
            hero.moveXY(40, 34);
        }
        
        // If enemy isBelow and isRight:
        // buildXY() a "fire-trap" at the X mark.
        if(isBelow && isRight) {
            hero.buildXY("fire-trap", 60, 17);
            hero.moveXY(40, 34);
    } else {
        hero.moveXY(40, 34);
    }
}
}

Thanks again.

“overshoot moving to the center”
You won’t overshoot the target if you use move instead of moveXY. Replace all occurrences of

hero.moveXY(40, 34)
// with
while (hero.distanceTo({x:40, y:34}) > 0 )hero.move({x:40, y:34})
// or the same thing expressed with vectors
while (hero.distanceTo(Vector(40, 34)) > 0 )hero.move(Vector(40, 34));
1 Like

i don’t no what is wrong whit my code there is always a weird error that’s telling me to put a " : " after a if statement but there is already a " : "

here is my code

# Figure out which direction the ogres are coming from.

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Left: enemy.pos.x is less than hero.pos.x
        isLeft = enemy.pos.x < hero.pos.x
        # Above: enemy.pos.y is greater than hero.pos.y
        isAbove = enemy.pos.y > hero.pos.y
        # Right: enemy.pos.x is greater than hero.pos.x
        isRight = enemy.pos.x > hero.pos.x
        # Below: enemy.pos.y is less than hero.pos.y
        isBelow = enemy.pos.y < hero.pos.y
        # If enemy isAbove and isLeft:
        # buildXY() a "fire-trap" at the X mark.
        if isLeft and isAbove:
            hero.buildXY("fire-trap", 20, 51)
        # If enemy isAbove and isRight:
        # buildXY() a "fire-trap" at the X mark.
        if enemy isAbove and isRight:
            hero.buildXY("fire-trap", 60, 51)
        # If enemy isBelow and isLeft:
        # buildXY() a "fire-trap" at the X mark.
        if enemy isBelow and isLeft:
            hero.buildXY("fire-trap", 20, 17)
        # If enemy isBelow and isRight:
        # buildXY() a "fire-trap" at the X mark.
        if enemy isBelow and isRight:
            hero.buildXY("fire-trap", 60, 17)
        hero.moveXY(40, 34)

Hi @FaasS, welcome to the forum :tada:
I think you’re taking the comments a little bit too literally.

Your if statement syntax is incorrect. It’s not possible to put two things straight after each other with out any connecting statement like and. You’ve got: enemy isAbove. That won’t work.
Look at the first if statement. It has the same comment as the one you’re supposed to be doing. Remember isAbove concerns the enemy already, so when the comment said “if enemy isAbove” It doesn’t mean you have to actually include enemy.
I hope this makes sense,
Danny

1 Like

Hi @FaasS Welcome to the forum!!! :partying_face: :partying_face:
Also try putting this in an else statement

1 Like