Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with rat race game
#1
How would I make a function that:
Moves the rat unless there is a wall
If there is a brussels sprout eat it and:
Increase the rat's num_sprouts_eaten variable by one
Decrease the maze's num_sprouts_left variable by one
Change the SPROUT to a HALL
Thanks for helping! Here's the code:

Code:
# Constants for the contents of the maze.

# The visual representation of a wall.
WALL = '#'

# The visual representation of a hallway.
HALL = '.'

# The visual representation of a brussels sprout.
SPROUT = '@'

# Constants for the directions. Use these to make Rats move.

# The left direction.
LEFT = -1

# The right direction.
RIGHT = 1

# No change in direction.
NO_CHANGE = 0

# The up direction.
UP = -1

# The down direction.
DOWN = 1

# The letters for rat_1 and rat_2 in the maze.
RAT_1_CHAR = 'J'
RAT_2_CHAR = 'P'


class Rat:
    """ A rat caught in a maze. """

    def __init__(self, symbol, row, col):
        ''' (Rat, str, int, int) -> NoneType

        A rat with symbol, row, column, and number of Brussel Sprouts eaten.
        '''

        self.symbol = symbol
        self.row = row
        self.col = col
        self.num_sprouts_eaten = 0

    def set_location(self, row, col):
        ''' (Rat, int, int) -> NoneType

        Set the rat's row and col instance variables to the given row and column

        >>> rat = Rat('k', 3, 1)
        >>> Rat.set_location(rat, 2, 4)
        >>> rat.row
        2
        >>> rat.col
        4
        '''

        self.row = row
        self.col = col
        
    def eat_sprout(self):
        ''' (Rat) -> NoneType

        Add one to the rats instance variable num_sprouts_eaten.

        >>> r = Rat('k', 4, 5)
        >>> r.num_sprouts_eaten
        0
        >>> Rat.eat_sprout(r)
        >>> r.num_sprouts_eaten
        1
        '''

        self.num_sprouts_eaten = self.num_sprouts_eaten + 1

    def __str__(self):
        ''' (Rat) -> str

        Return a string representation of the rat, in this format: symbol at (row, col) ate num_sprouts_eaten sprouts.

        >>> r = Rat('j', 4, 3)
        >>> str(r)
        'j at (4, 3) ate 0 sprouts.'
        '''
        
        return "{self.symbol} at ({self.row}, {self.col}) ate {self.num_sprouts_eaten} sprouts.".format(self=self)
        
    

class Maze:
    """ A 2D maze. """

    def __init__(self, maze, rat_1, rat_2):
        ''' (Maze, list of list of str, Rat, Rat)

        A rat maze with Brussel Sprouts.
        '''
        self.maze = maze
        self.rat_1 = rat_1
        self.rat_2 = rat_2
        num_sprouts_left = 0
        for lst in maze[:]:
            for ch in lst:
                if ch == SPROUT:
                    num_sprouts_left = num_sprouts_left + 1
        self.num_sprouts_left = num_sprouts_left
    
    def is_wall(self, row, col):
        ''' (Maze, int, int) -> bool

        Check to see if there is a wall at given coordinates.

        >>> maze = Maze([['#', '#', '#', '#', '#', '#', '#'], \
            ['#', '.', '.', '.', '.', '.', '#'], \
            ['#', '.', '#', '#', '#', '.', '#'], \
            ['#', '.', '.', '@', '#', '.', '#'], \
            ['#', '@', '#', '.', '@', '.', '#'], \
            ['#', '#', '#', '#', '#', '#', '#']], \
            Rat('J', 1, 1), \
            Rat('P', 1, 4))
        >>> Maze.is_wall(maze, 0, 0)
        True
        >>> Maze.is_wall(maze, 3, 2)
        False
        '''
        return self.maze[row][col] == WALL

    def get_character(self, row, col):
        ''' (Maze, int, int) -> str

        Return the character in the maze at the given row and column. If there is a rat at that location, then its character is returned.

        >>> maze = Maze([['#', '#', '#', '#', '#', '#', '#'], \
            ['#', '.', '.', '.', '.', '.', '#'], \
            ['#', '.', '#', '#', '#', '.', '#'], \
            ['#', '.', '.', '@', '#', '.', '#'], \
            ['#', '@', '#', '.', '@', '.', '#'], \
            ['#', '#', '#', '#', '#', '#', '#']], \
            Rat('J', 1, 1), \
            Rat('P', 1, 4))
        >>> Maze.get_character(maze, 4, 5)
        'HALL'
        >>> Maze.get_character(maze, 3, 4)
        'WALL'
        >>> Maze.get_character(maze, 3, 3)
        'SPROUT'
        '''

        if self.maze[row][col] == self.rat_1.symbol:
            return self.rat_1.symbol
        elif self.maze[row][col] == self.rat_2.symbol:
            return self.rat_2.symbol
        elif self.maze[row][col] == '.':
            return 'HALL'
        elif self.maze[row][col] == '#':
            return 'WALL'
        elif self.maze[row][col] == '@':
            return 'SPROUT'


        
                        
                    
            


    def __str__(self):
        """ (Maze) -> str

        Return a string representation of the maze.
        """
        
        return '{self.maze}  {self.rat_1} {self.rat_2}'.format(self=self)
Reply
#2
This is an annoying one i feel your pain, havent done these for quite some time now
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)