Python Python Collections (2016, retired 2019) Dungeon Game Cartographer

Raduica Sebastian
PRO
Raduica Sebastian
Pro Student 1,356 Points

Can someone please explain me the draw_map function?I have difficulties trying to understand the way it is written.

def draw_map(player): print (" _"*5) tile="|{}"

 for cell in CELLS:
    x, y =cell
    if x < 4:
        line_end=""
        if cell == player:
            output=tile.format("X")
        else:
            output=tile.format("_")
    else:
        line_end="\n"
        if cell == player:
            output = tile.format("X|")
        else:
            output=tile.format("_|")
    print(output, end=line_end)

3 Answers

Flore W
Flore W
4,719 Points

Hey, I have found it easier to understand if you actually take each cell in CELLS, and start drawing on a piece of paper as defined by the code.

CELLS = [(0,0), (1,0), (2,0), (3,0), (4,0),

                (0,1), (1,1), (2,1), (3,1), (4,1),

                (0,2), (1,2), (2,2), (3,2), (4,2),

                (0,3), (1,3), (2,3), (3,3), (4,3),

                (0,4), (1,4), (2,4), (3,4), (4,4)] 

player = (0,1)

You start by drawing 5 x " _" which gives you the upper end of the map. So you have a single line composed of 5 small _. It looks like this:

 _ _ _ _ _

The first item is (0,0). So we are in the case where x < 4 (0 < 4), and (0,0) is different from (0,1) (player). The code reads:

  • line_end ="" that means after this first item is printed, the next item will stay on the same line (as opposed to a line break)
  • output = tile.format("_") meaning what you draw for position (0,0) is a |_ . Your map should resemble:
 _ _ _ _ _
|_

The next item is (1,0), so again 1 < 4 and (1,0) different from (2,3). If you apply the same logic, your map should resemble:

 _ _ _ _ _
|_|_

... same for (2,0), (3,0) ...

Let's look at when you reach the last item with y =0. For (4,0), we are in the case where x > 4, and (4,0) is not (0,1). The code reads:

  • line_end = "\" that means after this we go to the next line instead of continuing horizontally.
  • output = tile.format("_|") this is to close the right hand side of the map, otherwise it would be open like |_ instead of |_|

Now your map should resemble:

 _ _ _ _ _
|_|_|_|_|_|

When we reach the player at (0,1), we are in the case where 0 <4, so:

  • line-end =""
  • output = tile.format("X") (that's basically |X) Note as in Kenneth' video, there is no line under the X.

Your map would resemble:

 _ _ _ _ _
|_|_|_|_|_|
|X

So on for the rest of the tuples in CELLS.

Hope you understand the logic better now. It is always a good thing to start drawing the map by hand, and back engineer how you would execute it in code.

Thank you so much bro

Ali Dahud
Ali Dahud
3,459 Points

And I Would like to add to this awesome explanation that after line_end='\n':

there's also an if-else statement which means IF your player is located on the 4th x draw a tile.format(X|) which looks like this |X|.

Kushagra Patel
Kushagra Patel
6,469 Points

this explanation really helped