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.py print map and player move functions not working (possibly more issues)

Neither my player or monster are moving from their original positions on the map, and the movement directions don't work. I keep getting my "walls are hard..." string returned. Any help/guidance is appreciated!

import random

size = 8 # test size

DUNGEON = []
for x in range(size):
  for y in range(size):
    DUNGEON.append((x,y))



def get_locations():

  monster = random.choice(DUNGEON)
  door = random.choice(DUNGEON)
  player = random.choice(DUNGEON)

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


def move_player(player, move):

  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 move_monster():

  monster = random.choice(DUNGEON)

  if monster == door:
    return move_monster()
  else:
    return monster()


def get_moves(player):

  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

  if player[1] == 0:
    moves.remove('LEFT')
  if player[0] == 0:
    moves.remove('UP')
  if player[1] == size - 1:
    moves.remove('RIGHT')
  if player[0] == size - 1:
    moves.remove('DOWN')
  return moves


def draw_map(player):

  dungeon_lid = ' _' * size
  print(dungeon_lid)
  tile = '|{}'
  dungeon_array = []

  for i in range(len(DUNGEON)):
    if (i + 1) % (size) == 0:
      dungeon_array.append(i)

  for idx, cell in enumerate(DUNGEON):
    if idx not in dungeon_array:
      if cell == player:
        print(tile.format('X'), end='')
      elif cell == monster:
        print(tile.format('O'), end='')
      else:
        print(tile.format('_'), end='')

    else:
      if cell == player:
        print(tile.format('X|'))
      elif cell == monster:
        print(tile.format('O|'))
      else:
        print(tile.format('_|'))




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


while True:
  moves = get_moves(player)
  print('You are currently in room {}'.format(player))
  draw_map(player)
  print('You can move {}'.format(moves))
  print('Enter QUIT to quit')

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

  if move == 'QUIT':
    break

  if move in moves:
    player = move_player(player, move)
    monster = move_monster()
  else:
    print('** walls are hard...stop walking into them **')
    continue

  if player == door:
    print("Congratulations, adventuror! You've escaped the dungeon!")
    break
  elif player == monster:
    print("You've been ripped limb from limb by the monster.  Sorry about your life.")
    break

Hi Jenna,

I notice that you are using 3 quote marks (''') to try to format your code. You actually need to use 3 backticks (```) not quotes, that's why the formatting isn't coming through. The backtick is usually found in the upper left corner of your keyboard, sharing the same key as the ~ symbol.

It'll be easier to troubleshoot if we can see it formatted.

Ryan, thank you so much for that feedback! I had no idea all this time I was doing it wrong. Fixed!

3 Answers

Hi Jenna,

I just quickly tested your code and the only error that came up was "lIne 46: monster() not callable".

You just need to remove the parentheses from your second return statement in the move_monster function.

After fixing line 46, the game seems to run just fine. I was unable to replicate the errors you describe so I'm not sure what is going on with that.

Hope this helps.

Weird, I am not getting that error at all. Still having the same issues. My x and o t represent the player and monster aren't moving, and any direction I enter to move the player just gives me my running into walls error. I even tried running this on repl.it on a python 3 emulator (I have python 2.7 installed on my Mac) to see if maybe it was something in the cache on the Treehouse workspace.

Anyone?

Holy...yeah, after doing an OS update on my MacBook, tried it again and it worked. Maybe something was stuck in a cache somewhere...

Thank you for your input and guidance on this, Ryan!

No problem, although it looks like you solved it on your own. Glad you got it working.