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

Dungeon game map coordinates?

I cannot figure out the visual map behaviour with this and why it doesn't correspond to the coordinate system I specified. This is effectively Kenneth's solution, but it was more intuitive to me to have the x-axis represent horizontal movement and the y-axis, vertical movement. As it's written at the moment, the map that is produced is as if it's been rotated 90º clockwise - i.e. what should be the right wall is now the floor .

Would greatly appreciate any help.

Thanks,

Paul

import random
import sys

# Python convention - capitalise something that is a constant (cells wont change)
CELLS = [(0,0), (0,1), (0,2),
         (1,0), (1,1), (1,2),
         (2,0), (2,1), (2,2)]   

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

    if monster == door or monster == start or door == start:
        return get_locations() #repeat the function until unique values

    return monster, door, start

def move_player(player,move):
    x,y = player
    if move == 'LEFT':
        x -= 1
    elif move == 'RIGHT':
        x += 1
    elif move == 'DOWN':
        y -= 1
    elif move == 'UP':
        y += 1
    # if move is left, x - 1
    # if move is right, x + 1
    # if move is down, y - 1
    # if move is up, y + 1
    return x, y

#This is a much more efficient way than I did it. Using an IF statement rather than elif also means it doesn't matter what the other coordinate value is - VERY CLEVER
def get_moves(player):
    moves = ['LEFT', 'RIGHT', 'DOWN', 'UP']
    if player[0]==0:
        moves.remove('LEFT')
    if player[0]==2:
        moves.remove('RIGHT')
    if player[1]==0:
        moves.remove('DOWN')
    if player[1]==2:
        moves.remove('UP')

    return moves    
# if player's x is 0, remove LEFT
# if player's y is 0, remove DOWN
# if player's x is 2, remove RIGHT
# if player's y is 2, remove UP 

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

    for idx, cell in enumerate(CELLS):
        if idx in [0, 1, 3, 4, 6, 7]: # ie in left or centre tiles - select these as we don't want a new line.
            if cell == player:
                sys.stdout.write(tile.format('X')) # If I just use 'print' here I would get a blank space after each underscore and the map looks odd. In Python 3 can just use the print statement with an 'end' statement to avoid printing out a blank space.
            else:
                sys.stdout.write(tile.format('_'))
        else:
            if cell == player:
                print(tile.format('X|'))
            else:
                print(tile.format('_|'))


# Get the player, monster and door locations
monster, door, player = get_locations()
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("Enter QUIT to quit")

    move = raw_input("> ")
    move = move.upper()

    if move == 'QUIT':
        break
    if move in moves:
        player = move_player(player,move)
    else:
        print("Walls are hard - stop walking into them!")
        continue

    if player == door:
        print("You escaped!")
        break
    elif player == monster:
        print("You were eaten by the monster!")
        break

        # if it's a valid move, change the player's position
        # if it's not a valid move (e.g want to go up and they're already at the upper edge),           don't change anything
        # if the new player position is the door, they win!
        # if the new player position is the monster, they lose!
        # otherwise, continue

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

If I'm reading your question correctly, the rest of the code is working properly it is just the display of the dungeon that is off due to rotation?

Looking at the display function, it walks through the CELLS list in order and skips printing a new line character for non-right-edge cells.

If you are using X as horizontal, then reorder the CELLS in the list:

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

That's right Chris - just the display of the dungeon.

I get it now - thank you for your help. I didn't think that the ordering of the cells should make a difference.

I actually had to tweak it a little more to get it to work properly:

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

Thank you again.