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

Trouble drawing the map in python basics 'dungeon game'

sometimes the map draws perfectly fine, other times it has added rooms or duplicate symbols, really confusing me! Any ideas? Any other overall advice before moving on?

*link to code snapshot below

6 Answers

Hi Robert,

I'm seeing 3 problems so far.

1. Multiple doors and players at a grid size of 20 (probably grid size 11 or higher) -

This seems to be fixed after correcting the grid generation problem that Kenneth pointed out.

In your code you're trying to generate a list of a repeating number.

Here's a more reliable way that you could do that:

>>> [9] * 10
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]

You can use your row and size variables to get each list that you need.

As Kenneth mentioned, it can be made simpler. Try to think about how to do it with nested for loops and range(). It can also be done in 1 line with a list comprehension.

2. Sometimes missing door or player (probably monster too) noticed at grid size of 5 -

This one seems to happen when a gem location is chosen that was already taken by the player or door.

3. An extra room in one of the rows noticed at grid size of 5 -

This one seems to happen when 2 gems share the same location. One gem goes in the correct location and the other gem goes in the room to the right pushing everything after that to the right by one.

Problems 2 and 3 are related and originate in your get_locations() function. They are due to either incorrect or missing collision detection.

You're using the following for loop to check if any gems collide with the player, door, or monster.

for gem in gems:
    if gem == player or gem == door or gem == monster:
        continue

The problem is that you're only continuing the for loop but you really need to continue the while loop so that new choices are made. To get around this, I would recommend refactoring that into an if condition.

if player in gems or monster in gems or door in gems:
    continue

What you're missing is if there is any collision among the gems. One way that you can do this is to convert the list to a set which will remove any duplicates then you can compare the length of the set against the length of the list.

if len(gems) != len(set(gems)):
    # gems had duplicates
    continue

Give these things a try and see if that takes care of all the problems. I ran it a few times after those changes and hadn't noticed anything.

With all the collision detecting you have to do, I started wondering if it would be easier/better to work on a copy of the grid and remove each choice after you make it.

Then you could remove the while loop and all the collision detecting code.

Example for player:

grid_copy = GRID[:]

player = random.choice(grid_copy)
grid_copy.remove(player)

Alternatively, you could shuffle the grid and then pop items off as you need it.

grid_copy = GRID[:]

random.shuffle(grid_copy)
player = grid_copy.pop()
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Can you provide a screenshot of the duplicated symbols, etc? Or give me a way to reliably trigger them? It's easier to debug if we know what the bad output looks like.

it seems to be on the higher numbers, so this was done with 'size' as 20 (20x20 grid)

MAP

sorry i don't know how to post a picture!

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I printed out the GRID variable when it's generated. Here's what I got:

