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
Rufus M
2,725 PointsGetting extra characters when drawing map inside Python dungeon game
So , I figured I'd try to add something to a game and made it so that a user can specify how big they want a dungeon to be. Basically, the space is always square and I ask for a number and generate map based on it.
Now when I draw it though.. That's when it gets peculiar since I get those extra characters when I'm supposed to start new row. Its almost like it prints end parameters of the print function one more time even though I don't ask it it to. Could someone explain this to me?
import random
global dimension
def generate_map(num):
map = [(x,y) for x in range(num) for y in range(num)]
return map
def init_positions(map):
player = random.choice(map)
monster = random.choice(map)
door = random.choice(map)
if (player == monster or player == door or monster == door):
return init_positions(map)
return player, monster, door
def draw_map(map,player, monster, door):
tile = '|{}'
for cell in map:
if (cell[1] != dimension-1):
e = ''
else:
e = '|\n'
if cell == player:
print(tile.format('P'+e), end=e)
elif cell == monster:
print(tile.format('M' + e), end=e)
elif cell == door:
print(tile.format('D' + e), end=e)
else:
print(tile.format('-' + e),end=e)
def get_directions(player):
directions = ['UP','RIGHT','DOWN','LEFT']
x, y = player
if x == 0:
directions.remove('UP')
if y == 0:
directions.remove('LEFT')
if x == dimension-1:
directions.remove('DOWN')
if y == dimension-1:
directions.remove('RIGHT')
return directions
print("Welcome to the dungeon!")
print("Choose the dimensions of the dungeon:")
dimension = int(input("> "))
map = generate_map(dimension)
player, monster, door = init_positions(map)
while True:
if player == monster:
print('Sorry, you got eaten')
break
if player == door:
print('You escaped!!!')
break
draw_map(map,player, monster, door)
print('You are at position {}'.format(player))
available_moves = get_directions(player)
print('Available moves are {}'.format(available_moves))
print('Where would you like to go?')
move = input('>').upper()
x, y = player
if move in available_moves:
if move == 'UP':
x -= 1
elif move == 'DOWN':
x += 1
elif move == 'LEFT':
y -= 1
else:
y += 1
elif move == 'QUIT':
break
player = x, y
2 Answers
George Zabel
5,194 PointsYou're close! All you have to do is delete the | in line 25. It should read
e = '\n'
Currently, your code is asking for a line break plus a '|' in the first position. So just don't ask for it!
Also, if you want a more compact grid (i.e., no space in between each row, making it more gridlike), consider deleting the '+e' on lines 27, 29, 31, and 33. So, it would read
print(tile.format('P'), end=e)
for example. Then you'd stop asking for a line break for each tile, if that's what you want.
Please upvote and mark as best answer if this was helpful, I'm gunning for 60 forum points to get me a job :)
Rufus M
2,725 PointsThanks George. I realized that if I remove '+e' part from print statements, it does the trick. I can keep e defined as it is so it doesn't remove the walls.