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

Sleepwalkers help

# Senick's prepared the grid map how to build fences.
hunter = hero.findNearest(hero.findFriends())
fenceMap = hunter.getMap()

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


# Iterate over fenceMap and build at fence at all 1s.
for i in fenceMap:
    for j in fenceMap:
        if i == 1:
            if j == 1:
                site = convertCoor(i, j)
                hero.buildXY('fence', site.x, site.y)
            


        
    

# Move back to the village after building the fences.
hero.moveXY(26, 17)

Just looking for some help here: basically nothing is happening at the moment! :frowning:

1 Like

Is there anything else that you can tell us that would be helpful? Are there any errors? Have you completed any objectives? A screenshot would be helpful.

No errors come up. Nothing happens.

There is more than one problem here.
One of them is this:

for i in fenceMap: #this should be:
for i in range(len(fenceMap)):
#the same for: for j in fenceMap:

The other one is to do with this section:

if i == 1:
    if j == 1:
        site = convertCoor(i, j)
        hero.buildXY('fence', site.x, site.y)

I’m not going to tell you what that bit is, or I might as well give you the answer.
I recommend reading the Hints carefully (especially the second page) and going back to other levels in this map series.
I’ll also give you a big Hint, if you’re stuck (try first, then look at the Hint)

Hint

You only need one if statement, and this part is right. Don’t change it!:

site = convertCoor(i, j)
hero.buildXY('fence', site.x, site.y)

:lion: :lion: :lion:

1 Like
for i in range(len(fenceMap)):
#the same for: for j in fenceMap:

This advice will work in this particular case because the 2D array has the same number of rows and columns, but otherwise it will fail. We have in this level. 8X8 array filled only with 0 and 1. See the visualisation. You can replace arr with arr1 to see how the code works with an “irregular array”.
Edit: @mratranslate : you have similar structure in Power points stuck and you did it right then :slight_smile:

1 Like

Yeah, you’re right.
@mratranslate listen to @xython. (although in this level, using what I said would work, but it’s better to do it properly (yes I do think there is a right way to do this.).)
:lion: :lion: :lion:

Conceptually, I like to think of the game battleship. Identify the grid and the X, Y coordinates to get the exact location. If that location grid[X][Y] == 1, (using the battleship analogy, “HIT”).

battleship

2 Likes