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
Gary Gibson
5,011 PointsCan't understand why code is producing this grid.
Here is the full code:
import random
CELLS = [(0, 2), (1, 2), (2, 2),
(0, 1), (1, 1), (2, 1),
(0, 0), (1, 0), (2, 0)]
def get_locations():
monster = random.choice(CELLS)
door = random.choice(CELLS)
player = random.choice(CELLS)
start = player
if monster == door or monster == player or player == door:
return get_locations()
return monster, door, player
def move_player(player, move):
x, y = player
if move == 'LEFT':
x -= 1
elif move == 'RIGHT':
x += 1
elif move == 'UP':
y += 1
elif move == 'DOWN':
y -= 1
return x, y
def get_moves(player):
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
if player[0] == 0:
moves.remove('LEFT')
if player[0] == 2:
moves.remove('RIGHT')
if player[1] == 0:
moves.remove('DOWN')
if player[1] == 2:
moves.remove('UP')
return moves
def draw_map(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='')
else:
if cell == player:
print(tile.format('x|'))
else:
print(tile.format('_|'))
monster, door, player = get_locations()
start = player
print('Welcome to The Dungeon.')
print("Enter QUIT to quit.")
while True:
moves = get_moves(player)
print("You're currenty in room {}.".format(player))
print('You can move {}.'.format(moves))
draw_map(player)
move = input("> ")
if move == "QUIT":
break
if move == "MONSTER":
print("The Grue is in {}.".format(monster))
continue
if move == "DOOR":
print("The door is in {}.".format(door))
continue
if move in moves:
start = player
player = move_player(player, move)
else:
print("** Walls are hard. Stop walking into them! **")
player = start
if player == door:
print("You escaped!")
break
elif player == monster:
print("You were eaten by the Grue.")
break
It works and produces a grid like this:
_ _ _
|_|_|x|
|_|_|_|
|_|_|_|
Here is the function that draws the map:
def draw_map(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='')
else:
if cell == player:
print(tile.format('x|'))
else:
print(tile.format('_|'))
What I don't understand is why it doesn't produce a map like this:
_ _ _
|_|_ x|
|_|_ _|
|_|_ _|
Where is the third pipe from the left (second from the right) coming from?
1 Answer
Jeff Wilton
16,646 PointsI think the answer is in the second line of draw_map:
tile = '|{}'
Each tile is a left pipe followed by something passed in via string formatting: either an x, an underscore, an x followed by a pipe or an underscore followed by a pipe. Dynamic string formatting at it's finest!
Gary Gibson
5,011 PointsI get that, but it should only produce a pipe for the first two columns, as far as I can see: 0,1, 3,4, 6,7, not 2, 5, and 8.
Jeff Wilton
16,646 Points2, 5, and 8 still print the title string, which is always a left pipe plus extra content passed in.
Gary Gibson
5,011 PointsAh, I now that you've said that, I totally see it. Thank you.
Jeff Wilton
16,646 PointsJeff Wilton
16,646 PointsYou're welcome - glad I could help :)