Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dungeon Game Building the Game: Part 2

Yuda Leh
Yuda Leh
7,618 Points

Getting a better understanding of the Draw Map

Hey, there guys!!!!

I somewhat understand what we are doing in the draw_map() function but I am do not 100% get what it code is doing.

No disrespect to Kenneth (he's amazing) but I wish he had explained some of the things he did a bit more

the Code

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

    for index, cell in enumerate(CELLS):
        if index in [0, 1, 3, 4, 6, 7]:
            if cell == player:
                print(tile.format('X'), end="")
            else:
                print(tile.format('_'), end="")
        else:
            if cell == player:
                print(tile.format('X|'))
            else:
                print(tile.format('_|'))

What I do understand: I understand why we use/need the index, its so that we can test if the player is that coordination of the CELLS (0, 1, 3, 4, 6, 7, center and left)

What I don't 100% understand: This part if more questions. First, the cell it the location of the CELLS tuple that is is currently at in the loop. So is it saying that if the player's current location(coordinates) is the same as the cell to the left of center then the tile will be "X" (meaning the player had moved to that location?)? And that if the cell is not at the player's location then it will be an empty spot by printing "_"?

It also says that if the player is not in the center or left (0, 1, 3, 4, 6, 7) and if the cell is at the current (same) location as the player then print "X|"? Meaning the player is on the right-hand side of the map. Otherwise, the player did not move to the right and it print "_|"? Meaning an empty slot?

I know this is very long! xD That why I appreciate anyone taking time to read this and help me out!! Thank you guys so much! Hope you had a great Thanksgiving day and a great weekend!

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Yuda,

When using enumerate(CELLS), we are basically pulling out each tuple and it's associated index. So if you look at the CELLS variable, the order of the coordinates are from the top left (0, 0) to the bottom right (2, 2) of the 3x3 matrix:

So the map with it's indexes looks like this. (This is also the order each cell will be drawn in.)

| 0 | 1 | 2 |

| 3 | 4 | 5 |

| 6 | 7 | 8 |

In the first if statement, we are first checking to see if the current index is in the first 2 columns (0, 1, 3, 4, 6, 7). These particular cells are drawn with the pipe symbol '|' on the left side of the tile, and left open on the right side.

If they are not in this list, we will drop down to the else: statement and take care of the remaining cells (2, 5, 8). These cells have the pipe symbol added on to the right side.

So once we determine which cell we are drawing, we then check if the player is in that cell. If yes, then draw the tile with an "X" instead of an underscore. If not, then draw an underscore. We use the "tile" variable and the string format method to make sure we have the pipe symbol on the left side of the cell.

You'll notice that when checking if index is in [0, 1, 3, 4, 6, 7], the print functions have the parameter end="". This prevents a new line from occurring when these cells are printed. When we get to cells 2, 5, and 8, that end attribute isn't there so we create a new line after printing and move down to the next row.

Every time we move the player, the map will be redrawn from scratch using this same function. But since the player location is different, the "X" will appear in a different place.

I hope this clears things up.

Yuda Leh
Yuda Leh
7,618 Points

I see, this really helped! I added comments to my code based on the info you had given me so that I can look back to it and understand what is going on. Thanks for all your help!

Happy Holidays!