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 drawing map function

Here's the layout of my map when I start the program:

treehouse:~/workspace$ python dungeon.py                                                                                                         
Welcome to the dungeon!                                                                                                                          
You are currently in room (4, 1)                                                                                                                 
 _ _ _ _ _ _ _ _                                                                                                                                 
|_|                                                                                                                                              
|_|_|_|_|_|_|_|_|                                                                                                                                
|_|_|_|_|_|_|_|_|                                                                                                                                
|_|_|_|_|_|_|_|_|                                                                                                                                
|_|_|_|_|_|_|_|_|                                                                                                                                
|X|_|_|_|_|_|_|_|                                                                                                                                
|_|_|_|_|O|_|_|_|                                                                                                                                
|_|_|_|_|_|_|_|_|                                                                                                                                
|_|_|_|_|_|_|_You can move ['LEFT', 'RIGHT', 'UP', 'DOWN']                                                                                       
Enter QUIT to quit                                                                                                                               
>                                                                                                                                                

I'll follow up with my code next. I've been banging my head against a wall trying to figure out why it's appending a whole cell at the top of the dungeon and starting my available moves string on the same line as the end of my map. Any help/guidance is appreciated.

[MOD: added ```python formatting -cf]

# make monster move
# leave breadcrumbs for rooms you've visited
# fix running into wall print function


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

[MOD: added ```python formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

I see an issue with the loop variable I as a module of size. On the first loop, I has a value 0 which is a True modulo of and value:

>>> size = 10
>>> 0 % size == 0
True

If 1 is added before the test, it seems to correct the line alignment:

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

Addition correction might be needed in position reporting to handle the zero-based counting.

Oh, wow! So, all I had to do was skip that first test so as not to test 0. Totally understand and now see what type of affect testing 0 had on the dungeon.

Thank you much!