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

Gilang Ilhami
Gilang Ilhami
12,045 Points

Dungeon Game

The problem is that my map returns a picture that goes down instead of a 3x3 picture

|_|                                                                                              
|_|                                                                                              
|_|                                                                                              
|_|                                                                                              
|X|                                                                                              
|_|                                                                                              
You can move ['LEFT', 'RIGHT', 'DOWN'].                                                          
Type QUIT  to quit     

Here is the code

import random 

CELLS = [(0, 0), (0, 1), (0, 2),
         (1, 0), (1, 1), (1, 2),
         (2, 0), (2, 1), (2, 2)]

def get_location():
    monster = random.choice(CELLS)
    door = random.choice(CELLS)
    start = random.choice(CELLS)

    if monster == start or monster == door or door == start:
        return get_location()

    return monster, door, start

def move_player(player, move):
    # player = (x, y)
    x, y = player

    if move == 'LEFT':
        y -= 1
    elif move == 'RIGHT':
        y += 1
    elif move == 'UP':
        x += 1
    elif move == 'DOWN':
        x -= 1
    return x, y

def get_moves(player):
    moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
    # player = (x, y)
    if player[1] == 0:
        moves.remove('LEFT')
    if player[1] == 2:
        moves.remove('RIGHT')
    if player[0] == 0:
        moves.remove('DOWN')
    if player[0] == 2:
        moves.remove('UP')
    return moves

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

    for idx, cell in enumerate(CELLS):
        if idx == [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('_|'))

monster, door, player = get_location()
print ('Welcome to the dungeon!')

while True:
    moves = get_moves(player)

    print ('You\'re currently in room {}.'.format(player)) 
    draw_map(player)
    print ('You can move {}.'.format(moves)) 
    print ('Type QUIT  to quit')

    move = input('> ')
    move = move.upper()

    if move =='QUIT':
        break

    if move in moves:
        player = move_player(player, move)
    else:
        print ('**Walls are hard, don\'t move any further**')
        continue

    if player == door:
        print('You\'ve escaped')
        break
    if player == monster:
        print('You\'ve been eaten by the grue')
        break

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Gilang,

The issue can be found in your draw_map function. In the for loop, you need to check if "idx" is in the list, not if it equals it.

for idx, cell in enumerate(CELLS):
        if idx in [0, 1, 3, 4, 6, 7]:

This statement is basically allowing you to draw the first 2 columns of each row. Each of those numbers (0, 1, 3, 4, 6, 7) represents the index of each item in the CELLS list needed to draw these columns. So we need to check if "idx" is a member of (in) that particular list of indexes.

The missing three numbers represent the 3rd column (idx = 2 or 5 or 8) and will require a tile with the '|' symbol on the right side.

Gilang Ilhami
Gilang Ilhami
12,045 Points

Thank you very much Ryan :)