DEBUG:root:grid: [
(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (0, 11), (0, 12), (0, 13), (0, 14), (0, 15), (0, 16), (0, 17), (0, 18), (0, 19),
(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (1, 13), (1, 14), (1, 15), (1, 16), (1, 17), (1, 18), (1, 19),
(2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (2, 11), (2, 12), (2, 13), (2, 14), (2, 15), (2, 16), (2, 17), (2, 18), (2, 19),
(3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (3, 11), (3, 12), (3, 13), (3, 14), (3, 15), (3, 16), (3, 17), (3, 18), (3, 19), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (4, 11), (4, 12), (4, 13), (4, 14), (4, 15), (4, 16), (4, 17), (4, 18), (4, 19), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (5, 15), (5, 16), (5, 17), (5, 18), (5, 19), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (6, 15), (6, 16), (6, 17), (6, 18), (6, 19), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (7, 15), (7, 16), (7, 17), (7, 18), (7, 19), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (8, 15), (8, 16), (8, 17), (8, 18), (8, 19), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (9, 11), (9, 12), (9, 13), (9, 14), (9, 15), (9, 16), (9, 17), (9, 18), (9, 19), (1, 0), (0, 1), (1, 2), (0, 3), (1, 4), (0, 5), (1, 6), (0, 7), (1, 8), (0, 9), (1, 10), (0, 11), (1, 12), (0, 13), (1, 14), (0, 15), (1, 16), (0, 17), (1, 18), (0, 19), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (1, 13), (1, 14), (1, 15), (1, 16), (1, 17), (1, 18), (1, 19), (1, 0), (2, 1), (1, 2), (2, 3), (1, 4), (2, 5), (1, 6), (2, 7), (1, 8), (2, 9), (1, 10), (2, 11), (1, 12), (2, 13), (1, 14), (2, 15), (1, 16), (2, 17), (1, 18), (2, 19), (1, 0), (3, 1), (1, 2), (3, 3), (1, 4), (3, 5), (1, 6), (3, 7), (1, 8), (3, 9), (1, 10), (3, 11), (1, 12), (3, 13), (1, 14), (3, 15), (1, 16), (3, 17), (1, 18), (3, 19), (1, 0), (4, 1), (1, 2), (4, 3), (1, 4), (4, 5), (1, 6), (4, 7), (1, 8), (4, 9), (1, 10), (4, 11), (1, 12), (4, 13), (1, 14), (4, 15), (1, 16), (4, 17), (1, 18), (4, 19), (1, 0), (5, 1), (1, 2), (5, 3), (1, 4), (5, 5), (1, 6), (5, 7), (1, 8), (5, 9), (1, 10), (5, 11), (1, 12), (5, 13), (1, 14), (5, 15), (1, 16), (5, 17), (1, 18), (5, 19), (1, 0), (6, 1), (1, 2), (6, 3), (1, 4), (6, 5), (1, 6), (6, 7), (1, 8), (6, 9), (1, 10), (6, 11), (1, 12), (6, 13), (1, 14), (6, 15), (1, 16), (6, 17), (1, 18), (6, 19), (1, 0), (7, 1), (1, 2), (7, 3), (1, 4), (7, 5), (1, 6), (7, 7), (1, 8), (7, 9), (1, 10), (7, 11), (1, 12), (7, 13), (1, 14), (7, 15), (1, 16), (7, 17), (1, 18), (7, 19), (1, 0), (8, 1), (1, 2), (8, 3), (1, 4), (8, 5), (1, 6), (8, 7), (1, 8), (8, 9), (1, 10), (8, 11), (1, 12), (8, 13), (1, 14), (8, 15), (1, 16), (8, 17), (1, 18), (8, 19), (1, 0), (9, 1), (1, 2), (9, 3), (1, 4), (9, 5), (1, 6), (9, 7), (1, 8), (9, 9), (1, 10), (9, 11), (1, 12), (9, 13), (1, 14), (9, 15), (1, 16), (9, 17), (1, 18), (9, 19)]

Notice anything missing from the X coordinates above 9?

This is because of your tuple(str(row) * size) line. The tuple of 10 is ('1' '0') because each item in the string is turned into an item in the tuple.

That whole make_grid method can be simplified.

thanks guys, maybe i need to go over all the basics and collections tracks again, im not sure i got alot of that, but ill try and work it through and make the adjustments, thanks for the help!! one thing though... the tracks are like '5 hours' and '3 hours' and ive gone through them twice already, taken me weeks!! and yet still couldn't work through any of these problems without your help, are other people actually doing this in just a few hours or days?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Different people learn at different speeds. Don't compare your learning to anyone else's! Some people grasp these concepts right way and other take longer. For my own example, I've created my own decorators several times, I've even done a workshop about them...but I still have to look up the docs every single time and often get it wrong the first time anyway. That doesn't make me worse than someone that can create a decorator from memory in a few minutes. It doesn't mean I'm a bad programmer, bad at Python, or a bad learner.

sorry... 1 problem!!!

i'm struggling with condensing the make_grid function down into a single line, ive got:

def make_grid(size):
    GRID = []
    for row in range(size):
        GRID.extend(list(zip([row] * size, range(size))))
    return GRID

which is working so far it seems, but when i do something like:

def make_grid(size):
    GRID = [GRID.extend(list(zip([row] * size, range(size)))) for row in range(size)]
    return GRID

or

def make_grid(size):
    return [list(zip([row] * size, range(size))) for row in range(size)]

it doesn't work, i can't quite work out how to condense it any further.

sorry to ask again just can't get it right!

Instead of trying to add a whole row at a time, you could solve it by adding one position at a time.

Here's what I had in mind for the nested for loops

GRID = []
for row in range(size):
    for col in range(size):
        GRID.append( (row, col) )

return GRID

And as a list comprehension

return [(row, col) for row in range(size) for col in range(size)]

Let me know if you have any questions about either one.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

For the GRID = [GRID.extend(list(zip([row] * size, range(size)))) for row in range(size)] line, this is failing, at least in part, due to the fact that GRID.extend() changes GRID in place and, so, the outside GRID assignment ends up being given an empty list.