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

Jason Smith
Jason Smith
8,668 Points

Trouble with Hitpoints

is there anything i'm missing? also, can the player move diagonally in this scenario?

# 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):
    x, y, hp = player
    for x in player:
        if x == -1:
            hp -= 5
            x = 0
        if x == 10:
            hp -= 5
            x = 9
    for y in player:
        if y == -1:
            hp -= 5
            y = 0
        if y == 10:
            hp -= 5
            y = 9
    return x, y, hp

1 Answer

Boban Talevski
Boban Talevski
24,793 Points

I'm assuming the player can't move diagonally according to the examples.

But what you should be doing is checking the current position of the player, and the resulting position by applying the move in the specified direction. Now if that move turns out to be a valid move, you just return the new position and the same hitpoints. If not, you should keep the current player position and deduct the according number of hitpoints which your function returns. Checking if a move is valid would be if you apply the move, would the ending position be in the defined grid? I don't know the size of the defined grid, probably it was defined in the videos/challenges leading to this one.

Note that the input to the function is one of the three examples at a time (not all of them together), so there's no point in looping through the values of player, you already got the single values in variables x, y and hp respectively. And you should make sure your function would return the output as in the examples for the respective inputs, but still, one input and one output at a time.