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
Oti Oritsejafor
3,281 PointsDungeon game - allows players move out of board?
I noticed that my game allows the player to be in the position (-1, 1) or (3 ,2). What is wrong please?
import random
BOARD = [(0, 0), (0, 1), (0, 2),
(1, 0), (1, 1), (1, 2),
(2, 0), (2, 1), (2, 2)]
def get_locations():
monster = random.choice(BOARD)
start = random.choice(BOARD)
door = random.choice(BOARD)
#If player, door or monster are random, do it all over again.
if (start == monster or start == door) or (monster == door):
return get_locations()
return monster, door, start
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
else:
print("That move doesn't exist PLEASE.")
return x, y
def get_moves(player):
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
if player[1] == 0:
moves.remove('LEFT')
if player[1] == 2:
moves.remove('DOWN')
if player[0] == 0:
moves.remove('RIGHT')
if player[0] == 2:
moves.remove('UP')
return moves
monster, player, door = get_locations()
while True:
moves = get_moves(player)
print("Welcome to the dungeon! *evil laugh*")
print("You are currently in room {}".format(player)) #fill in with player position
print("You can move {}".format(moves)) # fill in with available positions
print("Enter 'GIVEUP' to quit")
move = input("> ")
move = move.upper()
if move == "GIVEUP":
print("Giving up, you wait sadly for the Beast to find you. It does, and makes a tasty meal of you...")
print("You lose.")
break
if player == door:
print("You escaped, narrowly avoiding the Beast..")
print("You win!")
break
if player == monster:
print("You have been eaten by the Beast...")
break
if move in moves:
player = move_player(player, move)
else:
print("Walls are hard, stop walking into them!")
continue
1 Answer
Steven Parker
243,227 Points The direction names and values in get_moves don't correspond with those in move_player.
The former should probably look like this:
def get_moves(player):
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
if player[1] == 0:
moves.remove('LEFT')
if player[1] == 2:
moves.remove('RIGHT')
if player[0] == 0:
moves.remove('UP')
if player[0] == 2:
moves.remove('DOWN')
return moves
Oti Oritsejafor
3,281 PointsOti Oritsejafor
3,281 PointsThank you!