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

Dungeon Game in OOP

I'm Stuck, when the player try to move out of limit of the board When the coordonate at x , reach 0, i receive an error

Here is the link for the code: https://repl.it/@jzzmatt/dungeongames

Jeff Muday
Jeff Muday
Treehouse Moderator 28,731 Points

This code looks pretty nice, you are very close to making it work out. The error being thrown in the boardLimit() method is because the code attempts to remove a letter from the self.moves parameter that was taken out in a previous move. You can fix this by reinstantiate the self.moves to the full list. You also want to check if a move is in the moves available to the user by checking if the "move" is in "self.moves"

    def set_move(self, move):
        if move in self._moves: # added this, check if move can be made
            if move == "L":
                self._x -= 1

            elif move == "R":
                self._x += 1

            elif move == "U":
                self._y -= 1

            elif move == "D":
                self._y += 1
        else: # added this else clause to show the move was an error, and the program didn't change their location
            print "That move is not available, try again."

    def boardLimit(self):
        self._moves = ["L", "R", "U", "D"] # add this to reinstatiate all the moves
        if self._x == 0 or self._x < 0:
            self._moves.remove("L")

        if self._x == 4 or self._x > 4:
            self._moves.remove("R")

        if self._y == 0 or self._y < 0:
            self._moves.remove("U")

        if self._y ==4 or self._y > 4:
            self._moves.remove("D")

2 Answers

Hi Jeff thank for you advise, i indeed realize that had a problem with the available list moves. Instead of using the original list of the instance, i make a copy of the available move in boardLimit method. So Each time the while loop start, i get a fresh new copy...Seem to fix the issue here is the code with this corrected version: https://repl.it/@jzzmatt/dungeongames

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,731 Points

Your work looks great! So glad you are taking advantage of repl.it for presenting your code.

You are definitely on track with your clear, concise, and highly-readble code. Good luck with your learning at Treehouse! I look forward to seeing more of your craft!