Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

I need help on Explosive Works

# Place explosives to clear a passage to the dungeon.

# The mines should form a rectangle with this perimeter.
perimeter = 160
area = 0
# The bottom left corner of the rectangle.
bottomLeft = {"x": hero.pos.x, "y": hero.pos.y}

# One mine is placed already.
topLeft = hero.findHazards()[0].pos
# So we can find the height of the rectangle.
height = hero.distanceTo(topLeft)
# Find the width. Use the perimeter and height to calculate it.
width = (perimeter - height - height) / 2

# First, we need to place a mine in the bottom left corner.
hero.buildXY("fire-trap", bottomLeft.x, bottomLeft.y)
# Put a mine in the bottom right corner.
hero.buildXY("fire-trap", hero.pos.x + width, hero.pos.y)
# Put a mine in the top right corner.
hero.buildXY("fire-trap", hero.pos.x, hero.pos.y + height)
# Now go to the demolitionist.
hero.moveXY(74, 32)
# Calculate the area of the rectangle to know the charge.
area = width * height
# Say the area of the rectangle to start the explosion.
hero.say(area)

What is wrong with my code?

Please Help

The placement for the top right corner is skewed because you used the hero.pos instead of the bottomLeft base coordinates. If you watch your hero build the first mine, he/she isn’t standing at the exact coordinate you are building the fire-trap. So when you call on the hero.pos.x for the last trap the x position is not correct with the bottom right corner.

The bottomLeft variable is constant based on when the code ran, to it isn’t recalculating the hero.pos every time. Instead of using the hero.pos, I’d suggest using the bottomLeft coordinates consistently like it starts out with.

You mean like this?

This doesn’t work out well

# Place explosives to clear a passage to the dungeon.

# The mines should form a rectangle with this perimeter.
perimeter = 160
area = 0
# The bottom left corner of the rectangle.
bottomLeft = {"x": hero.pos.x, "y": hero.pos.y}

# One mine is placed already.
topLeft = hero.findHazards()[0].pos
# So we can find the height of the rectangle.
height = hero.distanceTo(topLeft)
# Find the width. Use the perimeter and height to calculate it.
width = (perimeter - height - height) / 2

# First, we need to place a mine in the bottom left corner.
hero.buildXY("fire-trap", bottomLeft.x, bottomLeft.y)
# Put a mine in the bottom right corner.
hero.buildXY("fire-trap", bottomLeft.x + width, bottomLeft.y)
# Put a mine in the top right corner.
hero.buildXY("fire-trap", hero.pos.x, bottomLeft.y + height)
# Now go to the demolitionist.
hero.moveXY(74, 32)
# Calculate the area of the rectangle to know the charge.
area = width * height
# Say the area of the rectangle to start the explosion.
hero.say(area)

I have got it! Thanks