SleepWalkers [HELP]

my code works so far but the hero only builds one wall. any help will be greatly appreciated.

var hunter = hero.findNearest(hero.findFriends());
var fenceMap = hunter.getMap();

// This function converts grid into XY coordinates.
function convertCoor(row, col) {
    return {x: 34 + col * 4, y: 26 + row * 4};
}


// Iterate over fenceMap and build at fence at all 1s.
for(var j = 0; j < fenceMap.length; j++) {
    for(var i = 0; i<fenceMap.length; i++){
        if(i && j == 1){
            var coor = convertCoor(i,j);
            hero.buildXY("fence", coor.x, coor.y);
        }
    }
}
// Move back to the village after building the fences.
hero.moveXY(22, 15);

Keep in mind that you are checking the fenceMap grid coordinates for the “1’s”. Right now you are only comparing the index variables to 1.

if(i && j == 1)  //  check fenceMap with both variables  i and j in the array to compare against 1

I like to think of battleship on this one with fenceMap being the grid, with i and j the X, Y coordinates and 1 is their ship.

ok. thanks. ill try this and see if it works.:+1:

2 Likes

@brooksy125 Could you guys please help me with this level. I still don’t understand after your explanation. My code is very similar to that of @Alexander_Hollowell. Thank you

This is my code. It builds a fence in a random pattern, so any help would be very appreciated.

Our sleepwalking peasants are returning.

But sleeping yetis are also coming.

DONT WAKE UP THEM!.

We need to build fences to pass peasants and stop yetis.

Senick’s prepared the grid map how to build fences.

hunter = hero.findNearest(hero.findFriends())
fenceMap = hunter.getMap()

This function convert grid map coordinates to x-y coordinates.

def convertCoor(row, col):
return {‘x’: 34 + col * 4, ‘y’: 26 + row * 4}

Iterate fenceMap and if an element equals 1, then build there a fence.

for ri, row in enumerate(fenceMap):
for rx, col in enumerate(row):
if (col == 1):
coor = convertCoor(ri, rx)
hero.buildXY(‘fence’, coor.x, coor.y)

Yetis wake up if they smell the hero near. Hide in the village.

hero.moveXY(22, 15)

Can you please format your code according to this: [Essentials] How To Post/Format Your Code Correctly so we can help you?

Thanks,
-@Anna

1 Like
# Our sleepwalking peasants are returning.

# But sleeping yetis are also coming.

# DONT WAKE UP THEM!.

# We need to build fences to pass peasants and stop yetis.

# Senick’s prepared the grid map how to build fences.

hunter = hero.findNearest(hero.findFriends())
fenceMap = hunter.getMap()

# This function convert grid map coordinates to x-y coordinates.

def convertCoor(row, col):
return {‘x’: 34 + col * 4, ‘y’: 26 + row * 4}

# Iterate fenceMap and if an element equals 1, then build there a fence.

for ri, row in enumerate(fenceMap):
for rx, col in enumerate(row):
if (col == 1):
coor = convertCoor(ri, rx)
hero.buildXY(‘fence’, coor.x, coor.y)

# Yetis wake up if they smell the hero near. Hide in the village.

hero.moveXY(22, 15)

This is how you should format.

I tried your code and it worked.

Thank you for trying to help @jaden_the_best, but this topic has been dead for three months, if she still needed help she probably would have posted again.
I really appreciated you helping and please continue, but only on new topics. Otherwise no-one will see your post and your wasting your own time.
Danny

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