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

Anibal Marquina
Anibal Marquina
9,523 Points

Can I get a hand in this one??..

this is the challenge........

Our game's player only has two attributes, x and y coordinates. Let's practice with a slightly different one, though. This one has x, y, and "hp", which stands for hit points. Our move function takes this three-part tuple player and a direction tuple that's two parts, the x to move and the y (like (-1, 0) would move to the left but not up or down). Finish the function so that if the player is being run into a wall, their hp is reduced by 5. Don't let them go past the wall. Consider the grid to be 0-9 in both directions. Don't worry about keeping their hp above 0 either.

always says thay im not getting the rigth movements!!

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):
    x, y, hp = player
    direction = ['left','right','up','down']
    if direction == 'left':
        x -= 1
        if x == 0:
            direction.remove('left')
            hp -= 5
    elif direction == 'right':
        x += 1
        if x == 9:
            direction.remove('right')
            hp -= 5
    elif direction == 'up':
        y -= 1
        if y == 0:
            direction.remove('up')
            hp -= 5
    elif direction == 'right':
        y += 1
        if y == 9:
            direction.remove('down')
            hp -= 5

    return x, y, hp

3 Answers

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Hello,

it appears the function 'move' will receive two tuples, one for the player and one for the requested move. In your function you are taking a tuple as the argument direction, then you reset the argument direction to a list, ['left', 'right'.......]. You then check that direction is equal to 'left' for example, but direction is always equal to the list you set it to be.

you need to check the contents of the direction tuple, if the direction[0] == 0, you are moving in the y-direction, for example.

let me know if that helps?

James

Alright, so take this with a grain of salt as I haven't completed the challenge.

In my opinion there are only a few parts that you actually have to check. Keep in mind that you have your 0 to 9 limitations to think about. One can also go like this move (player, (1,1)) alright?

So if that's the case I would first check if the move is valid. By that I mean: (sorry for camel casing but I use a lot of java in my cs course)

def move(player, direction):
    x, y, hp = player
    moveX, moveY = direction
    if (x + moveX) < 0 or (y + moveY) < 0 or (x + moveX) > 9 or (y + moveY) > 9:
        hp = hp - 5
    else:
        x = x + moveX
        y = y + moveY
    return x, y, hp

I don't know if this will let you pass the challenge, but as you can see if first checks if any of the moves may it be in x or y are valid i.e. the sum results in < 0 or > 9. If none of these checks are tripped the move is valid.

Anibal Marquina
Anibal Marquina
9,523 Points

I modified the code like this.. in my python shell in working but is not working for overcome the challenge!

def move(player, direction):
    x, y, hp = player
    if direction == (-1,0):
        x -= 1
        if x < 0:
            hp -= 5
            x += 1
    elif direction == (1,0):
        x += 1
        if x > 9:
            hp -= 5
            x -= 1
    elif direction == (0,-1):
        y -= 1
        if y < 0:
            hp -= 5
            y += 1
    elif direction == (0,1):
        y += 1
        if y > 9:
            hp -= 5
            y -=1

    return x, y, hp          

          ```
James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

In the question it states that the players position, x, y should never be outside the 0-9 grid in either direction. I guess your code is failing because you allow the x or y coordinates to go to -1, or 10, before checking they are outside the range, deducting HP if so and then resetting them back to a correct coordinate. This code below should pass, the checking if an invalid move is done for each direction tuple first then either the HP is reduced or the x, y coordinate incremented.

If it still does not pass maybe you have to add an else statement that returns x, y, HP unmodified as the direction tuple would have to be an invalid move.

def move(player, direction):
    x, y, hp = player

    if direction == (-1, 0):
        if x == 0:
            hp -= 5
        else:
            x -= 1
    elif direction == (1, 0):
        if x == 9:
            hp -= 5
        else:
            x += 1
    elif direction == (0, 1):
        if y == 9:
            hp -= 5
        else:
            y += 1
    elif direction == (0, -1):
        if y == 0:
            hp -= 5
        else:
            y -= 1
    return x, y, hp