Intro
Gridmancer Redux is a tricky level, and if you check the scoreboards, most of the people have empty code! At first glance, it might sound hard, but don’t you worry! I’ll teach you step-by-step on how to crack down this level.
How does it work?
You are given a two-dimensional array, and you must use that given data to place rectangles where it is needed. Hand-placing is not allowed!. So what exactly is a two-dimensional array? Here’s your simple array:
arr = [1,2,3,4]
Here’s a two-dimensional array:
arr2 = [[1,2,3],[4,5,6]]
In our case, instead of numbers, we have a “Wall”, or "Coin:.
First step – Go through the grid
The first step is to iterate in both dimensions to see if we get a Coin or not. First, we must iterate through the “rows”. To do that, you must iterate through the navGrid, the number of elements it has, total. To find the number of elements, you’d use len()
, and to repeat that the number of times, we can name our element, i. So here’s what we’ll have:
for i in range(len(hero.navGrid)):
...More Code
Then, we must go through the other way, so we can iterate through every single “placement” in the map. To do that, repeat the iteration but the i
th element of navGrid. Try to figure it out yourself! Call the second element, j
.
Second step – Finding the coin/wall
After you’ve finished the two for
loops, you can now check if the given “searched” element is a Wall, or a Coin. It’s just similar to finding a type of a troop in the array of friends, but with two dimensions. Here’s finding a “griffin-rider” within the array, friends:
friends = hero.findFriends()
for friend in friends:
if friend.type == "griffin rider":
More code...
You can also do the same for our case. Check if our i
and j
th element is a Coin. You can do so by using two elements instead of one. Since navGrid[i]
returns another array, we must also find the j
th element inside that array. Try figuring it out!
Third step – Placing the rectangles
Now that you have found whether that certain area is a Wall or Coin, you can start placing the squares on the coins! The command to place down a rectangle is hero.addRect(x,y,width,height)
. The x
and y
is the center of that rectangle. Remember, a single rectangle can pick multiple coins, use that as your advantage and start Reduxing!
If all goes well, you can finish this level in 27 lines!
Happy coding!