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

[SOLVED] Help on Sleepwalker

So my hero only places fences in either a straight row or a column not both and im a little stuck


// Our sleepwalking peasants are returning.
// But sleeping yetis are also coming.
// Build fences to let peasants through and stop yetis.

// Senick's prepared the grid map how to build fences.
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 ri = 0; ri < fenceMap.length; ri++) {
    for(var rx = 0; rx<fenceMap.length; rx++){
        if(ri == 1){
            var coor = convertCoor(ri,rx);
            hero.buildXY("fence", coor.x, coor.y);

// Move back to the village after building the fences.
hero.moveXY(22, 15);

Two things I noticed:

  1. Your are using the index in your if check, not the array.
  2. When you check the β€œ1” you are currently only checking against one variable, so you will only get one dimension.
if(ri == 1){ // need to check array fenceMap with both variables ri & rx == 1
1 Like

@brooksy125 could you please post the if statement for i cannot seem to get my head around your statement. Thanks

Hi MrJumping,
It’s a while since the original discussion about this level. Could you post your code, and we can help you with it?

Thanks for your reply. In the end i figured it out :slight_smile: