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 trialVuk Spasojevic
9,491 PointsList of available moves in 2 x 2 dungeon game. How this is possible?
Kenneth overwrites grid, creating 2 x 2 grid. Then he placed player at the corner (0,2). After calling get_moves() function he receives list of available moves ['RIGHT', 'UP','DOWN'] in video. How this is possible? If you are placed in any of the corners of 2x2 grid you will always have just 2 available moves, not three as mentioned in the video. Am I wrong, can someone please provide some answer.
4 Answers
Marcin Lubke
13,253 PointsLogically thinking you're right, but it's not the way that program is written. It removes some move choices only if one of the player's coordinates is equal to the edge of the board. If your outside the board, your coordinates is "more than edge", so following the logic of the program, the move stays as an available option. The program simply doesn't think it's possible to be outside the board. In my opinion it should be corrected, but that's actually not the main topic of this particular section of the course.
Alex Wootton
Courses Plus Student 5,943 PointsI though this was the case as well however if you correct the test to use the coordinates (0, 1) which would be within the bounds of the game grid the function still returns ['RIGHT', 'UP', 'DOWN'] as possible options.
To figure out what was going on I used pdb (Python Debugger) to step through the code as it was executing. It turns out the new GAME_DIMENSIONS variable we set in the unit test (or in the python shell as shown below) is not used by the function and it is instead reading the original values set in dd_game.py
Here is my output when running the code through pdb:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> from dd_game import get_moves
>>> GAME_DIMENSIONS = (2, 2)
>>> get_moves((0, 1))
> d:\projects\python\python testing\dungeon\dd_game.py(70)get_moves()
-> x, y = player
(Pdb) n
> d:\projects\python\python testing\dungeon\dd_game.py(71)get_moves()
-> moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
(Pdb) x, y
(0, 1)
(Pdb) GAME_DIMENSIONS
(5, 5)
Whilst I did step through some more of the function the useful information is all here. You can see that it is registering the position of the player correctly however the values returned for GAME_DIMENSIONS
is that of the variable as it is set in dd_game.py
and not as we set it prior to running the function.
Now, can anyone explain why our variable is ignored? I'm sure there's a simple explanation but it'd be great to hear from an authority.
Thanks, Alex
Marcin Lubke
13,253 PointsSomeone already mentioned about this in the previous questions. I'm not sure if there's an error in the video, or was it intentional.
First, Kenneth forced grid size to 2x2, then he set the player's position to be (0,2) - remember that it's 0-based location, so actually the player is now outside the board. That's why the moves has three choices instead of two - the player actually isn't in the corner.
Vuk Spasojevic
9,491 PointsCan't understand. Even if he is off the grid, in order to enter the grid he will then only have one choice since he is located at 0th row(off grid) and second column(also off the grid). There is no point giving player the option to move on different off grid fields.
Alex Wootton
Courses Plus Student 5,943 PointsIn case anyone is interested someone has answered this here: https://teamtreehouse.com/community/how-could-the-player-possibly-move-up-python-testing
Vuk Spasojevic
9,491 PointsAlex, thanks for info.
Vuk Spasojevic
9,491 PointsVuk Spasojevic
9,491 PointsI agree with you. Thanks for this smart exchange of thoughts. Best regards