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

I expanded the Dungeon Game to have more tiles. Now I can't keep the player from going past the walls.

Here is the original code.

import random

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

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

    if monster == door or monster == player or player == door:
        return get_locations()
    return monster, door, player

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

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

    return x, y

def get_moves(player):
    moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

    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

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

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


monster, door, player = get_locations()

start = player
print('Welcome to The Dungeon.')
print("Enter QUIT to quit.")


while True:
    moves = get_moves(player)
    print("You're currenty in room  {}.".format(player)) 
    print('You can move {}.'.format(moves))

    draw_map(player)

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

    if move == "QUIT":
        break

    if move == "MONSTER":
        print("The Grue is in {}.".format(monster)) 
        continue

    if move == "DOOR":
        print("The door is in {}.".format(door))
        continue

    if move in moves:
        player = move_player(player, move)
    else:
        print("** Walls are hard. Stop walking into them! **")




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

Here is my tweaked code:

import random

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

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

    if monster == door or monster == player or player == door:
        return get_locations()
    return monster, door, player

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

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

    return x, y

def get_moves(player):
    moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

    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

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

    for idx, cell in enumerate(CELLS):
        if idx in [5, 11, 17, 23, 29, 35]:
            if cell == player:
                print(tile.format('x|'))
            else:
                print(tile.format('_|'))
        else:
            if cell == player:
                print(tile.format('x'), end='')
            else:
                print(tile.format('_'), end='')
    print("")

def draw_monster(monster):
    print(" _ _ _ _ _ _")
    tile = '|{}'

    for idx, cell in enumerate(CELLS):
        if idx in [5, 11, 17, 23, 29, 35]:
            if cell == monster:
                print(tile.format('x|'))
            else:
                print(tile.format('_|'))
        else:
            if cell == monster:
                print(tile.format('x'), end='')
            else:
                print(tile.format('_'), end='') 

monster, door, player = get_locations()

start = player
print('Welcome to The Dungeon.')
print("Enter QUIT to quit.")


while True:
    moves = get_moves(player)
    print("You're currenty in room  {}.".format(player)) 
    print('You can move {}.'.format(moves))

    draw_map(player)

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

    if move == "QUIT":
        break

    if move == "MONSTER":
        print("The Grue is in {}.".format(monster)) 
        draw_monster(monster)
        continue

    if move == "DOOR":
        print("The door is in {}.".format(door))
        continue


    if move in moves:
        player = move_player(player, move)
    else:
        print("** Walls are hard. Stop walking into them! **")
        player = start      


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

1 Answer

Hi there, I have taken a look at your code. Where you have gone wrong is inside the get_moves function, you have expanded your grid so inside your get moves function you should have made the grid boundaries to 5 since that is the coordinate of your outer cell. Doing this will stop the player from going outside of that cell. Your function should look like this:

def get_moves(player):
    moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

    if player[0] == 0:
        moves.remove('LEFT')
    if player[0] == 5: #<-- should be 5
        moves.remove('RIGHT')
    if player[1] == 0:
        moves.remove('DOWN')
    if player[1] == 5: #<-- should be 5
        moves.remove('UP')

    return moves

If you have any further questions, please feel free to leave a comment :).

Thanks,

Haider

Thank you. Honestly, sometimes I miss the explanations of why a number is there. I completely misunderstood just how this function was limiting the movement of the player. Now I get it.

I have to remove "player = start" which I added as a convoluted way to get the player back to a correct position.