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 Python Collections (2016, retired 2019) Dungeon Game Hit points

Idan shami
Idan shami
13,251 Points

I have no idea how to solve it... hints?

What I did wrong? how can I set that when the player hit the wall he will lose 5 points...? hints, please... have a good day

movement.py
# EXAMPLES:
# move((1, 1, 10), (-1, 0)) => (0, 1, 10)
# move((0, 1, 10), (-1, 0)) => (0, 1, 5)
# move((0, 9, 5), (0, 1)) => (0, 9, 0)

def move(player, direction):
             #right  left       up       down
    moves = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    x, y, hp = player
    xdire, ydire = direction
    if x == 0:
        moves.remove((-1, 0))
    if x == 8:
        moves.remove((1, 0))
    if y == 0:
        moves.remove((0, -1))
    if y == 8:
        moves.remove((0, 1))
    if direction == (-1, 0):
        xdire -= 1
    if direction == (1, 0):
        xdire += 1
    if direction == (0, -1):
        ydire -= 1
    if direction == (0, 1):
        ydire += 1
    return x, y, hp

2 Answers

andren
andren
28,558 Points

Your code is actually not that far off from the answer, you have come up with some good code but seems to have confused yourself slightly along the way.

You create a moves list and remove options from it based on where the player is, this is a good idea but after that you don't actually use this list at all.

Using that list you can determine if the direction the player is moving in is a is valid or invalid direction. And based on that either move the player or subtract hp from them.

Also you seem to have slightly misunderstood the size of the map, the valid squares are 0 to 9. Meaning that you should remove options if the player is on square 9, not on square 8.

Lastly xdire and ydire hold the x and y direction the player is asked to move, not the x and y of the player, so in your last section of code you check the direction the player is asked to move and then change said direction, rather than changing the player's position.

Looking at your code you don't really need the xdire and ydire variables at all, all places where you use them you should have used x and y instead.

If you need additional hints or want to see the solution then feel free to ask me and I'll be glad to assist in any way I can.

Idan shami
Idan shami
13,251 Points

what wrong in this code?

def move(player, direction):
             #right  left       up      down
    moves = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    x, y, hp = player
    x, y = direction
    if x == 0:
        moves.remove((-1, 0))
    if x == 9:
        moves.remove((1, 0))
    if y == 0:
        moves.remove((0, -1))
    if y == 9:
        moves.remove((0, 1))
    if direction == (-1, 0):
        x -= 1
    if direction == (1, 0):
        x += 1
    if direction == (0, -1):
        y -= 1
    if direction == (0, 1):
        y += 1
    if direction not in moves:
           hp -= 5
    return x, y, hp

I changed xdire and ydire to x and y, changed from 8 to 9 and said, if the direction not in moves subtract 5 from the hit points... thanks for answering erlier (:

andren
andren
28,558 Points

There are two issues, the first is this line:

x, y = direction

You assign the x and y variables to the x and y from the direction the player is moving, which overwrites the player position that you assigned them in the line above.

You don't really need to split direction into x and y at all so you can just remove that line entirely and your code will work better.

The second issue in your code is the fact that the player's position is changed even if they move in an invalid direction.

All of the x += 1 and similar code should only be ran if the direction that is moved in is valid.

This can be fixed very easily, all you need to do is add an else statement to your if direction not in moves code and move all of the if direction == (1, 0): and similar code into that else statement.

That way you will only change the player's x and y position if they are moving in a direction found in the moves list.

Idan shami
Idan shami
13,251 Points

WOW! thank you! you were very nice and explained it to me very nicely! (: I still don't believe you had the time and patience to help me! good day (: