Feedback: Danger Valley


#1

This level is really cool, but the guide needs help. It isn’t at all clear how the array of data is set up. Where am I supposed to build the traps? (The red Xs on the ground are a good hint though).

Not sure what type of thing we are trying to teach, if this is an advanced looping thing where players have recently been working on laying out a location based on loop indexes maybe, but probably you need to include a bit of the calculation for getting the location of the trap.

On an aesthetics nature, would be really cool if the bombs that you placed didn’t blow up until everyone was in place, so the referee sets them off at the point when the first ogre hits the trip wire. This could allow for a little more randomness in the location of the peasants with some peasants between ogres.


#2

Same questions here: there is a the “grid” (= 5x5 array) and a bunch of red Xs (1+4+3+2)… but what’s the relation between them? Where should I build those fire-traps?


#3

Yes, you have an array of arrays. You have to iterate the grid array which contains several rows, each row is an array that contains 0’s and 1’s for each column.

@nemoyatpeace @ant You have to iterate the “grid” array (an array of rows) storing the row index in the i variable, and for each row you have to iterate its columns storing the column index in the j variable. Then, check if the value of the given column in the given row is 1 and call execute the snippet that was commented out in the starting code: self.buildXY("fire-trap", 36 + 6 * j, 20 + 6 * i)

If you have ideas to improve the level or make this easier to understand, here is the place! :wink:

As for my feedback: i believe this level should require a hammer, which it currently doesn’t.
Also, the visual help (the red X’s on the ground) are a nice touch for users to see how close they are to the solution, but I wonder if those couldn’t be used to cheat the level (i.e. just build traps on the X’s)?


#4

I’m rather confused. I see nowhere instructions to define j, but only “Do a check to see if the tile of row i at j is 1.” How is the program supposed to use j if j is undefined?


#5

The comments in the code mention:

Iterate over all the tiles in this array:

And then the next comments and code use j, so I figured I should iterate the tiles in the row storing the iteration index in a j variable. Also, from the courses I’ve taken, it seems to be common practice to name iteration index variables as i, j, k etc. when iterating “multidimensional”/nested arrays/vectors—although I personally find that a terrible practice; variables should have meaningful names. Maybe it would be clearer if the variables were called rowIndex and tileIndex?


#6

At the time I tried the level, there was no sample code in the python version, only comments. I reloaded the level and now I see the commented line of code.


#7

The basic idea is good (to teach about 2D arrays), but it’s probably not the best visual representation of it.

Anyway, the red Xs are unnecessary: they are a bit “misleading”, and not needed for the solution.


#9

I dont understand how use “for…in grid … do”…
I use
for i=1, #hero.grid do

for j=1, #row do

I’m not sure of the correctness of the decision


#10

I’m not really a Lua programmer, but I believe you are looking for the Generic for documentation.


#11

I’m still having problems with this level. Here’s my code:

for i in range(len(hero.grid)):
    row = hero.grid[i]
    # Now, row is just another array!
    # Iterate over all the tiles in this array:
    for j in hero.grid[i]:
        # Check if the tile at i, j is 0:
        if hero.grid[i][j] == 1:
            hero.buildXY("fire-trap", 36 + 6 * j, 20 + 6 * i)
        
# Finally, retreat back to cover.
hero.moveXY(29, 55)

My hero builds fire-traps on the x’s in the leftmost two columns only. Could I have help, please?


#12
for j in hero.grid[i]:

is the problem here. hero.grid[i] is an array. Just like hero.grid is.


#13

I’m not sure I understand. Isn’t the point of the for-statement to iterate through an array? Should I use “row” instead?


#14

So, in python, it’s a bit more complicated.

for element in array:
    # element is a[0], a[1], a[2], ..., a[len(array)-1]
for index in range(len(hero.grid)):
    # index is 0, 1, 2, ..., len(array) - 1

The pythonic way of writing the function would be: (which I didn’t understand while writing the level)

# Iterate over each row
for row in hero.grid:
    # Iterate over each cell in a row
    for cell in row:
        hero.say(cell) # 1 or 0

But the traditional for-loop way with indexii is:

# Iterate over each index of the parent array:
for rowIndex in range(len(hero.grid)):
    row = hero.grid[rowIndex]
    # Iterate over each index of the nested array:
    for cellIndex in range(len(row)):
        cell = row[cellIndex]
        hero.say(cell) # 1 or 0

Edit, now finally, what you are saying is:

for i in range(len(hero.grid)):
    row = hero.grid[i]
    for j in hero.grid[i]:
        # hero.grid[i as index (number)][j as element (a number, but only 0 or 1)]
        if hero.grid[i][j] == 1:
            # do stuff

#16

Sorry if I’m being thick, but I’m still getting it wrong. I’m trying

for i in hero.grid:
    for j in i:
        if hero.grid[i][j] == 1:
            hero.buildXY("fire-trap", 36 + 6 * j, 20 + 6 * i)

And I get the message, “TypeError: Cannot read property ‘1’ of undefined.”