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 trialWilfredo Casas
6,174 PointsThere is no monster in my dungeon
Hello, when I run my code I never hit the monster or the door, I am just moving indefinitely. May someone tell me where is my code wrong?
import random
#--------------------------
CELLS = [(0,2), (1,2), (2,2),
(0,1), (1,1), (2,1),
(0,0), (1,0), (2,0)]
#--------------------------
def get_places():
mo = (random.choice(CELLS))
do = (random.choice(CELLS))
me = (random.choice(CELLS))
if mo == do or mo == me or do==me:
return get_places()
return mo, do, me
#--------------------------
def move_player(me, move):
# i thought i had to get the me and move values first
x, y = me
if move == "left":
x -= 1
if move == "right":
x += 1
if move == "up":
y += 1
if move == "down":
y -= 1
return x,y
#--------------------------
def get_moves(me):
moves = ['left', 'right', 'up', 'down']
if me[0] == 0:
moves.remove('left')
if me[0] == 2:
moves.remove('right')
if me[1] == 2 :
moves.remove('up')
if me[1] == 0:
moves.remove('down')
return moves
#--------------------------
mo, do, me = get_places()
print("Welcome to the Dungeon!")
while True:
moves = get_moves(me)
print("Your are at {}, RUN!".format(me))
print("You can move to {}.".format(moves))
#print("press quit to quit the game")
move = input("> ")
if move == "quit":
break
if move == mo:
print("You were eaten by the grue")
print("GAME OVER")
break
elif move == do:
print("You won the game")
break
elif move in moves:
me = move_player(me, move)
else:
print("** Walls are hard, stop walking into them **")
continue
1 Answer
Peter Lodri
6,757 PointsIt's infinite cos you compare the location of the door and monster with the move, not with your coordinate. Just change:
if *me == mo:
print("You were eaten by the grue")
print("GAME OVER")
break
elif *me == do:
print("You won the game")
break
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsChanged to answer.