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 - Moving the Monster Around

To the draw_map function, I have added a tilda to represent the grue that prints to the map. It was challenging to figure out how to print the monster and the player at the same, but after trial and error, I figured it out by using what looks to be my sloppy code as the solution. I have an understanding on how I did it, but could still use some clarification...Is there any way that I could simplify the code?

As for the monster's random generation, I added a move_monster function.

Criticism is advised. Let me know what you think.

Cheers,

Scott

import random

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

  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


# New
def move_monster(door):
  monster = random.choice(CELLS)
  if monster == door:
    return move_monster(door)
  return monster



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('UP')
  if player[0] == 2:
    moves.remove('DOWN')

  return moves


def draw_map(player, monster):
  print(' _ _ _')
  player_tile = '|{}'
  monster_tile = '|{}'
  # Please review this ugly mess...
  for idx, cell in enumerate(CELLS):
    if idx in [0, 1, 3, 4, 6, 7]:
      if cell == player:
        print(player_tile.format('x'), end='')
      elif cell == monster:
        print(monster_tile.format('~'), end='')
      elif cell != monster:
        print(monster_tile.format('_'), end='')
      else:
        print(player_tile.format('_'), end='')
    else:
      if cell == player:
        print(player_tile.format('x|'))
      elif cell == monster:
        print(monster_tile.format('~|'))
      elif cell != monster:
        print(monster_tile.format('_|'))
      else:
        print(player_tile.format('_|'))


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, monster)

  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(door)
  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 grue!")
    break

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points
# Don't need two variables to track the same format template
# Due to the "if player / elif monster", don't need check for not monster
# increased indent to 4 spaces
def draw_map(player, monster):
    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='')
            elif cell == monster:
                print(tile.format('~'), end='')
            else:
                print(tile.format('_'), end='')
        else:
            if cell == player:
                print(tile.format('x|'))
            elif cell == monster:
                print(tile.format('~|'))
            else:
                print(tile.format('_|'))

Ahh...ok. My over analyzing keeps getting in the way. Thanks a bunch, Chris.