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 Testing First Steps With Testing Writing and Running Doctests

Flore W
Flore W
4,744 Points

Can someone suggest a correction to the Dungeon get_moves() function?

Hi all, I read all the previous questions/answers regarding the error in the code regarding the get_moves() function. But I can't seem to find a solution to it - can someone suggest one? Thanks!

Here is a reminder of the code:

def get_moves(player):
    x, y = player
    moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

    if x == 0:
        moves.remove('UP')
    if y == 0:
        moves.remove('DOWN')
    if x == GAME_DIMENSIONS[0]-1:
        moves.remove('RIGHT')
    if y == GAME_DIMENSIONS[1]-1:
        moves.remove('DOWN')
    return moves

The result in the console shows:

>>> from dd_game import get_moves
>>> GAME_DIMENSIONS = (2,2)
>>> get_moves((2,2))
['LEFT','RIGHT','UP','DOWN']
>>> get_moves((1,1))
['LEFT','RIGHT','UP','DOWN']

First question: why do we need '-1' in x == GAME_DIMENSIONS[0] - 1? If I a in cell (2,2), I should just have x == GAME_DIMENSIONS[0].

Second question: if I am in (1,1), I should fill the conditions x == GAME_DIMENSIONS[0]-1 (2-1=1) and y == GAME_DIMENSIONS[1]-1 (2-1 =1), but why do I still get 'RIGHT' and 'DOWN' in my list of possible moves?

Thanks!

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

:point_right: First question: why do we need '-1' in x == GAME_DIMENSIONS[0] - 1?

The GAME_DIMENSIONS are given in natural numbers (1, 2, 3, ...). That is, a 3 means there are three positions. Since the code in python is using the 0-based indexing, the three positions would be numbered 0, 1, 2. The "- 1" is used to correctly compare the maximum dimension with the maximum python index.

:point_right: If I a in cell (2,2), I should just have x == GAME_DIMENSIONS[0].

Given GAME_DIMENSIONS = (2,2), the position "(2, 2)" would be outside of the game grid where the maximum position would be (1, 1) out of the list [(0, 0), (0, 1), (1, 0), (1, 1)]

:point_right: Second question: if I am in (1,1), I should fill the conditions x == GAME_DIMENSIONS[0]-1 (2-1=1) and y == GAME_DIMENSIONS[1]-1 (2-1 =1), but why do I still get 'RIGHT' and 'DOWN' in my list of possible moves?

Look again at which direction is being removed. Each direction should only have one way to removed it. "Down" appears to have two ways to delete it. Also, be sure that the x and y components are only affecting on direction. Either x does "UP" and "DOWN" and y does "LEFT" and "RIGHT" or vise versa. You have x limiting "UP" and y limiting "DOWN".

Post back if you need more help. Good luck!!

Flore W
Flore W
4,744 Points

Hi Chris, thanks a lot for this! Points 1 and 2 are clear.

Regarding Point 3, you're right, I have altered my code to look like this now, so RIGHT, LEFT, UP and DOWN are only removed once.

def get_moves(player):
    x, y = player
    moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']

    if y == 0:
        moves.remove('UP')
    if x == 0:
        moves.remove('LEFT')
    if x == GAME_DIMENSIONS[0]-1:
        moves.remove('RIGHT')
    if y == GAME_DIMENSIONS[1]-1:
        moves.remove('DOWN')
    return moves

However I still get the exact same result in the console:

>>> from dd_game import get_moves
>>> GAME_DIMENSIONS = (2,2)
>>> get_moves((1,1))
['LEFT', 'RIGHT', 'UP', 'DOWN']

instead of ['LEFT', 'UP']

Is something broken in my IF condition? I checked the code against the dungeon_game we built in Python Collections course, and it's the same, so I am not sure why this is not working...

Thanks for your help!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

You new code works correctly for me. Be sure to restart the REPL if you edit an import. Importing again in the same REPL session doesn't always grab the latest code.

GAME_DIMENSIONS = (2,2)
print(get_moves((1,1)))
['LEFT', 'UP']
Ross Coe
Ross Coe
5,061 Points

this is annoying - still not working.. get the same as Florence above even with repl restart

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Hi Ross, if you’re still having trouble post your current code in a new post and tag me. I’ll take a look.