Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dungeon Game Building the Game: Part 2

Evan Waterfield
Evan Waterfield
3,240 Points

need help finding the syntaxError

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

  return moves

def draw_maps(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(title.format('_'), end= '')
    else:
      if cell == player:
        print(tile.format('x|'))
      else:
        print(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)

  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)
  else:
    print("**Walls are hard, stop walking into them! **")
    continue

  if player == door:
    print(" You've escaped!")
    break
  elif player == monster:
    print("You were eaten by ther grue!")
    break


  # If it's a good move, change the player's position
  # If it's a bad move, 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
line 69
monster, door, player = get_locations()
      ^
SyntaxError: invalid syntax

2 Answers

Hey, i notice just a few things, only syntax and typos. I'll comment my changes.

 def draw_maps(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= '') # fix typo here
    else:
      if cell == player:
        print(tile.format('x|'))
      else:
        print(tile.format('_|')) # add closing )



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_maps(player)  # change draw_map to draw_maps

worked for me after these tiny changes : )

Evan Waterfield
Evan Waterfield
3,240 Points

Nice! that did clear up my main issue now i'm having the issue of it not displaying my board correctly. As I don't see a 3 x 3 grid with my player in it.