Need Help for crossroads Java


#1

Plz Someone give code heres my code
// Use fire-traps to defeat the ogres attacking the trading post.

loop {
var enemy = this.findNearestEnemy();
if(enemy) {
if(enemy.pos.x < this.pos.x) {
// If the enemy is to the left, build a fire-trap to the left.

    } else if (enemy.pos.x > this.pos.x) {
        // If the enemy is to the right, build a fire-trap to the right.
        
    } else if (enemy.pos.y < this.pos.y) {
        // If the enemy is below the hero, build a fire-trap below.
        
    } else if (enemy.pos.y > this.pos.y) {
        // If the enemy is above the hero, build a fire-trap above.
        
    }
}
this.moveXY(40, 34);

}


#2

I believe this is the default code. You haven’t done anything to this. Furthermore, the code is not even formatted in the first place. Read the FAQ before posting again. You must learn how to format your code. I’ve done it for you this time.

We can not help you unless we have a specific problem to target. Please give us more information.


#3

I need help! Here is my code:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if enemy.pos.x < hero.pos.x:
            # If the enemy is to the left, build a fire-trap to the left.
            hero.buildXY("fire-trap", enemy.pos.x, hero.pos.x) + 10
            pass
        elif enemy.pos.x > hero.pos.x:
            # If the enemy is to the right, build a fire-trap to the right.
            hero.buildXY("fire-trap", enemy.pos.x, hero.pos.x) - 10
            pass
        elif enemy.pos.y < hero.pos.y:
            # If the enemy is below the hero, build a fire-trap below.
            hero.buildXY("fire-trap", enemy.pos.y, hero.pos.y) + 10
            pass
        elif enemy.pos.y > hero.pos.y:
            # If the enemy is above the hero, build a fire-trap above.
            hero.buildXY("fire-trap", enemy.pos.y, hero.pos.y) - 10
            pass
    hero.moveXY(enemy.pos.x, enemy.pos.y)

Whenever I hit “Run” my hero goes off to the side, builds a fire-trap too far from the X mark, walks down into the wall, and then gets killed by an enemy. How do I fix this?


#4

I’m not sure what you are trying to do here. Look at the syntax of buildXY. You need three parameters: type, x and y coordinates. You use two x coordinates instead. Also +10 to the result of the function is useless.


#5

Ha! For some reason I got mixed up and got from somewhere that it was okay to put the +10 after the buildXY Sorry about that! For the x and y coordinates, I thought that if both were on the same coordinate instead of x, y, then it would help, somehow, sorry, I’m not that great at explaining things… :stuck_out_tongue: Here’s an edit:

while True:
    yak = hero.findNearestEnemy()
    heroPos = hero.pos
    heroX = hero.pos.x
    heroY = hero.pos.y
    if yak:
        if yak.pos.x < hero.pos.x:
            # If the enemy is to the left, build a fire-trap to the left.
            hero.buildXY("fire-trap", yak.pos.x, hero.pos.y)
            hero.moveXY(hero.pos.x, yak.pos.y) + 10
            pass
        elif yak.pos.x > hero.pos.x:
            # If the enemy is to the right, build a fire-trap to the right.
            hero.buildXY("fire-trap", yak.pos.x, hero.pos.y)
            hero.moveXY(hero.pos.x, yak.pos.y) -10
            pass
        elif yak.pos.y < hero.pos.y:
            # If the enemy is below the hero, build a fire-trap below.
            hero.buildXY("fire-trap", yak.pos.y, hero.pos.x)
            hero.moveXY(yak.pos.x, hero.pos.y) + 10
            pass
        elif yak.pos.y > hero.pos.y:
            # If the enemy is above the hero, build a fire-trap above.
            hero.buildXY("fire-trap", yak.pos.y, hero.pos.x)
            hero.moveXY(yak.pos.x, hero.pos.y) - 10
            pass

Now I’m starting to get a little bit confused. I can see what my hero is doing, which is to walk to wherever it is that the enemy is at, build a mine there, then both my hero and the enemy get blown up, so I don’t think I very much understand the concept yet, and I must have gotten confused somewhere along the road, while this was being taught.


#6

I’m also getting the idea that my entire code is a mess :confounded:


#7

Sorry, but yes.

Look, if you need to get coordinates that are left from the hero:

hero.moveXY(hero.pos.x - 10, hero.pos.y)

so we take hero.pos and change only x coordinates.


#8

So I insert the -10 and +10 inside the coordinate space? Then, only put them after the x, so that only the x coordinates get changed?


#9

for the hero.pos?


#10

Sorry, I just can’t seem to figure out how to solve this level…


#11

Hey! I came to say that I appreciated your help, and thanks for trying your best to help me out. I finally found a solution to solving the level and it worked :slight_smile: So again, thanks for helping me out with my understanding on this, and I’m very grateful that you took some of your time to help me out, so yeah! Thanks! :smiley:


#12

Oh. sorry. Just saw this post and your answers. Gratz!