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

X prints twice on dungeon map. Why!?

I've completed the Dungeon Map game. Everything works well, but whenever the "player" is on a tile where x=3, an "X" shows up on both the x=3 and x=4 tiles of the map.

Also, the right wall of the map is not printing. Not sure if these issues are related. I've gone over the code a few times and I'm not sure why this is happening. How could both conditions of an if/else conditions be drawn?

import random
import os



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

def clear_screen():
  os.system('cls' if os.name == 'nt' else 'clear')


def get_locations():
  return random.sample(CELLS, 3)

def move_player(player, move):
  x,y = player
  if move == "LEFT":
    x = x - 1
  elif move == "RIGHT":
    x = x + 1
  elif move == "UP":
    y = y - 1
  elif move == "DOWN":
    y = y + 1
  else :
    print("That's not a direction!")
  player = x,y  
  return player

def get_moves(player):
  moves = ["LEFT", "RIGHT", "UP", "DOWN"]
  if player[1] == 0 :
    moves.remove("UP")
  if player[1] == 4:
    moves.remove("DOWN")
  if player[0] == 0:
    moves.remove("LEFT")
  if player[0] == 4:
    moves.remove("RIGHT")
  return moves

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

    for cell in CELLS:
        x, y = cell
        if x < 4:
            line_end = ""
            if cell == player:
                output = tile.format("X")
            else:
                output = tile.format("_")
        else:
            line_end = "\n"
            if cell == player:
                output = tile.format("X|")
            else:
                ouput = tile.format("_|")
        print(output, end=line_end)


def game_loop():
  monster, door, player = get_locations()
  playing = True

  while playing:
    clear_screen() 
    draw_map(player)
    valid_moves = get_moves(player)
    print("You're currently in room {}".format(player))
    print("You can move {}".format(", ".join(valid_moves)))
    print("Enter QUIT to quit")

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

    if move == 'QUIT' :
      print("\n See you next time! \n")
      break
    if move in valid_moves:
      player = move_player(player, move)

      if player == monster :
        print("\n You ran into the Monster. You're dead! \n")
        playing = False

      if player == door :
        print("\n You found the door, You win! \n")
        playing = False

    else: 
      input("\n ** You ran into a wall, ouch! **\n")
  else:
    if input("Play again? [Y/n]").lower() != "n":
      game_loop()


clear_screen()
print("Welcome to the dungeon!")
input("Press Enter Button to start!")
clear_screen()
game_loop()

2 Answers

you spelled output wrong here:

ouput = tile.format("_|")

Doh.

Thanks!