# [Help] Double Gaps [RESOLVED]

Here is my Code:

For some reason, whenever my hero has built a fence, it moves back to rebuild the fence and causes a dead loop. I have tried for many hours but to no avail? Any advice on how to beat this level?

// Get the hero and the peasant to the south.

// The function move your hero down along the center line.
function moveDownCenter() {
var x = 40;
var y = hero.pos.y - 12;
hero.moveXY(x, y);
}

// The function build a fence on the right of something.
function buildRightOf(something) {
// buildXY a â€śfenceâ€ť 20 meters to somethingâ€™s right.
hero.buildXY(â€śfenceâ€ť, something.pos.x + 20, something.pos.y);
}

// The function build a fence on the left of something.
function buildLeftOf(something) {
// buildXY a â€śfenceâ€ť 20 meters to somethingâ€™s left.
hero.buildXY(â€śfenceâ€ť, something.pos.x - 20, something.pos.y);
}

while(true) {
var ogre = hero.findNearestEnemy();
var coin = hero.findNearestItem();
if (ogre) {
buildLeftOf(ogre);
}
if (coin) {
buildRightOf(coin);
}
moveDownCenter();
}

## twilight glasses ?

1 Like

Yeah. Thanks that was the problem. It had infinite range

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Number 1: Do not post solutions.
Number 2: When you post code, post using BEAUTIFUL formatting, according to FAQ.
Number 3: Do not revive dead threads. The last reply was 9 months ago. So donâ€™t reply!
Number 4: Twilight Glasses was THE PROBLEM, not the solution.
Number 5: Iâ€™m pretty sure when Code Combat gave you the sample code, it did not give you the link. You probably plagarized this code from somewhere else.

Above are 5 reasons you should delete this post.